author | Stefan Vogel <sv@exept.de> |
Thu, 08 Nov 2007 15:03:20 +0100 | |
changeset 1909 | c76efba26558 |
parent 1176 | 8224efd15c5a |
child 2076 | 57efa314833a |
permissions | -rw-r--r-- |
377 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
||
12 |
This is a demo example: |
|
13 |
||
14 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND |
|
15 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
16 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
17 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE |
|
18 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
19 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
20 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
21 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
22 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
23 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
24 |
SUCH DAMAGE. |
|
25 |
" |
|
26 |
||
905 | 27 |
"{ Package: 'stx:libbasic2' }" |
28 |
||
377 | 29 |
ArrayedCollection variableByteSubclass:#BooleanArray |
30 |
instanceVariableNames:'tally' |
|
31 |
classVariableNames:'' |
|
32 |
poolDictionaries:'' |
|
33 |
category:'Collections-Arrayed' |
|
34 |
! |
|
35 |
||
36 |
!BooleanArray class methodsFor:'documentation'! |
|
37 |
||
38 |
copyright |
|
39 |
" |
|
40 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
41 |
All Rights Reserved |
|
42 |
||
43 |
This software is furnished under a license and may be used |
|
44 |
only in accordance with the terms of that license and with the |
|
45 |
inclusion of the above copyright notice. This software may not |
|
46 |
be provided or otherwise made available to, or used by, any |
|
47 |
other person. No title to or ownership of the software is |
|
48 |
hereby transferred. |
|
49 |
||
50 |
This is a demo example: |
|
51 |
||
52 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND |
|
53 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
54 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
55 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE |
|
56 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
57 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
58 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
59 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
60 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
61 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
62 |
SUCH DAMAGE. |
|
63 |
" |
|
64 |
! |
|
65 |
||
66 |
documentation |
|
67 |
" |
|
68 |
This is a simple demo class only; currently not used in the system. |
|
69 |
||
70 |
example for bulk boolean data (requires only 1/32th the memory |
|
71 |
compared to an array of booleans). |
|
72 |
||
73 |
This one stores 8 booleans per byte. Since instances store bits in multiples |
|
74 |
of 8, we have to keep the real size of the collection in an extra instance |
|
75 |
variable (tally). |
|
552 | 76 |
It may be useful if huge boolean arrays are to be used. |
377 | 77 |
|
750 | 78 |
Bits 1 to 8 of the BooleanArray are stored in bits 8 to 1 of the |
79 |
corresponding byte, to allow easy mapping to ASN.1 BIT STRING encoding |
|
80 |
in the BER. |
|
81 |
||
736 | 82 |
[memory requirements:] |
83 |
OBJ-HEADER + ((size + 7) // 8) |
|
84 |
||
377 | 85 |
[see also:] |
86 |
ByteArray WordArray Array |
|
87 |
||
88 |
[author:] |
|
89 |
Claus Gittinger |
|
90 |
" |
|
91 |
! |
|
92 |
||
93 |
examples |
|
94 |
" |
|
95 |
[exBegin] |
|
96 |
(BooleanArray new:7) inspect |
|
97 |
[exEnd] |
|
98 |
[exBegin] |
|
99 |
(BooleanArray new:7) basicInspect |
|
100 |
[exEnd] |
|
101 |
[exBegin] |
|
102 |
|flags| |
|
103 |
||
104 |
flags := BooleanArray new:1000000. |
|
105 |
(flags at:9999) printNL. |
|
106 |
flags at:9999 put:true. |
|
107 |
(flags at:9999) printNL. |
|
108 |
[exEnd] |
|
109 |
" |
|
110 |
! ! |
|
111 |
||
112 |
!BooleanArray class methodsFor:'instance creation'! |
|
113 |
||
114 |
new:size |
|
115 |
"return a new instance, capable of holding size booleans" |
|
116 |
||
117 |
|nBytes| |
|
118 |
||
119 |
nBytes := size // 8. |
|
120 |
(size \\ 8) ~~ 0 ifTrue:[nBytes := nBytes + 1]. |
|
121 |
^ (super new:nBytes) setTally:size |
|
907 | 122 |
|
123 |
" |
|
124 |
BooleanArray new:10 |
|
125 |
" |
|
377 | 126 |
! ! |
127 |
||
128 |
!BooleanArray methodsFor:'accessing'! |
|
129 |
||
130 |
at:index |
|
131 |
"retrieve the boolean at index" |
|
132 |
||
846 | 133 |
|byte mask i0| |
377 | 134 |
|
846 | 135 |
(index between:1 and:tally) ifFalse:[ |
136 |
^ self subscriptBoundsError:index |
|
377 | 137 |
]. |
846 | 138 |
i0 := index - 1. |
139 |
byte := super basicAt:(i0 // 8)+1. |
|
847 | 140 |
mask := 1 bitShift:(7 - (i0 \\ 8)). |
377 | 141 |
^ (byte bitAnd:mask) ~~ 0 |
552 | 142 |
|
143 |
" |
|
144 |
(BooleanArray new:1000) at:555 |
|
145 |
" |
|
146 |
||
147 |
" |
|
148 |
|b| |
|
149 |
||
150 |
b := BooleanArray new:1000. |
|
151 |
b at:555 put:true. |
|
152 |
b at:555 |
|
153 |
" |
|
154 |
||
750 | 155 |
"Modified: / 31.7.1997 / 18:37:25 / cg" |
156 |
"Modified: / 23.5.1999 / 20:02:57 / stefan" |
|
377 | 157 |
! |
158 |
||
159 |
at:index put:aBoolean |
|
160 |
"store the argument, aBoolean at index; return aBoolean." |
|
161 |
||
846 | 162 |
|byte mask idx i0| |
377 | 163 |
|
846 | 164 |
(index between:1 and:tally) ifFalse:[ |
165 |
^ self subscriptBoundsError:index |
|
377 | 166 |
]. |
167 |
||
846 | 168 |
i0 := index - 1. |
1176
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
169 |
idx := (i0 // 8) + 1. |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
170 |
byte := super basicAt:idx. |
847 | 171 |
mask := 1 bitShift:(7 - (i0 \\ 8)). |
846 | 172 |
aBoolean == true ifTrue:[ |
173 |
byte := byte bitOr:mask |
|
377 | 174 |
] ifFalse:[ |
846 | 175 |
aBoolean == false ifTrue:[ |
176 |
byte := byte bitAnd:(mask bitInvert) |
|
177 |
] ifFalse:[ |
|
178 |
"/ not true or false |
|
1176
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
179 |
^ self elementBoundsError:aBoolean |
846 | 180 |
] |
377 | 181 |
]. |
1176
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
182 |
super basicAt:idx put:byte. |
377 | 183 |
^ aBoolean. |
552 | 184 |
|
185 |
" |
|
186 |
|b| |
|
187 |
||
188 |
b := BooleanArray new:1000. |
|
189 |
b at:555 put:true. |
|
190 |
b at:555 |
|
191 |
" |
|
192 |
||
750 | 193 |
"Modified: / 31.7.1997 / 18:37:35 / cg" |
194 |
"Modified: / 23.5.1999 / 20:02:42 / stefan" |
|
905 | 195 |
! ! |
196 |
||
197 |
!BooleanArray methodsFor:'filling & replacing'! |
|
198 |
||
846 | 199 |
atAllPut:aBoolean |
200 |
|v| |
|
201 |
||
1176
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
202 |
aBoolean == true ifTrue:[ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
203 |
v := 255 |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
204 |
] ifFalse:[ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
205 |
aBoolean == false ifTrue:[ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
206 |
v := 0 |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
207 |
] ifFalse:[ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
208 |
"/ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
209 |
"/ booleanArrays can only hold true and false |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
210 |
"/ |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
211 |
^ self elementBoundsError:aBoolean |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
212 |
] |
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
213 |
]. |
846 | 214 |
1 to:self basicSize do:[:i | |
215 |
super basicAt:i put:v |
|
216 |
]. |
|
377 | 217 |
! ! |
218 |
||
219 |
!BooleanArray methodsFor:'private'! |
|
220 |
||
221 |
setTally:size |
|
222 |
tally := size |
|
223 |
! ! |
|
224 |
||
906 | 225 |
!BooleanArray methodsFor:'queries'! |
226 |
||
907 | 227 |
defaultElement |
228 |
^ false |
|
229 |
! |
|
230 |
||
906 | 231 |
size |
232 |
"return the size of the receiver" |
|
233 |
||
234 |
^ tally |
|
235 |
! ! |
|
236 |
||
377 | 237 |
!BooleanArray class methodsFor:'documentation'! |
238 |
||
239 |
version |
|
1176
8224efd15c5a
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
907
diff
changeset
|
240 |
^ '$Header: /cvs/stx/stx/libbasic2/BooleanArray.st,v 1.11 2003-04-22 09:38:23 cg Exp $' |
377 | 241 |
! ! |