summaryrefslogtreecommitdiff
path: root/static/v10/man3/map.3
blob: 21c3caf570eac9f5b35c20c2d79ce589ca9d92ca (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
.TH MAP 3+
.CT 2 datatype
.SH NAME
Map \- associative array classes
.SH SYNOPSIS
.nf
.B "#include <Map.h>"
.PP
.ds 1s \*S
.ds S \fIS\fP
.ds T \fIT\fP
.ft L
Mapdeclare(\*S,\*T)
Mapimplement(\*S,\*T)
.PP
.ft L
struct Map(\*S,\*T) {
	Map(\*S,\*T)();
	Map(\*S,\*T)(const \*T&);
	Map(\*S,\*T)(const Map(\*S,\*T)&);
	~Map(\*S,\*T);
	Map(\*S,\*T)& operator= (const Map(\*S,\*T)&);
	\*T& operator[] (int);
	int size();
	Mapiter(\*S,\*T) element(const \*S&);
	Mapiter(\*S,\*T) first();
	Mapiter(\*S,\*T) last();
};
.PP
.ft L
struct Mapiter(\*S,\*T) {
	Mapiter(\*S,\*T) (const Map(\*S,\*T)&);
	~Mapiter(\*S,\*T);
	operator int();
	\*S key();
	\*T value();
	Mapiter(\*S,\*T)& operator++ (Mapiter(\*S,\*T)&);
	Mapiter(\*S,\*T)& operator-- (Mapiter(\*S,\*T)&);
};
.ft R
.fi
.SH DESCRIPTION
A
.I map
is a collection of
.I elements,
each of which contains a
.I key
part of type
.I S
and a
.I value
part of type
.I T,
where
.I S
and
.I T
are type names.
Both
.I S
and
.I T
must have value semantics:
assignment or initialization have the effect of copying.
(It is unlikely for
.I S
and
.I T
to be pointers.)
.PP
Map elements are ordered by key: type
.I S
must have a transitive boolean
.BR operator< .
.PP
The macro call
.L Mapdeclare(\*S,\*T)
declares the classes
.B Map(\*S,\*T)
and
.BR Mapiter(\*S,\*T) .
It must appear once in every source file that uses either.
The macro call
.B Mapimplement(\*S,\*T)
defines the functions that implement the map classes.
It must appear exactly once in the entire program.
.SS Map constructors
.nr xx \w'\fLMap(\*S,\*T)(m)\ \fP'
.TP \n(xxu
.B Map(\*S,\*T)()
An empty map.
The value part of future elements
is the value of an otherwise uninitialized
static object of type
.I T .
.TP
.BI Map(\*S,\*T)( x )
An empty map whose future elements have
default value
.I x .
.TP
.BI Map(\*S,\*T)( m )
A copy of map
.I m
obtained by copying the elements and default value of
.I m.
.SS Map operators
.TP  \n(xxu
.IB n " = " m
All the elements of map
.I n
are deleted and and
copies of the elements of
.I m 
are added.
The default value of
.I n
does not change.
Running time is
.IR O (log(| m |)
+
.RI log(| n |)),
where
.RI | m |
means
.IB m .size() .
.TP
.IB m [ k ]
A reference
to the value part of the element of map
.I m
with key
.BR k .
If the element does not exist, it is created.
Running time is
.IR O (log(| m |)) .
.SS Other Map functions
.TP  \n(xxu
.IB m .size()
The number of elements in
.LR m .
Running time is
.IR O (1).
.TP
.IB m .element( k )
A map iterator 
referring to the element of
.I m
with key
.I k
if such an element exists.
Otherwise the result is vacuous.
Running time is
.IR O (log(| m |)) .
.TP
.IB m .first()
.br
.ns
.TP
.IB m .last()
A map iterator
referring to the element of
.I m
with the smallest (or largest) key.
If
.L m
has no elements, the result is vacuous.
Running time is
.IR O (log(| m |)) .
.SS "Map iterators"
For every class
.B Map(\*S,\*T)
there is a class
.BR Mapiter(\*S,\*T) .
A map iterator identifies a map object and possibly
an element in that map.
An iterator that does not identify an element is
.IR vacuous .
.SS Mapiter constructors
.TP  \n(xxu
.BI Mapiter(\*S,\*T)( m )
A vacuous iterator referring to map
.I m.
Running time is
.IR O (1).
.SS Mapiter operators
.TP  \n(xxu
.IB i " = " j
Make iterator
.I i
refer (for now) to the same map as does
.I j.
.TP
.BI (int) i
Zero if iterator
.I i
is vacuous, otherwise nonzero.
.TP
.BI ++ i
.br
.ns
.TP
.BI -- i
If iterator
.I i
is vacuous, make it refer to the map element with
the smallest (or largest) key
Otherwise, make it refer to the map element with the
next key greater (or less) than the key of the
current element.
If no such element exists,
.I i
becomes vacuous.
The running time of a single increment
operation for map
.I m
is
.IR O (log(| m |)).
However an iterator 
takes only time
.IR O (| m |)
to sequence through the whole map.
.SS Other mapiter functions
.TP  \n(xxu
.IB i .key()
.br
.ns
.TP
.IB i .value()
The key (or value) part of the element referred to by
.I i .
If
.I i
is vacuous, return
the value of an otherwise uninitialized static
object of appropriate type.
Running time is
.IR O (1).
.SH EXAMPLES
.EX
struct city { char name[100]; };
typedef int population;
int operator< (const city&, const city&);
.EE
.PP
.B Mapdeclare(name,population)
.PP
.B Map(name,population) gazetteer;
.PP
.B "// Print big cities; set populations of others to zero.
.PP
.EX
	for(Mapiter(name,population) i = gazetteer.first(); i; i++)
		if(i.value() > 1000000)
			printf("%s\en", i.key().name);
		else
			gazetteer[i.key()] = 0;
.EE
.SH BUGS
A `type name'
.B Map(\*S,\*T)
or
.BR Mapiter(\*S,\*T) 
that contains spaces will be mangled by
.IR cpp (8).
.br
There is no way to delete a single element.
.br
Ambiguities can occur if the type name
.I S
contains an underscore.
.br
No precautions are taken against running out of memory.