1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <IntTools_CArray1OfReal.hxx>
16 #include <IntTools_MarkedRangeSet.hxx>
17 #include <IntTools_Range.hxx>
19 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet() :
24 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const Standard_Real theFirstBoundary,
25 const Standard_Real theLastBoundary,
26 const Standard_Integer theInitFlag)
28 SetBoundaries(theFirstBoundary, theLastBoundary, theInitFlag);
31 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const IntTools_CArray1OfReal& theSortedArray,
32 const Standard_Integer theInitFlag)
35 SetRanges(theSortedArray, theInitFlag);
38 void IntTools_MarkedRangeSet::SetBoundaries(const Standard_Real theFirstBoundary,
39 const Standard_Real theLastBoundary,
40 const Standard_Integer theInitFlag)
42 myRangeSetStorer.Clear();
43 myRangeSetStorer.Append(theFirstBoundary);
44 myRangeSetStorer.Append(theLastBoundary);
47 myFlags.Append(theInitFlag);
50 void IntTools_MarkedRangeSet::SetRanges(const IntTools_CArray1OfReal& theSortedArray,
51 const Standard_Integer theInitFlag)
53 myRangeSetStorer.Clear();
55 Standard_Integer i = 0;
57 for(i = 0; i < theSortedArray.Length(); i++) {
58 myRangeSetStorer.Append(theSortedArray(i));
60 myRangeNumber = myRangeSetStorer.Length() - 1;
62 for(i = 1; i <= myRangeNumber; i++) {
63 myFlags.Append(theInitFlag);
67 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
68 const Standard_Real theLastBoundary,
69 const Standard_Integer theFlag)
71 Standard_Integer anIndex1 = GetIndex(theFirstBoundary, Standard_True);
74 return Standard_False;
75 Standard_Integer anIndex2 = GetIndex(theLastBoundary, Standard_False);
78 return Standard_False;
80 if(anIndex2 < anIndex1) { // it can be if theLastBoundary==theFirstBoundary==boundary_of_a_range or theFirstBoundary > theLastBoundary
81 Standard_Integer atmpindex = anIndex1;
85 if(theLastBoundary < theFirstBoundary)
86 return Standard_False;
89 Standard_Boolean areEqualIndices = (anIndex1 == anIndex2);
90 Standard_Integer aPrevFlag = myFlags(anIndex1);
92 myRangeSetStorer.InsertAfter(anIndex1, theFirstBoundary);
94 myFlags.InsertAfter(anIndex1, theFlag);
95 myRangeNumber = myRangeSetStorer.Length() - 1;
97 myRangeSetStorer.InsertAfter(anIndex2, theLastBoundary);
100 myFlags.InsertAfter(anIndex2, aPrevFlag);
103 myFlags.InsertBefore(anIndex2, theFlag);
106 if(!areEqualIndices) {
110 for(Standard_Integer i = anIndex1; i < anIndex2; i++) {
111 myFlags.SetValue(i, theFlag);
115 myRangeNumber = myRangeSetStorer.Length() - 1;
117 return Standard_True;
120 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
121 const Standard_Integer theFlag)
123 return InsertRange(theRange.First(), theRange.Last(), theFlag);
126 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
127 const Standard_Real theLastBoundary,
128 const Standard_Integer theFlag,
129 const Standard_Integer theIndex)
131 Standard_Real aTolerance = 1.e-15;
132 Standard_Integer anIndex = theIndex;
134 if((theIndex <= 0) || (theIndex > myRangeNumber))
135 return Standard_False;
137 if((theFirstBoundary < myRangeSetStorer(theIndex)) ||
138 (theLastBoundary > myRangeSetStorer(theIndex+1)) ||
139 (Abs(theFirstBoundary - theLastBoundary) < aTolerance)) {
140 return InsertRange(theFirstBoundary, theLastBoundary, theFlag);
143 Standard_Integer aPrevFlag = myFlags(anIndex);
145 if((Abs(theFirstBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
146 (Abs(theFirstBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
147 myRangeSetStorer.InsertAfter(anIndex, theFirstBoundary);
148 myFlags.InsertAfter(anIndex, theFlag);
150 myRangeNumber = myRangeSetStorer.Length() - 1;
153 myFlags.SetValue(anIndex, theFlag);
156 if((Abs(theLastBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
157 (Abs(theLastBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
158 myRangeSetStorer.InsertAfter(anIndex, theLastBoundary);
159 myRangeNumber = myRangeSetStorer.Length() - 1;
160 myFlags.InsertAfter(anIndex, aPrevFlag);
164 return Standard_True;
167 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
168 const Standard_Integer theFlag,
169 const Standard_Integer theIndex)
171 return InsertRange(theRange.First(), theRange.Last(), theFlag, theIndex);
174 void IntTools_MarkedRangeSet::SetFlag(const Standard_Integer theIndex,
175 const Standard_Integer theFlag)
177 myFlags.SetValue(theIndex, theFlag);
180 Standard_Integer IntTools_MarkedRangeSet::Flag(const Standard_Integer theIndex) const
182 return myFlags(theIndex);
185 const TColStd_SequenceOfInteger& IntTools_MarkedRangeSet::GetIndices(const Standard_Real theValue)
187 myFoundIndices.Clear();
189 if(theValue < myRangeSetStorer(1))
190 return myFoundIndices;
192 Standard_Boolean found = Standard_False;
194 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
196 if(theValue >= myRangeSetStorer(i-1)) {
197 myFoundIndices.Append(i-1);
204 if(theValue <= myRangeSetStorer(i)) {
205 myFoundIndices.Append(i-1);
206 found = Standard_True;
216 return myFoundIndices;
220 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue) const
222 Standard_Integer anIndex = 0;
224 if(theValue < myRangeSetStorer(1))
227 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
228 if(theValue <= myRangeSetStorer(i)) {
238 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue,
239 const Standard_Boolean UseLower) const
241 Standard_Integer anIndex = 0;
243 if((UseLower && (theValue < myRangeSetStorer(1))) ||
244 (!UseLower && (theValue <= myRangeSetStorer(1))))
247 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
248 if((UseLower && theValue < myRangeSetStorer(i)) ||
249 (!UseLower && theValue <= myRangeSetStorer(i))) {
258 IntTools_Range IntTools_MarkedRangeSet::Range(const Standard_Integer theIndex) const
260 IntTools_Range aRange(myRangeSetStorer(theIndex), myRangeSetStorer(theIndex+1));