blob: a8351c17ec640df7e273f598a53ec5f939e0e504 (
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
|
.TH BLOCK 3+
.CT 2 datatype
.SH NAME
block \- adjustable arrays
.SH SYNOPSIS
.nf
.B "#include <Block.h>"
.PP
.ds T \fIT\fP
.ft L
Blockdeclare(\*T)
Blockimplement(\*T)
.PP
.ft L
struct Block(\*T) {
Block(\*T)(unsigned = 0);
Block(\*T)(const Block(\*T&);
~Block(\*T);
Block(\*T)& operator= (const Block(\*T)&);
\*T& operator[] (int);
operator \*T* ();
unsigned size();
unsigned size(unsigned);
\*T* end();
void swap(const Block(\*T)&);
}
.ft R
.fi
.SH DESCRIPTION
A
.BI Block( T )
is an array of zero or more
.IR elements
of type
.I T,
where
.I T
is a type name.
.I T
must have assignment
.RB ( operator= )
and initialization
.RB ( \*T(\*T&) )
operations.
.PP
The macro call
.BI Blockdeclare( T )
declares the class
.BI Block( T ) .
It must appear once in every source file that uses type
.BI Block( T ) .
The macro call
.BI Blockimplement( T )
defines the functions that implement the block class.
It must appear exactly once in the entire program.
.PP
New elements
are initialized to the value of an otherwise
uninitialized static object of type
.I T.
.SS Constructors
.nr xx \w'\fLBlock(\fIT\fL)(\fIb\fL)'
.TP \n(xxu
.BI Block( T )
An empty block.
.TP
.BI Block( T )( n )
A block of
.I n
elements.
.TP
.BI Block( T )( b )
A new block
whose elements are copies of the elements of
.BR b .
.SS Operations
Assignment copies elements and size.
.TP \n(xxu
.IB b [ k ]
A reference to element
.L k
of block
.IR b ;
undefined if
.I k
is out of bounds.
.TP
.BI ( T *) b
A pointer to the first element of block
.I b.
.SS Other functions
.TP \n(xxu
.IB b .size()
Return the number of elements in
.I b.
.TP
.IB b .size( n )
Set the size of
.I b
to
.LR n .
If the new size is greater than the old.
Otherwise,
.I n
old elements are kept.
Return the new size.
.TP
.IB b .reserve( n )
The size of
.I b
is increased, if necessary, to some value greater than
.I n.
If
.I b
already has room,
.I b
is not changed.
Return zero if memory could not be allocated
and non-zero otherwise.
.TP
.IB b .end()
Returns
a pointer to just past the last element in
.LR b .
Equivalent to
.BR (T*)b+b.size() .
.TP
.IB a .swap( b )
The memory associated with blocks
.I a
and
.I b
is exchanged.
.SS Performance
Most operations
are implemented by the obvious uses of the
.L new
and
.L delete
operators.
.I Reserve
checks the size inline.
If it isn't big enough, the size is increased by multiplying by 3/2
(and adding one) enough times to increase it beyond
.I n .
.SH EXAMPLES
.EX
Blockdeclare(long)
unsigned n = 0;
Block(long) b;
long x;
while (cin >> x) {
b.reserve(n);
b[n++] = x;
}
.EE
.SH SEE ALSO
.IR malloc (3),
.IR map (3)
.SH DIAGNOSTICS
The only error detected is running out of memory;
this is indicated in all cases by setting the size of the
block for which allocation failed to zero.
.SH BUGS
Elements are copied during reallocation by using
.L T::operator=
instead of
.LR T(T&) .
.br
Because the `type parameter'
.I T
is implemented by the C preprocessor, white space is
forbidden inside the parentheses of
.BI Block( T ) .
|