-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_8_set.e
219 lines (190 loc) · 3.76 KB
/
character_8_set.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
indexing
description: "8-bit character sets implemented using boolean arrays."
author: "Eser Aygün"
date: "$Date$"
revision: "$Revision$"
class
CHARACTER_8_SET
inherit
SUBSET [CHARACTER_8]
create
make
feature {NONE} -- Initialization
make is
-- Initialize set.
do
create bits.make ({CHARACTER_8}.min_value, {CHARACTER_8}.max_value)
end
feature -- Measurement
count: INTEGER is
-- Number of items.
local
i: INTEGER
do
from
i := {CHARACTER_8}.min_value
until
i > {CHARACTER_8}.max_value
loop
if bits [i] then
Result := Result + 1
end
i := i + 1
end
end
feature -- Status report
has (v: CHARACTER_8): BOOLEAN is
-- Does structure include `v'?
do
Result := bits [v.code]
end
extendible: BOOLEAN is
-- May new items be added?
do
Result := True
end
prunable: BOOLEAN is
-- May items be removed?
do
Result := True
end
is_empty: BOOLEAN is
-- Is there no element?
do
Result := count = 0
end
is_subset (other: SUBSET [CHARACTER_8]): BOOLEAN is
-- Is current set a subset of `other'?
local
i: INTEGER
do
Result := True
from
i := {CHARACTER_8}.min_value
until
(i > {CHARACTER_8}.max_value) or not Result
loop
if has (i.to_character_8) and not other.has (i.to_character_8) then
Result := False
else
i := i + 1
end
end
end
feature -- Element change
extend, put (v: CHARACTER_8) is
-- Ensure that set includes `v'.
do
bits [v.code] := True
end
merge (other: CONTAINER [CHARACTER_8]) is
-- Add all items of `other'.
local
l: LINEAR [CHARACTER_8]
do
l := other.linear_representation
from
l.start
until
l.off
loop
put (l.item)
l.forth
end
end
feature -- Removal
prune (v: CHARACTER_8) is
-- Remove `v' if present.
do
bits [v.code] := False
end
wipe_out is
-- Remove all items.
local
i: INTEGER
do
from
i := {CHARACTER_8}.min_value
until
i > {CHARACTER_8}.max_value
loop
bits [i] := False
i := i + 1
end
end
feature -- Conversion
linear_representation: LINEAR [CHARACTER_8] is
-- Representation as a linear structure.
local
i: INTEGER
r: ARRAYED_LIST [CHARACTER_8]
do
create r.make (1)
from
i := {CHARACTER_8}.min_value
until
i > {CHARACTER_8}.max_value
loop
r.force (i.to_character_8)
i := i + 1
end
Result := r
end
feature -- Duplication
duplicate (n: INTEGER): like Current is
-- New structure containing min (`n', `count')
-- items from current structure.
local
i, m: INTEGER
do
create Result.make
m := 0
from
i := {CHARACTER_8}.min_value
until
(i > {CHARACTER_8}.max_value) or (m = n)
loop
if has (i.to_character_8) then
Result.put (i.to_character_8)
m := m + 1
end
i := i + 1
end
end
feature -- Basic operations
intersect (other: SUBSET [CHARACTER_8]) is
-- Remove all items not in `other'.
local
i: INTEGER
do
from
i := {CHARACTER_8}.min_value
until
i > {CHARACTER_8}.max_value
loop
if not other.has (i.to_character_8) then
bits [i] := False
end
i := i + 1
end
end
subtract (other: SUBSET [CHARACTER_8]) is
-- Remove all items also in `other'.
local
i: INTEGER
do
from
i := {CHARACTER_8}.min_value
until
i > {CHARACTER_8}.max_value
loop
if other.has (i.to_character_8) then
bits [i] := False
end
i := i + 1
end
end
feature {NONE} -- Implementation
bits: ARRAY [BOOLEAN]
-- Boolean array storing existence information.
end