IntervalTree is now support multiple intervals with same lower bound.

* New: Interval::CompareByMinMax, lexicographical comparison of
    Intervals, first compare the lower bound (VMin) then the
    upper bound (VMax). Needed for IntervalTree.
* Change: In IntervalTree, the key comparison was only based on
    the lower bound (VMin). That means that the tree would accept
    only one interval for a given VMin. You couldn't store both
    [1:4] and [1:5]. Now use CompareByMinMax that allows it.
This commit is contained in:
Jean-Paul Chaput 2023-03-28 14:52:23 +02:00
parent a0911be50a
commit e22e7f7476
3 changed files with 29 additions and 18 deletions

View File

@ -1,6 +1,6 @@
// -*- C++ -*-
//
// Copyright (c) BULL S.A. 2000-2018, All Rights Reserved
// Copyright (c) BULL S.A. 2000-2023, All Rights Reserved
//
// This file is part of Hurricane.
//
@ -29,9 +29,7 @@
// +-----------------------------------------------------------------+
#ifndef HURRICANE_INTERVAL_H
#define HURRICANE_INTERVAL_H
#pragma once
#include "hurricane/DbU.h"
namespace Hurricane {
@ -48,6 +46,11 @@ namespace Hurricane {
inline bool operator() ( const Interval& rhs, const Interval& lhs ) const;
inline bool operator() ( const Interval* rhs, const Interval* lhs ) const;
};
class CompareByMinMax {
public:
inline bool operator() ( const Interval& rhs, const Interval& lhs ) const;
inline bool operator() ( const Interval* rhs, const Interval* lhs ) const;
};
public:
Interval ( bool makeEmpty=true );
Interval ( const DbU::Unit& );
@ -124,6 +127,20 @@ namespace Hurricane {
{ return lhs->getVMin() < rhs->getVMin(); }
inline bool Interval::CompareByMinMax::operator() ( const Interval& lhs, const Interval& rhs ) const
{
if (lhs.getVMin() != rhs.getVMin()) return lhs.getVMin() < rhs.getVMin();
return lhs.getVMax() < rhs.getVMax();
}
inline bool Interval::CompareByMinMax::operator() ( const Interval* lhs, const Interval* rhs ) const
{
if (lhs->getVMin() != rhs->getVMin()) return lhs->getVMin() < rhs->getVMin();
return lhs->getVMax() < rhs->getVMax();
}
} // Hurricane namespace.
@ -136,7 +153,5 @@ inline void jsonWrite ( JsonWriter* w, const std::string& key, const Hurricane:
w->endArray();
}
INSPECTOR_PR_SUPPORT(Hurricane::Interval);
#endif // HURRICANE_INTERVAL_H

View File

@ -1,6 +1,6 @@
// -*- C++ -*-
//
// Copyright (c) BULL S.A. 2018-2018, All Rights Reserved
// Copyright (c) BULL S.A. 2018-2023, All Rights Reserved
//
// This file is part of Hurricane.
//
@ -34,9 +34,7 @@
// Third edition, MIT press, 2011, p. 348.
#ifndef HURRICANE_INTERVAL_TREE_H
#define HURRICANE_INTERVAL_TREE_H
#pragma once
#include "hurricane/Interval.h"
#include "hurricane/RbTree.h"
@ -117,14 +115,14 @@ namespace Hurricane {
return record;
}
// -------------------------------------------------------------------
// Class : "Hurricane::IntervalTree".
template< typename Data >
class IntervalTree : public RbTree< IntervalData<Data>, Interval::CompareByMin > {
class IntervalTree : public RbTree< IntervalData<Data>, Interval::CompareByMinMax > {
public:
typedef RbTree< IntervalData<Data>, Interval::CompareByMin > Super;
typedef RbTree< IntervalData<Data>, Interval::CompareByMinMax > Super;
public:
class overlap_iterator : public Super::iterator {
public:
@ -414,5 +412,3 @@ namespace Hurricane {
} // HUrricane namespace.
#endif // HURRICANE_INTERVAL_TREE_H

View File

@ -557,10 +557,10 @@ namespace Hurricane {
Node* current = root_;
while ( current ) {
cdebug_log(0,0) << "| " << current << std::endl;
cdebug_log(0,0) << "| " << current->getValue() << std::endl;
if (current->getValue() == value) {
cdebug_log(0,-1) << "> Value found: " << current <<std::endl;
cdebug_log(0,-1) << "> Value found: " << current->getValue() <<std::endl;
return iterator(current);
}