1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <IntTools_MarkedRangeSet.ixx>
20 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet() :
25 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const Standard_Real theFirstBoundary,
26 const Standard_Real theLastBoundary,
27 const Standard_Integer theInitFlag)
29 SetBoundaries(theFirstBoundary, theLastBoundary, theInitFlag);
32 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const IntTools_CArray1OfReal& theSortedArray,
33 const Standard_Integer theInitFlag)
36 SetRanges(theSortedArray, theInitFlag);
39 void IntTools_MarkedRangeSet::SetBoundaries(const Standard_Real theFirstBoundary,
40 const Standard_Real theLastBoundary,
41 const Standard_Integer theInitFlag)
43 myRangeSetStorer.Clear();
44 myRangeSetStorer.Append(theFirstBoundary);
45 myRangeSetStorer.Append(theLastBoundary);
48 myFlags.Append(theInitFlag);
51 void IntTools_MarkedRangeSet::SetRanges(const IntTools_CArray1OfReal& theSortedArray,
52 const Standard_Integer theInitFlag)
54 myRangeSetStorer.Clear();
56 Standard_Integer i = 0;
58 for(i = 0; i < theSortedArray.Length(); i++) {
59 myRangeSetStorer.Append(theSortedArray(i));
61 myRangeNumber = myRangeSetStorer.Length() - 1;
63 for(i = 1; i <= myRangeNumber; i++) {
64 myFlags.Append(theInitFlag);
68 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
69 const Standard_Real theLastBoundary,
70 const Standard_Integer theFlag)
72 Standard_Integer anIndex1 = GetIndex(theFirstBoundary, Standard_True);
75 return Standard_False;
76 Standard_Integer anIndex2 = GetIndex(theLastBoundary, Standard_False);
79 return Standard_False;
81 if(anIndex2 < anIndex1) { // it can be if theLastBoundary==theFirstBoundary==boundary_of_a_range or theFirstBoundary > theLastBoundary
82 Standard_Integer atmpindex = anIndex1;
86 if(theLastBoundary < theFirstBoundary)
87 return Standard_False;
90 Standard_Boolean areEqualIndices = (anIndex1 == anIndex2);
91 Standard_Integer aPrevFlag = myFlags(anIndex1);
93 myRangeSetStorer.InsertAfter(anIndex1, theFirstBoundary);
95 myFlags.InsertAfter(anIndex1, theFlag);
96 myRangeNumber = myRangeSetStorer.Length() - 1;
98 myRangeSetStorer.InsertAfter(anIndex2, theLastBoundary);
100 if(areEqualIndices) {
101 myFlags.InsertAfter(anIndex2, aPrevFlag);
104 myFlags.InsertBefore(anIndex2, theFlag);
107 if(!areEqualIndices) {
111 for(Standard_Integer i = anIndex1; i < anIndex2; i++) {
112 myFlags.SetValue(i, theFlag);
116 myRangeNumber = myRangeSetStorer.Length() - 1;
118 return Standard_True;
121 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
122 const Standard_Integer theFlag)
124 return InsertRange(theRange.First(), theRange.Last(), theFlag);
127 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
128 const Standard_Real theLastBoundary,
129 const Standard_Integer theFlag,
130 const Standard_Integer theIndex)
132 Standard_Real aTolerance = 1.e-15;
133 Standard_Integer anIndex = theIndex;
135 if((theIndex <= 0) || (theIndex > myRangeNumber))
136 return Standard_False;
138 if((theFirstBoundary < myRangeSetStorer(theIndex)) ||
139 (theLastBoundary > myRangeSetStorer(theIndex+1)) ||
140 (Abs(theFirstBoundary - theLastBoundary) < aTolerance)) {
141 return InsertRange(theFirstBoundary, theLastBoundary, theFlag);
144 Standard_Integer aPrevFlag = myFlags(anIndex);
146 if((Abs(theFirstBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
147 (Abs(theFirstBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
148 myRangeSetStorer.InsertAfter(anIndex, theFirstBoundary);
149 myFlags.InsertAfter(anIndex, theFlag);
151 myRangeNumber = myRangeSetStorer.Length() - 1;
154 myFlags.SetValue(anIndex, theFlag);
157 if((Abs(theLastBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
158 (Abs(theLastBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
159 myRangeSetStorer.InsertAfter(anIndex, theLastBoundary);
160 myRangeNumber = myRangeSetStorer.Length() - 1;
161 myFlags.InsertAfter(anIndex, aPrevFlag);
165 return Standard_True;
168 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
169 const Standard_Integer theFlag,
170 const Standard_Integer theIndex)
172 return InsertRange(theRange.First(), theRange.Last(), theFlag, theIndex);
175 void IntTools_MarkedRangeSet::SetFlag(const Standard_Integer theIndex,
176 const Standard_Integer theFlag)
178 myFlags.SetValue(theIndex, theFlag);
181 Standard_Integer IntTools_MarkedRangeSet::Flag(const Standard_Integer theIndex) const
183 return myFlags(theIndex);
186 const TColStd_SequenceOfInteger& IntTools_MarkedRangeSet::GetIndices(const Standard_Real theValue)
188 myFoundIndices.Clear();
190 if(theValue < myRangeSetStorer(1))
191 return myFoundIndices;
193 Standard_Boolean found = Standard_False;
195 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
197 if(theValue >= myRangeSetStorer(i-1)) {
198 myFoundIndices.Append(i-1);
205 if(theValue <= myRangeSetStorer(i)) {
206 myFoundIndices.Append(i-1);
207 found = Standard_True;
217 return myFoundIndices;
221 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue) const
223 Standard_Integer anIndex = 0;
225 if(theValue < myRangeSetStorer(1))
228 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
229 if(theValue <= myRangeSetStorer(i)) {
239 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue,
240 const Standard_Boolean UseLower) const
242 Standard_Integer anIndex = 0;
244 if(UseLower && (theValue < myRangeSetStorer(1)) ||
245 (!UseLower && (theValue <= myRangeSetStorer(1))))
248 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
249 if((UseLower && theValue < myRangeSetStorer(i)) ||
250 (!UseLower && theValue <= myRangeSetStorer(i))) {
259 IntTools_Range IntTools_MarkedRangeSet::Range(const Standard_Integer theIndex) const
261 IntTools_Range aRange(myRangeSetStorer(theIndex), myRangeSetStorer(theIndex+1));