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