Commit | Line | Data |
---|---|---|
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 | // |
d5f74e42 | 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 | |
973c2be1 | 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 | |
42cf5bc1 | 14 | |
15 | #include <Interface_BitMap.hxx> | |
7fd59977 | 16 | #include <Standard_NotImplemented.hxx> |
42cf5bc1 | 17 | #include <TCollection_AsciiString.hxx> |
7fd59977 | 18 | |
bc650d41 G |
19 | Interface_BitMap::Interface_BitMap() |
20 | { | |
21 | Initialize(0); | |
22 | } | |
23 | ||
24 | ||
25 | Interface_BitMap::Interface_BitMap | |
26 | (const Standard_Integer nbitems, const Standard_Integer resflags) | |
27 | { | |
28 | Initialize(nbitems,resflags); | |
29 | } | |
7fd59977 | 30 | |
bc650d41 | 31 | void Interface_BitMap::Initialize(const Standard_Integer nbitems, const Standard_Integer resflags) |
7fd59977 | 32 | { |
33 | thenbitems = nbitems; | |
34 | thenbwords = nbitems/32 + 1; | |
35 | thenbflags = 0; | |
bc650d41 G |
36 | if(nbitems) |
37 | { | |
38 | theflags = new TColStd_HArray1OfInteger (0,thenbwords*(resflags+1)); | |
39 | theflags->Init(0); | |
40 | } | |
41 | } | |
42 | ||
43 | Interface_BitMap::Interface_BitMap | |
44 | (const Interface_BitMap& other, const Standard_Boolean copied) | |
45 | { | |
46 | ||
47 | Initialize(other,copied); | |
7fd59977 | 48 | } |
49 | ||
bc650d41 G |
50 | void Interface_BitMap::Initialize(const Interface_BitMap& other, |
51 | const Standard_Boolean copied) | |
7fd59977 | 52 | { |
53 | other.Internals (thenbitems,thenbwords,thenbflags,theflags,thenames); | |
54 | if (!copied) return; | |
55 | Standard_Integer nb = theflags->Upper (); | |
56 | Handle(TColStd_HArray1OfInteger) flags = new TColStd_HArray1OfInteger(0,nb); | |
57 | Standard_Integer i; // svv Jan11 2000 : porting on DEC | |
58 | for (i = 0; i <= nb; i ++) | |
59 | flags->SetValue (i,theflags->Value(i)); | |
60 | theflags = flags; | |
61 | if (thenames.IsNull()) return; | |
62 | nb = thenames->Length(); | |
63 | Handle(TColStd_HSequenceOfAsciiString) names = new TColStd_HSequenceOfAsciiString(); | |
64 | for (i = 1; i <= nb; i ++) | |
65 | names->Append ( TCollection_AsciiString(thenames->Value(i)) ); | |
66 | thenames = names; | |
67 | } | |
68 | ||
bc650d41 G |
69 | void Interface_BitMap::Internals |
70 | (Standard_Integer& nbitems, Standard_Integer& nbwords, | |
71 | Standard_Integer& nbflags, | |
72 | Handle(TColStd_HArray1OfInteger)& flags, | |
73 | Handle(TColStd_HSequenceOfAsciiString)& names) const | |
7fd59977 | 74 | { |
75 | nbitems = thenbitems; nbwords = thenbwords; nbflags = thenbflags; | |
76 | flags = theflags; names = thenames; | |
77 | } | |
78 | ||
79 | ||
bc650d41 | 80 | void Interface_BitMap::Reservate (const Standard_Integer moreflags) |
7fd59977 | 81 | { |
82 | Standard_Integer nb = theflags->Upper (); | |
83 | Standard_Integer nbflags = nb / thenbwords - 1; // flag 0 non compte ... | |
84 | if (nbflags >= thenbflags + moreflags) return; | |
85 | Standard_Integer nbw = thenbwords * (thenbflags+moreflags+2); | |
86 | Handle(TColStd_HArray1OfInteger) flags = new TColStd_HArray1OfInteger(0,nbw); | |
87 | Standard_Integer i; // svv Jan11 2000 : porting on DEC | |
88 | for (i = 0; i <= nb; i ++) | |
89 | flags->SetValue (i,theflags->Value(i)); | |
90 | for (i = nb+1; i <= nbw; i ++) flags->SetValue (i,0); | |
91 | theflags = flags; | |
92 | } | |
93 | ||
94 | ||
bc650d41 | 95 | void Interface_BitMap::SetLength (const Standard_Integer nbitems) |
7fd59977 | 96 | { |
97 | Standard_Integer nbw = nbitems/32 + 1; | |
98 | if (nbw == thenbwords) return; | |
99 | Handle(TColStd_HArray1OfInteger) flags = | |
100 | new TColStd_HArray1OfInteger(0,nbw*(thenbflags+1)); | |
101 | if (nbw > thenbwords) flags->Init(0); | |
102 | Standard_Integer nbmots = (nbw > thenbwords ? thenbwords : nbw); | |
103 | Standard_Integer i0 = 0, i1 = 0; | |
104 | for (Standard_Integer nf = 0; nf <= thenbflags; nf ++) { | |
105 | for (Standard_Integer i = 0; i < nbmots; i ++) | |
106 | flags->SetValue (i1+i,theflags->Value(i0+i)); | |
107 | i0 += thenbwords; i1 += nbw; | |
108 | } | |
109 | theflags = flags; | |
110 | thenbitems = nbitems; | |
111 | thenbwords = nbw; | |
112 | } | |
113 | ||
114 | ||
bc650d41 | 115 | Standard_Integer Interface_BitMap::AddFlag (const Standard_CString name) |
7fd59977 | 116 | { |
117 | Reservate(1); | |
118 | Standard_Integer deja = 0; | |
119 | if (thenames.IsNull()) thenames = new TColStd_HSequenceOfAsciiString(); | |
120 | else { | |
121 | Standard_Integer i, nb = thenames->Length(); | |
122 | for (i = 1; i <= nb; i ++) { | |
123 | if (thenames->Value(i).IsEqual(".")) | |
bc650d41 | 124 | { thenames->ChangeValue(i).AssignCat(name); deja = i; } |
7fd59977 | 125 | } |
126 | } | |
127 | if (!deja) thenames->Append (TCollection_AsciiString(name)); | |
128 | thenbflags ++; | |
129 | return (deja ? deja : thenbflags); | |
130 | } | |
131 | ||
bc650d41 G |
132 | Standard_Integer Interface_BitMap::AddSomeFlags |
133 | (const Standard_Integer more) | |
7fd59977 | 134 | { |
135 | Reservate(more); | |
136 | if (thenames.IsNull()) thenames = new TColStd_HSequenceOfAsciiString(); | |
137 | for (Standard_Integer i = 1; i <= more; i ++) | |
138 | thenames->Append (TCollection_AsciiString("")); | |
139 | thenbflags += more; | |
140 | return thenbflags; | |
141 | } | |
142 | ||
bc650d41 G |
143 | Standard_Boolean Interface_BitMap::RemoveFlag |
144 | (const Standard_Integer num) | |
7fd59977 | 145 | { |
146 | if (num < 1 || num > thenames->Length()) return Standard_False; | |
147 | if (num == thenames->Length()) thenames->Remove (thenames->Length()); | |
148 | else thenames->ChangeValue(num).AssignCat("."); | |
149 | thenbflags --; | |
150 | return Standard_True; | |
151 | } | |
152 | ||
bc650d41 G |
153 | Standard_Boolean Interface_BitMap::SetFlagName |
154 | (const Standard_Integer num, const Standard_CString name) | |
7fd59977 | 155 | { |
156 | if (num < 1 || num > thenames->Length()) return Standard_False; | |
157 | Standard_Integer deja = (name[0] == '\0' ? 0 : FlagNumber (name) ); | |
158 | if (deja != 0 && deja != num) return Standard_False; | |
159 | thenames->ChangeValue(num).AssignCat(name); | |
160 | return Standard_True; | |
161 | } | |
162 | ||
bc650d41 G |
163 | Standard_Integer Interface_BitMap::NbFlags () const |
164 | { return thenbflags; } | |
7fd59977 | 165 | |
bc650d41 G |
166 | Standard_Integer Interface_BitMap::Length () const |
167 | { return thenbitems; } | |
7fd59977 | 168 | |
bc650d41 G |
169 | Standard_CString Interface_BitMap::FlagName |
170 | (const Standard_Integer num) const | |
7fd59977 | 171 | { |
172 | if (theflags.IsNull()) return ""; | |
173 | if (num < 1 || num > thenames->Length()) return ""; | |
174 | return thenames->Value(num).ToCString(); | |
175 | } | |
176 | ||
bc650d41 G |
177 | Standard_Integer Interface_BitMap::FlagNumber |
178 | (const Standard_CString name) const | |
7fd59977 | 179 | { |
180 | if (name[0] == '\0') return 0; | |
181 | if (thenames.IsNull()) return 0; | |
182 | Standard_Integer i, nb = thenames->Length(); | |
183 | for (i = 1; i <= nb; i ++) | |
184 | if (thenames->Value(i).IsEqual(name)) return i; | |
185 | return 0; | |
186 | } | |
187 | ||
188 | ||
189 | // Les valeurs ... | |
190 | ||
bc650d41 G |
191 | Standard_Boolean Interface_BitMap::Value |
192 | (const Standard_Integer item, const Standard_Integer flag) const | |
7fd59977 | 193 | { |
194 | Standard_Integer numw = (thenbwords * flag) + (item >> 5); | |
195 | const Standard_Integer& val = theflags->Value (numw); | |
196 | if (val == 0 ) return Standard_False; | |
197 | if (val == ~(0)) return Standard_True; | |
198 | Standard_Integer numb = item & 31; | |
199 | return ( ((1 << numb) & val) != 0); | |
200 | } | |
201 | ||
bc650d41 G |
202 | void Interface_BitMap::SetValue |
203 | (const Standard_Integer item, const Standard_Boolean val, | |
204 | const Standard_Integer flag) const | |
7fd59977 | 205 | { |
206 | if (val) SetTrue (item,flag); | |
207 | else SetFalse (item,flag); | |
208 | } | |
209 | ||
bc650d41 G |
210 | void Interface_BitMap::SetTrue |
211 | (const Standard_Integer item, const Standard_Integer flag) const | |
7fd59977 | 212 | { |
213 | Standard_Integer numw = (thenbwords * flag) + (item >> 5); | |
214 | Standard_Integer numb = item & 31; | |
215 | theflags->ChangeValue (numw) |= (1 << numb); | |
216 | } | |
217 | ||
bc650d41 G |
218 | void Interface_BitMap::SetFalse |
219 | (const Standard_Integer item, const Standard_Integer flag) const | |
7fd59977 | 220 | { |
221 | Standard_Integer numw = (thenbwords * flag) + (item >> 5); | |
222 | Standard_Integer& val = theflags->ChangeValue (numw); | |
223 | if (val == 0) return; | |
224 | Standard_Integer numb = item & 31; | |
225 | theflags->ChangeValue (numw) &= ~(1 << numb); | |
226 | } | |
227 | ||
bc650d41 G |
228 | Standard_Boolean Interface_BitMap::CTrue |
229 | (const Standard_Integer item, const Standard_Integer flag) const | |
7fd59977 | 230 | { |
231 | Standard_Integer numw = (thenbwords * flag) + (item >> 5); | |
232 | Standard_Integer numb = item & 31; | |
233 | Standard_Integer& val = theflags->ChangeValue (numw); | |
234 | Standard_Integer res, mot = (1 << numb); | |
235 | ||
236 | if (val == 0) { val = mot; return Standard_False; } | |
237 | else { res = val & mot; val |= mot; } | |
238 | return (res != 0); | |
239 | } | |
240 | ||
bc650d41 G |
241 | Standard_Boolean Interface_BitMap::CFalse |
242 | (const Standard_Integer item, const Standard_Integer flag) const | |
7fd59977 | 243 | { |
244 | Standard_Integer numw = (thenbwords * flag) + (item >> 5); | |
245 | Standard_Integer numb = item & 31; | |
246 | Standard_Integer& val = theflags->ChangeValue (numw); | |
247 | Standard_Integer res, mot = ~(1 << numb); | |
248 | ||
249 | if (val == ~(0)) { val = mot; return Standard_False; } | |
250 | else { res = val | mot; val &= mot; } | |
251 | return (res != 0); | |
252 | } | |
253 | ||
254 | ||
bc650d41 G |
255 | void Interface_BitMap::Init |
256 | (const Standard_Boolean val, const Standard_Integer flag) const | |
7fd59977 | 257 | { |
258 | Standard_Integer i, ii = thenbwords, i1 = thenbwords *flag; | |
259 | if (flag < 0) { i1 = 0; ii = thenbwords*(thenbflags+1); } | |
260 | if (val) for (i = 0; i < ii; i ++) theflags->SetValue (i1+i,~(0)); | |
261 | else for (i = 0; i < ii; i ++) theflags->SetValue (i1+i, 0 ); | |
262 | } | |
bc650d41 G |
263 | |
264 | void Interface_BitMap::Clear() | |
265 | { | |
266 | theflags.Nullify(); | |
267 | Initialize(0); | |
973c2be1 | 268 | } |