1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <TDataStd_BooleanArray.ixx>
23 static Standard_Integer DegreeOf2(const Standard_Integer degree)
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& TDataStd_BooleanArray::GetID()
55 static Standard_GUID TDataStd_BooleanArrayID ("C7E98E54-B5EA-4aa9-AC99-9164EBD07F10");
56 return TDataStd_BooleanArrayID;
59 //=======================================================================
60 //function : TDataStd_BooleanArray
61 //purpose : Empty Constructor
62 //=======================================================================
63 TDataStd_BooleanArray::TDataStd_BooleanArray()
68 //=======================================================================
71 //=======================================================================
72 void TDataStd_BooleanArray::Init(const Standard_Integer lower,
73 const Standard_Integer upper)
79 if (myUpper >= myLower)
80 myValues = new TColStd_HArray1OfByte(0, Length() >> 3, 0/*initialize to FALSE*/);
83 //=======================================================================
86 //=======================================================================
87 Handle(TDataStd_BooleanArray) TDataStd_BooleanArray::Set(const TDF_Label& label,
88 const Standard_Integer lower,
89 const Standard_Integer upper)
91 Handle(TDataStd_BooleanArray) A;
92 if (!label.FindAttribute (TDataStd_BooleanArray::GetID(), A))
94 A = new TDataStd_BooleanArray;
95 A->Init (lower, upper);
96 label.AddAttribute(A);
98 else if (lower != A->Lower() || upper != A->Upper())
100 A->Init(lower, upper);
106 //=======================================================================
107 //function : SetValue
109 //=======================================================================
110 void TDataStd_BooleanArray::SetValue (const Standard_Integer index,
111 const Standard_Boolean value)
114 if (myValues.IsNull())
116 Standard_Integer byte_index = (index - myLower) >> 3;
117 Standard_Integer degree = index - (byte_index << 3) - myLower;
118 Standard_Integer byte_value = DegreeOf2(degree);
120 if ((value != 0) == ((myValues->Value(byte_index) & byte_value) > 0))
127 myValues->ChangeValue(byte_index) |= byte_value;
131 myValues->ChangeValue(byte_index) ^= byte_value;
135 //=======================================================================
138 //=======================================================================
139 Standard_Boolean TDataStd_BooleanArray::Value (const Standard_Integer index) const
141 if (myValues.IsNull())
142 return Standard_False;
143 if (index < myLower || index > myUpper)
144 return Standard_False;
146 Standard_Integer byte_index = (index - myLower) >> 3;
147 Standard_Integer degree = index - (byte_index << 3) - myLower;
148 Standard_Integer byte_value = DegreeOf2(degree);
150 return (myValues->Value(byte_index) & byte_value) > 0;
153 //=======================================================================
156 //=======================================================================
157 Standard_Integer TDataStd_BooleanArray::Lower (void) const
162 //=======================================================================
165 //=======================================================================
166 Standard_Integer TDataStd_BooleanArray::Upper (void) const
171 //=======================================================================
174 //=======================================================================
175 Standard_Integer TDataStd_BooleanArray::Length (void) const
177 return myUpper - myLower + 1;
180 //=======================================================================
181 //function : InternalArray
183 //=======================================================================
184 const Handle(TColStd_HArray1OfByte)& TDataStd_BooleanArray::InternalArray () const
189 //=======================================================================
190 //function : SetInternalArray
192 //=======================================================================
193 void TDataStd_BooleanArray::SetInternalArray (const Handle(TColStd_HArray1OfByte)& values)
198 //=======================================================================
201 //=======================================================================
202 const Standard_GUID& TDataStd_BooleanArray::ID () const
207 //=======================================================================
208 //function : NewEmpty
210 //=======================================================================
211 Handle(TDF_Attribute) TDataStd_BooleanArray::NewEmpty () const
213 return new TDataStd_BooleanArray();
216 //=======================================================================
219 //=======================================================================
220 void TDataStd_BooleanArray::Restore(const Handle(TDF_Attribute)& With)
222 Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(With);
223 if (!anArray->myValues.IsNull())
225 myLower = anArray->Lower();
226 myUpper = anArray->Upper();
227 Standard_Integer byte_upper = Length() >> 3;
228 myValues = new TColStd_HArray1OfByte(0, byte_upper, 0/*initialize to FALSE*/);
229 const TColStd_Array1OfByte& with_array = anArray->myValues->Array1();
230 for (Standard_Integer i = 0; i <= byte_upper; i++)
232 myValues->SetValue(i, with_array.Value(i));
241 //=======================================================================
244 //=======================================================================
245 void TDataStd_BooleanArray::Paste (const Handle(TDF_Attribute)& Into,
246 const Handle(TDF_RelocationTable)& ) const
248 if (!myValues.IsNull())
250 Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(Into);
251 if (!anArray.IsNull())
253 anArray->Init(myLower, myUpper);
254 for (Standard_Integer i = myLower; i <= myUpper; i++)
256 anArray->SetValue(i, Value(i));
262 //=======================================================================
265 //=======================================================================
266 Standard_OStream& TDataStd_BooleanArray::Dump (Standard_OStream& anOS) const
268 anOS << "BooleanArray";