1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <IntTools_MarkedRangeSet.hxx>
15
16 #include <IntTools_Range.hxx>
17
IntTools_MarkedRangeSet()18 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet() :
19 myRangeNumber(0)
20 {
21 }
22
IntTools_MarkedRangeSet(const Standard_Real theFirstBoundary,const Standard_Real theLastBoundary,const Standard_Integer theInitFlag)23 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const Standard_Real theFirstBoundary,
24 const Standard_Real theLastBoundary,
25 const Standard_Integer theInitFlag)
26 {
27 SetBoundaries(theFirstBoundary, theLastBoundary, theInitFlag);
28 }
29
IntTools_MarkedRangeSet(const TColStd_Array1OfReal & theSortedArray,const Standard_Integer theInitFlag)30 IntTools_MarkedRangeSet::IntTools_MarkedRangeSet (const TColStd_Array1OfReal& theSortedArray,
31 const Standard_Integer theInitFlag)
32 {
33 SetRanges(theSortedArray, theInitFlag);
34 }
35
SetBoundaries(const Standard_Real theFirstBoundary,const Standard_Real theLastBoundary,const Standard_Integer theInitFlag)36 void IntTools_MarkedRangeSet::SetBoundaries(const Standard_Real theFirstBoundary,
37 const Standard_Real theLastBoundary,
38 const Standard_Integer theInitFlag)
39 {
40 myRangeSetStorer.Clear();
41 myRangeSetStorer.Append(theFirstBoundary);
42 myRangeSetStorer.Append(theLastBoundary);
43 myRangeNumber = 1;
44 myFlags.Clear();
45 myFlags.Append(theInitFlag);
46 }
47
SetRanges(const TColStd_Array1OfReal & theSortedArray,const Standard_Integer theInitFlag)48 void IntTools_MarkedRangeSet::SetRanges (const TColStd_Array1OfReal& theSortedArray,
49 const Standard_Integer theInitFlag)
50 {
51 myRangeSetStorer.Clear();
52 myFlags.Clear();
53 for (TColStd_Array1OfReal::Iterator aRangeIter (theSortedArray); aRangeIter.More(); aRangeIter.Next())
54 {
55 myRangeSetStorer.Append (aRangeIter.Value());
56 }
57 myRangeNumber = myRangeSetStorer.Length() - 1;
58
59 for (Standard_Integer i = 1; i <= myRangeNumber; i++)
60 {
61 myFlags.Append(theInitFlag);
62 }
63 }
64
InsertRange(const Standard_Real theFirstBoundary,const Standard_Real theLastBoundary,const Standard_Integer theFlag)65 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
66 const Standard_Real theLastBoundary,
67 const Standard_Integer theFlag)
68 {
69 Standard_Integer anIndex1 = GetIndex(theFirstBoundary, Standard_True);
70
71 if(!anIndex1)
72 return Standard_False;
73 Standard_Integer anIndex2 = GetIndex(theLastBoundary, Standard_False);
74
75 if(!anIndex2)
76 return Standard_False;
77
78 if(anIndex2 < anIndex1) { // it can be if theLastBoundary==theFirstBoundary==boundary_of_a_range or theFirstBoundary > theLastBoundary
79 Standard_Integer atmpindex = anIndex1;
80 anIndex1 = anIndex2;
81 anIndex2 = atmpindex;
82
83 if(theLastBoundary < theFirstBoundary)
84 return Standard_False;
85 }
86
87 Standard_Boolean areEqualIndices = (anIndex1 == anIndex2);
88 Standard_Integer aPrevFlag = myFlags(anIndex1);
89
90 myRangeSetStorer.InsertAfter(anIndex1, theFirstBoundary);
91 anIndex2++;
92 myFlags.InsertAfter(anIndex1, theFlag);
93 myRangeNumber = myRangeSetStorer.Length() - 1;
94
95 myRangeSetStorer.InsertAfter(anIndex2, theLastBoundary);
96
97 if(areEqualIndices) {
98 myFlags.InsertAfter(anIndex2, aPrevFlag);
99 }
100 else {
101 myFlags.InsertBefore(anIndex2, theFlag);
102 }
103
104 if(!areEqualIndices) {
105 anIndex1++;
106 anIndex2++;
107
108 for(Standard_Integer i = anIndex1; i < anIndex2; i++) {
109 myFlags.SetValue(i, theFlag);
110 }
111 }
112
113 myRangeNumber = myRangeSetStorer.Length() - 1;
114
115 return Standard_True;
116 }
117
InsertRange(const IntTools_Range & theRange,const Standard_Integer theFlag)118 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
119 const Standard_Integer theFlag)
120 {
121 return InsertRange(theRange.First(), theRange.Last(), theFlag);
122 }
123
InsertRange(const Standard_Real theFirstBoundary,const Standard_Real theLastBoundary,const Standard_Integer theFlag,const Standard_Integer theIndex)124 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary,
125 const Standard_Real theLastBoundary,
126 const Standard_Integer theFlag,
127 const Standard_Integer theIndex)
128 {
129 Standard_Real aTolerance = 1.e-15;
130 Standard_Integer anIndex = theIndex;
131
132 if((theIndex <= 0) || (theIndex > myRangeNumber))
133 return Standard_False;
134
135 if((theFirstBoundary < myRangeSetStorer(theIndex)) ||
136 (theLastBoundary > myRangeSetStorer(theIndex+1)) ||
137 (Abs(theFirstBoundary - theLastBoundary) < aTolerance)) {
138 return InsertRange(theFirstBoundary, theLastBoundary, theFlag);
139 }
140 else {
141 Standard_Integer aPrevFlag = myFlags(anIndex);
142
143 if((Abs(theFirstBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
144 (Abs(theFirstBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
145 myRangeSetStorer.InsertAfter(anIndex, theFirstBoundary);
146 myFlags.InsertAfter(anIndex, theFlag);
147 anIndex++;
148 myRangeNumber = myRangeSetStorer.Length() - 1;
149 }
150 else {
151 myFlags.SetValue(anIndex, theFlag);
152 }
153
154 if((Abs(theLastBoundary - myRangeSetStorer(anIndex)) > aTolerance) &&
155 (Abs(theLastBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) {
156 myRangeSetStorer.InsertAfter(anIndex, theLastBoundary);
157 myRangeNumber = myRangeSetStorer.Length() - 1;
158 myFlags.InsertAfter(anIndex, aPrevFlag);
159 }
160 }
161
162 return Standard_True;
163 }
164
InsertRange(const IntTools_Range & theRange,const Standard_Integer theFlag,const Standard_Integer theIndex)165 Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange,
166 const Standard_Integer theFlag,
167 const Standard_Integer theIndex)
168 {
169 return InsertRange(theRange.First(), theRange.Last(), theFlag, theIndex);
170 }
171
SetFlag(const Standard_Integer theIndex,const Standard_Integer theFlag)172 void IntTools_MarkedRangeSet::SetFlag(const Standard_Integer theIndex,
173 const Standard_Integer theFlag)
174 {
175 myFlags.SetValue(theIndex, theFlag);
176 }
177
Flag(const Standard_Integer theIndex) const178 Standard_Integer IntTools_MarkedRangeSet::Flag(const Standard_Integer theIndex) const
179 {
180 return myFlags(theIndex);
181 }
182
GetIndices(const Standard_Real theValue)183 const TColStd_SequenceOfInteger& IntTools_MarkedRangeSet::GetIndices(const Standard_Real theValue)
184 {
185 myFoundIndices.Clear();
186
187 if(theValue < myRangeSetStorer(1))
188 return myFoundIndices;
189 else {
190 Standard_Boolean found = Standard_False;
191
192 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
193 if(found) {
194 if(theValue >= myRangeSetStorer(i-1)) {
195 myFoundIndices.Append(i-1);
196 }
197 else {
198 break;
199 }
200 }
201 else {
202 if(theValue <= myRangeSetStorer(i)) {
203 myFoundIndices.Append(i-1);
204 found = Standard_True;
205 }
206 else {
207 if(found) {
208 break;
209 }
210 }
211 }
212 }
213 }
214 return myFoundIndices;
215 }
216
217
GetIndex(const Standard_Real theValue) const218 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue) const
219 {
220 Standard_Integer anIndex = 0;
221
222 if(theValue < myRangeSetStorer(1))
223 anIndex = 0;
224 else {
225 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
226 if(theValue <= myRangeSetStorer(i)) {
227 anIndex = i-1;
228 break;
229 }
230 }
231 }
232
233 return anIndex;
234 }
235
GetIndex(const Standard_Real theValue,const Standard_Boolean UseLower) const236 Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue,
237 const Standard_Boolean UseLower) const
238 {
239 Standard_Integer anIndex = 0;
240
241 if((UseLower && (theValue < myRangeSetStorer(1))) ||
242 (!UseLower && (theValue <= myRangeSetStorer(1))))
243 anIndex = 0;
244 else {
245 for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) {
246 if((UseLower && theValue < myRangeSetStorer(i)) ||
247 (!UseLower && theValue <= myRangeSetStorer(i))) {
248 anIndex = i-1;
249 break;
250 }
251 }
252 }
253 return anIndex;
254 }
255
Range(const Standard_Integer theIndex) const256 IntTools_Range IntTools_MarkedRangeSet::Range(const Standard_Integer theIndex) const
257 {
258 IntTools_Range aRange(myRangeSetStorer(theIndex), myRangeSetStorer(theIndex+1));
259 return aRange;
260 }
261