summaryrefslogtreecommitdiff
path: root/utils/Array.cc
blob: e51e7aefe36f07c49a93ce55d42350771189c16f (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
/*
 *	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 CcuArray
\typ{CcuArray}s are dynamic arrays whose lower bound is 0. For a 
\typ{CcuArray} with size \var{size}, the valid indices are \var{0, \ldots, size -- 1.}
Their elements are of type \typ{CcuArrayItem*}. Methods are provided for 
initialization, data storage and retrieval, and a few other utilities. 
?*/

CcuArrayItem* CcuArray::InvalidCell = 0;

/*?
Initialize a \typ{CcuArray} with size \var{size}.
Entries are initialized to \var{value}.
?*/
CcuArray :: CcuArray (int size, CcuArrayItem* value)
{
	if (size > 0) {
		Data = new CcuArrayItem* [size];
		register CcuArrayItem** p = Data;
		for (register int i = 0; i < size; ++i, ++p)
			*p = value;
	} else {
		Data = 0;
		size = 0;
	}
	
	Length = size;

}

/*?nodoc?*/
CcuArray :: CcuArray (const CcuArray& a)
: Length (a.Length)
{
	if (Length == 0) {
		Data = 0;
	} else {
		Data = new CcuArrayItem* [Length];
		register CcuArrayItem** p;
		register CcuArrayItem** q;
		register int i;
		for (p = Data, q = a.Data, i = Length ; i--; ++p, ++q)
			*p = *q;
	}
}

/*?nodoc?*/
CcuArray :: ~CcuArray ()
{
	/* Free the data */
	Clean ();
}

/*
 *	Empty the array.
 */
/*?hidden?*/
void
CcuArray :: Clean ()
{
	if (Data)
#ifndef CPLUS_BUG4
		delete [] Data;
#else
		delete [Length] Data;
#endif

	Data = 0;
	Length = 0;
}


/*?
Set all entries to \var{value}.
?*/
void
CcuArray :: Reset (CcuArrayItem* value)
{
	register CcuArrayItem** 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.
?*/
CcuArray&
CcuArray :: operator = (const CcuArray& from)
{
	if (this != &from) {
		Clean ();
		Length = from.Length;
		if (Length > 0 && from.Data) {
			Data = new CcuArrayItem* [Length];
			memcpy (Data, from.Data, Length * sizeof (CcuArrayItem*));
		} 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
CcuArray :: Resize (int size, CcuArrayItem* value)
{
	if (size <= 0) {
		Clean ();
		return;
	}

	CcuArrayItem** NewData = new CcuArrayItem* [size];

	/* if the new sequence is longer, copy and fill end, else just copy */
	if (size > Length) {
		if (Length)
			memcpy (NewData, Data, Length * sizeof (CcuArrayItem*));

		register CcuArrayItem** pfil = NewData + Length;
		for (register int i = size - Length; i--; *pfil++ = value)
			;
	} else
		memcpy (NewData, Data, size * sizeof (CcuArrayItem*));

	
	/* free old data and set new array */
	Clean ();
	Data = NewData;
	Length = size;
}


/*?
Return the address of the \var{i}th element.
?*/
CcuArrayItem**
CcuArray :: 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.
?*/
CcuArrayItem*&
CcuArray :: 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 CcuArrayOf
The generic class \typ{CcuArrayOf} is a derived class of \typ{CcuArray} that provides
arrays of pointers to class objects. When parameterized by the class \typ{ITEM},
the following functions are redefined:
?*/

/*?nextdoc?*/
CcuArrayOf :: CcuArrayOf (int size, ITEM* value = 0)
{
}

/*?nextdoc?*/
void
CcuArrayOf :: Reset (ITEM* value = 0)
{
}

/*?nextdoc?*/
void
CcuArrayOf :: Resize (int size, ITEM* value = 0)
{
}

/*?nextdoc?*/
ITEM*&
CcuArrayOf :: operator [] (int i) const
{
}

/*?
These functions are equivalent to their homonyms in the class \typ{CcuArray},
except that they are customized in order to manipulate pointers to \typ{ITEM}
instead of \typ{void*}.
?*/
ITEM**
CcuArrayOf :: operator + (int i) const
{
}

#endif	/* DOC */