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