b311480e |
1 | // Created on: 2007-05-29 |
2 | // Created by: Vlad Romashko |
973c2be1 |
3 | // Copyright (c) 2007-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #include <TDataStd_BooleanArray.ixx> |
17 | |
18 | static Standard_Integer DegreeOf2(const Standard_Integer degree) |
19 | { |
20 | switch (degree) |
21 | { |
22 | case 0: |
23 | return 1; |
24 | case 1: |
25 | return 2; |
26 | case 2: |
27 | return 4; |
28 | case 3: |
29 | return 8; |
30 | case 4: |
31 | return 16; |
32 | case 5: |
33 | return 32; |
34 | case 6: |
35 | return 64; |
36 | case 7: |
37 | return 128; |
38 | case 8: |
39 | return 256; |
40 | } |
41 | return -1; |
42 | } |
43 | |
44 | //======================================================================= |
45 | //function : GetID |
46 | //purpose : |
47 | //======================================================================= |
48 | const Standard_GUID& TDataStd_BooleanArray::GetID() |
49 | { |
50 | static Standard_GUID TDataStd_BooleanArrayID ("C7E98E54-B5EA-4aa9-AC99-9164EBD07F10"); |
51 | return TDataStd_BooleanArrayID; |
52 | } |
53 | |
54 | //======================================================================= |
55 | //function : TDataStd_BooleanArray |
56 | //purpose : Empty Constructor |
57 | //======================================================================= |
58 | TDataStd_BooleanArray::TDataStd_BooleanArray() |
59 | { |
60 | |
61 | } |
62 | |
63 | //======================================================================= |
64 | //function : Init |
65 | //purpose : |
66 | //======================================================================= |
67 | void TDataStd_BooleanArray::Init(const Standard_Integer lower, |
68 | const Standard_Integer upper) |
69 | { |
70 | Backup(); |
71 | |
72 | myLower = lower; |
73 | myUpper = upper; |
74 | if (myUpper >= myLower) |
75 | myValues = new TColStd_HArray1OfByte(0, Length() >> 3, 0/*initialize to FALSE*/); |
76 | } |
77 | |
78 | //======================================================================= |
79 | //function : Set |
80 | //purpose : |
81 | //======================================================================= |
82 | Handle(TDataStd_BooleanArray) TDataStd_BooleanArray::Set(const TDF_Label& label, |
83 | const Standard_Integer lower, |
84 | const Standard_Integer upper) |
85 | { |
86 | Handle(TDataStd_BooleanArray) A; |
87 | if (!label.FindAttribute (TDataStd_BooleanArray::GetID(), A)) |
88 | { |
89 | A = new TDataStd_BooleanArray; |
90 | A->Init (lower, upper); |
91 | label.AddAttribute(A); |
92 | } |
93 | else if (lower != A->Lower() || upper != A->Upper()) |
94 | { |
95 | A->Init(lower, upper); |
96 | } |
97 | return A; |
98 | } |
99 | |
100 | |
101 | //======================================================================= |
102 | //function : SetValue |
103 | //purpose : |
104 | //======================================================================= |
105 | void TDataStd_BooleanArray::SetValue (const Standard_Integer index, |
106 | const Standard_Boolean value) |
107 | { |
7fd59977 |
108 | |
fa13a85d |
109 | if (myValues.IsNull()) |
110 | return; |
7fd59977 |
111 | Standard_Integer byte_index = (index - myLower) >> 3; |
112 | Standard_Integer degree = index - (byte_index << 3) - myLower; |
113 | Standard_Integer byte_value = DegreeOf2(degree); |
114 | |
498ce76b |
115 | if ((value != 0) == ((myValues->Value(byte_index) & byte_value) > 0)) |
7fd59977 |
116 | return; |
117 | |
118 | Backup(); |
119 | |
120 | if (value) |
121 | { |
122 | myValues->ChangeValue(byte_index) |= byte_value; |
123 | } |
124 | else |
125 | { |
126 | myValues->ChangeValue(byte_index) ^= byte_value; |
127 | } |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : Value |
132 | //purpose : |
133 | //======================================================================= |
134 | Standard_Boolean TDataStd_BooleanArray::Value (const Standard_Integer index) const |
135 | { |
136 | if (myValues.IsNull()) |
137 | return Standard_False; |
138 | if (index < myLower || index > myUpper) |
139 | return Standard_False; |
140 | |
141 | Standard_Integer byte_index = (index - myLower) >> 3; |
142 | Standard_Integer degree = index - (byte_index << 3) - myLower; |
143 | Standard_Integer byte_value = DegreeOf2(degree); |
144 | |
145 | return (myValues->Value(byte_index) & byte_value) > 0; |
146 | } |
147 | |
148 | //======================================================================= |
149 | //function : Lower |
150 | //purpose : |
151 | //======================================================================= |
152 | Standard_Integer TDataStd_BooleanArray::Lower (void) const |
153 | { |
154 | return myLower; |
155 | } |
156 | |
157 | //======================================================================= |
158 | //function : Upper |
159 | //purpose : |
160 | //======================================================================= |
161 | Standard_Integer TDataStd_BooleanArray::Upper (void) const |
162 | { |
163 | return myUpper; |
164 | } |
165 | |
166 | //======================================================================= |
167 | //function : Length |
168 | //purpose : |
169 | //======================================================================= |
170 | Standard_Integer TDataStd_BooleanArray::Length (void) const |
171 | { |
172 | return myUpper - myLower + 1; |
173 | } |
174 | |
175 | //======================================================================= |
176 | //function : InternalArray |
177 | //purpose : |
178 | //======================================================================= |
179 | const Handle(TColStd_HArray1OfByte)& TDataStd_BooleanArray::InternalArray () const |
180 | { |
181 | return myValues; |
182 | } |
183 | |
184 | //======================================================================= |
185 | //function : SetInternalArray |
186 | //purpose : |
187 | //======================================================================= |
188 | void TDataStd_BooleanArray::SetInternalArray (const Handle(TColStd_HArray1OfByte)& values) |
189 | { |
190 | myValues = values; |
191 | } |
192 | |
193 | //======================================================================= |
194 | //function : ID |
195 | //purpose : |
196 | //======================================================================= |
197 | const Standard_GUID& TDataStd_BooleanArray::ID () const |
198 | { |
199 | return GetID(); |
200 | } |
201 | |
202 | //======================================================================= |
203 | //function : NewEmpty |
204 | //purpose : |
205 | //======================================================================= |
206 | Handle(TDF_Attribute) TDataStd_BooleanArray::NewEmpty () const |
207 | { |
208 | return new TDataStd_BooleanArray(); |
209 | } |
210 | |
211 | //======================================================================= |
212 | //function : Restore |
213 | //purpose : |
214 | //======================================================================= |
215 | void TDataStd_BooleanArray::Restore(const Handle(TDF_Attribute)& With) |
216 | { |
217 | Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(With); |
218 | if (!anArray->myValues.IsNull()) |
219 | { |
220 | myLower = anArray->Lower(); |
221 | myUpper = anArray->Upper(); |
222 | Standard_Integer byte_upper = Length() >> 3; |
223 | myValues = new TColStd_HArray1OfByte(0, byte_upper, 0/*initialize to FALSE*/); |
224 | const TColStd_Array1OfByte& with_array = anArray->myValues->Array1(); |
225 | for (Standard_Integer i = 0; i <= byte_upper; i++) |
226 | { |
227 | myValues->SetValue(i, with_array.Value(i)); |
228 | } |
229 | } |
230 | else |
231 | { |
232 | myValues.Nullify(); |
233 | } |
234 | } |
235 | |
236 | //======================================================================= |
237 | //function : Paste |
238 | //purpose : |
239 | //======================================================================= |
240 | void TDataStd_BooleanArray::Paste (const Handle(TDF_Attribute)& Into, |
241 | const Handle(TDF_RelocationTable)& ) const |
242 | { |
243 | if (!myValues.IsNull()) |
244 | { |
245 | Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(Into); |
246 | if (!anArray.IsNull()) |
247 | { |
248 | anArray->Init(myLower, myUpper); |
249 | for (Standard_Integer i = myLower; i <= myUpper; i++) |
250 | { |
251 | anArray->SetValue(i, Value(i)); |
252 | } |
253 | } |
254 | } |
255 | } |
256 | |
257 | //======================================================================= |
258 | //function : Dump |
259 | //purpose : |
260 | //======================================================================= |
261 | Standard_OStream& TDataStd_BooleanArray::Dump (Standard_OStream& anOS) const |
262 | { |
263 | anOS << "BooleanArray"; |
264 | return anOS; |
265 | } |