/[eiffelstudio]/branches/CAT_mono/Src/library/base_extension/kernel/packed_booleans.e
ViewVC logotype

Contents of /branches/CAT_mono/Src/library/base_extension/kernel/packed_booleans.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69629 - (show annotations)
Mon Jul 23 21:36:18 2007 UTC (12 years, 6 months ago) by manus
File size: 3630 byte(s)
Creation of CAT_mono branch to implement variant/invariant proposal
1 indexing
2 description: "An array of BOOLEAN but in a packed forms, saving 7 bits for%
3 %every boolean contained by Current."
4 legal: "See notice at end of class."
5 status: "See notice at end of class."
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 PACKED_BOOLEANS
11
12 create
13 make
14
15 feature -- Initialization
16
17 make (n: INTEGER) is
18 require
19 non_negative_argument: n >= 0
20 do
21 create area.make (1 + (n // Integer_size))
22 ensure
23 allocated: area /= Void
24 enough_entries: count >= n
25 end
26
27 feature -- Access
28
29 item (i: INTEGER): BOOLEAN is
30 -- Access `i-th' boolean in Current.
31 require
32 valid_index: valid_index (i)
33 do
34 Result := area.item(i // Integer_size).bit_test (i \\ Integer_size)
35 end
36
37 feature -- Status Report
38
39 valid_index (i: INTEGER): BOOLEAN is
40 -- Is `i' within bounds?
41 do
42 Result := (lower <= i) and (i <= upper)
43 end
44
45 feature -- Measurement
46
47 lower: INTEGER is 0
48 -- Minimum index
49
50 upper: INTEGER is
51 -- Maximum index
52 do
53 Result := count - 1
54 end
55
56 count, capacity: INTEGER is
57 -- Number of available indices
58 do
59 Result := area.count * Integer_size
60 ensure then
61 consistent_with_bounds: Result = upper - lower + 1
62 end
63
64 feature -- Element change
65
66 put (v: BOOLEAN; i: INTEGER) is
67 -- Insert `v' at `i-th' bit of Current.
68 require
69 valid_index: valid_index (i)
70 local
71 index: INTEGER
72 do
73 index := i // Integer_size
74 if v then
75 area.put (area.item (index) | ((1).to_integer_32 |<< (i \\ Integer_size)), index)
76 else
77 area.put (area.item (index) & ((1).to_integer_32 |<< (i \\ Integer_size)).bit_not, index)
78 end
79 ensure
80 inserted: v = item (i)
81 end
82
83 force (v: BOOLEAN; i: INTEGER) is
84 require
85 min_i: i >= 0
86 local
87 old_count: INTEGER
88 do
89 old_count := count
90 if (i >= old_count) then
91 resize (i.max (old_count + old_count * 30 // 100))
92 end
93 put (v, i)
94 ensure
95 inserted: v = item (i)
96 end
97
98 include (other: PACKED_BOOLEANS) is
99 -- Include all items set to `True' from `other' in current.
100 -- (I.e. save a result of "Current or other" into current.)
101 require
102 other_not_void: other /= Void
103 other_small_enough: other.count <= count
104 local
105 i: INTEGER
106 current_area: like area
107 other_area: like area
108 do
109 current_area := area
110 other_area := other.area
111 from
112 i := other_area.count
113 until
114 i <= 0
115 loop
116 i := i - 1
117 current_area.put (current_area.item (i) | other_area.item (i), i)
118 end
119 end
120
121 feature -- Resizing
122
123 resize (n: INTEGER) is
124 -- Rearrange array so that it can accommodate `n' items.
125 -- Do not lose any previously entered item.
126 local
127 old_count, new_count: INTEGER
128 do
129 old_count := area.count
130 new_count := 1 + (n // Integer_size)
131 if new_count > old_count then
132 area := area.resized_area (new_count)
133 end
134 ensure
135 consistent_count: count >= n
136 end
137
138 feature -- Removal
139
140 clear_all is
141 -- Reset all items to default values.
142 do
143 area.clear_all
144 ensure
145 default_items: area.all_default (area.count - 1)
146 end
147
148 feature -- Constants
149
150 Integer_size: INTEGER is 32
151 -- Size of INTEGER in bits.
152
153 feature {PACKED_BOOLEANS} -- Implementation
154
155 area: SPECIAL [INTEGER]
156 -- Storage for booleans.
157
158 invariant
159 area_not_void: area /= Void
160 area_not_empty: area.count > 0
161
162 indexing
163 copyright: "Copyright (c) 1984-2006, Eiffel Software and others"
164 license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
165 source: "[
166 Eiffel Software
167 356 Storke Road, Goleta, CA 93117 USA
168 Telephone 805-685-1006, Fax 805-685-6869
169 Website http://www.eiffel.com
170 Customer support http://support.eiffel.com
171 ]"
172 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23