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