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
|
/*
* CENA C++ Utilities
*
* by Stephane Chatty
*
* Copyright 1990, 1991, 1992
* Laboratoire de Recherche en Informatique (LRI)
* Centre d'Etudes de la Navigation Aerienne (CENA)
*
* plain and generic arrays (orginal C version by Michel Beaudouin-Lafon)
*
* $Id$
* $CurLog$
*/
#include "Array.h"
#include <memory.h>
#include <stdio.h>
/*?class IvlArray
\typ{IvlArray}s are dynamic arrays whose lower bound is 0. For a
\typ{IvlArray} with size \var{size}, the valid indices are \var{0, \ldots, size -- 1.}
Their elements are of type \typ{IvlArrayItem*}. Methods are provided for
initialization, data storage and retrieval, and a few other utilities.
?*/
IvlArrayItem* IvlArray::InvalidCell = 0;
/*?
Initialize a \typ{IvlArray} with size \var{size}.
Entries are initialized to \var{value}.
?*/
IvlArray :: IvlArray (int size, IvlArrayItem* value)
{
if (size > 0) {
Data = new IvlArrayItem* [size];
register IvlArrayItem** p = Data;
for (register int i = 0; i < size; ++i, ++p)
*p = value;
} else {
Data = 0;
size = 0;
}
Length = size;
}
/*?nodoc?*/
IvlArray :: IvlArray (const IvlArray& a)
: Length (a.Length)
{
if (Length == 0) {
Data = 0;
} else {
Data = new IvlArrayItem* [Length];
register IvlArrayItem** p;
register IvlArrayItem** q;
register int i;
for (p = Data, q = a.Data, i = Length ; i--; ++p, ++q)
*p = *q;
}
}
/*?nodoc?*/
IvlArray :: ~IvlArray ()
{
/* Free the data */
Clean ();
}
/*
* Empty the array.
*/
/*?hidden?*/
void
IvlArray :: Clean ()
{
if (Data)
#ifndef CPLUS_BUG4
delete [] Data;
#else
delete [Length] Data;
#endif
Data = 0;
Length = 0;
}
/*?
Set all entries to \var{value}.
?*/
void
IvlArray :: Reset (IvlArrayItem* value)
{
register IvlArrayItem** p;
register int i;
for (p = Data, i = Length ; i--; ++p)
*p = value;
}
/*?
Copy the contents of the array \var{from} into this array.
The old contents, if any, are freed, and new memory is allocated for the copy.
?*/
IvlArray&
IvlArray :: operator = (const IvlArray& from)
{
if (this != &from) {
Clean ();
Length = from.Length;
if (Length > 0 && from.Data) {
Data = new IvlArrayItem* [Length];
memcpy (Data, from.Data, Length * sizeof (IvlArrayItem*));
} else
Data = 0;
}
return *this;
}
/*?
Change the size of the array to \var{size}. The new entries, if any,
are set to \var{value}.
This function reallocates the contents of the array.
?*/
void
IvlArray :: Resize (int size, IvlArrayItem* value)
{
if (size <= 0) {
Clean ();
return;
}
IvlArrayItem** NewData = new IvlArrayItem* [size];
/* if the new sequence is longer, copy and fill end, else just copy */
if (size > Length) {
if (Length)
memcpy (NewData, Data, Length * sizeof (IvlArrayItem*));
register IvlArrayItem** pfil = NewData + Length;
for (register int i = size - Length; i--; *pfil++ = value)
;
} else
memcpy (NewData, Data, size * sizeof (IvlArrayItem*));
/* free old data and set new array */
Clean ();
Data = NewData;
Length = size;
}
/*?
Return the address of the \var{i}th element.
?*/
IvlArrayItem**
IvlArray :: operator + (register int i) const
{
if (Check (i))
return Data+i;
else {
fprintf (stderr, "Index %d out of array bounds: size was %d\n", i, Length);
return &InvalidCell;
}
}
/*?
Get a reference to the \var{i}-th entry, so that this entry can be read or written.
If \var{i} is outside the bounds,
the operator returns a reference to a static variable which initially contains 0.
?*/
IvlArrayItem*&
IvlArray :: operator [] (register int i) const
{
if (Check (i))
return Data [i];
else {
fprintf (stderr, "Index %d out of array bounds: size was %d\n", i, Length);
return InvalidCell;
}
}
#ifdef DOC
/*?class IvlArrayOf
The generic class \typ{IvlArrayOf} is a derived class of \typ{IvlArray} that provides
arrays of pointers to class objects. When parameterized by the class \typ{ITEM},
the following functions are redefined:
?*/
/*?nextdoc?*/
IvlArrayOf :: IvlArrayOf (int size, ITEM* value = 0)
{
}
/*?nextdoc?*/
void
IvlArrayOf :: Reset (ITEM* value = 0)
{
}
/*?nextdoc?*/
void
IvlArrayOf :: Resize (int size, ITEM* value = 0)
{
}
/*?nextdoc?*/
ITEM*&
IvlArrayOf :: operator [] (int i) const
{
}
/*?
These functions are equivalent to their homonyms in the class \typ{IvlArray},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
ITEM**
IvlArrayOf :: operator + (int i) const
{
}
#endif /* DOC */
|