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