summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/HtVector.cc
blob: b6addea3d5b9a33a2fb7d6a90022fe0ec7334be3 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// HtVector.cc
//
// HtVector: A Vector class which holds objects of type Object.
//           (A vector is an array that can expand as necessary)
//           This class is very similar in interface to the List class
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later 
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtVector.cc,v 1.11 2004/05/28 13:15:21 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "HtVector.h"

//*********************************************************************
// void HtVector::HtVector()
//   Default constructor
//
HtVector::HtVector()
{
  data = new Object  *[4]; // After all, why would anyone want an empty vector?
  element_count = 0;
  allocated = 4;
  current_index = -1;
}


//*********************************************************************
// void HtVector::HtVector(int capacity)
//   Constructor with known capacity
//   (has the side effect of not allocating double memory)
//
HtVector::HtVector(int capacity)
{
  data = new Object  *[capacity];
  element_count = 0;
  allocated = capacity;
  current_index = -1;
}


//*********************************************************************
// void HtVector::~HtVector()
//   Destructor
//
HtVector::~HtVector()
{
  Destroy();
}


//*********************************************************************
// void HtVector::Release()
//   Remove all objects from the vector, but do not delete them
void HtVector::Release()
{
  for (current_index = 0; current_index < element_count; current_index++)
    {
      data[current_index] = NULL;
    }
  if (data)
    delete [] data;
  data = NULL;
  allocated = 0;
  element_count = 0;
  current_index = -1;
}

//*********************************************************************
// void HtVector::Destroy()
//   Deletes all objects from the vector
//
void HtVector::Destroy()
{
  for (current_index = 0; current_index < element_count; current_index++)
    if (data[current_index])
      {
	delete data[current_index];
	data[current_index] = NULL;
      }
  if (data)
    delete [] data;
  data = NULL;
  allocated = 0;
  element_count = 0;
  current_index = -1;
}


//*********************************************************************
// void HtVector::Add(Object *object)
//   Add an object to the list.
//
void HtVector::Add(Object *object)
{
  Allocate(element_count+1);
  data[element_count] = object;
  element_count += 1;
}


//*********************************************************************
// void HtVector::Insert(Object *object, int position)
//   Add an object into the list.
//
void HtVector::Insert(Object *object, int position)
{
  if (position < 0) return;
  if (position >= element_count)
    {
      Add(object);
      return;
    }
  
  Allocate(element_count + 1);
  for (int i = element_count; i > position; i--)
    data[i] = data[i-1];
  data[position] = object;
  element_count += 1;
}


//*********************************************************************
// void HtVector::Assign(Object *object, int position)
// Assign an object to the position
//
void HtVector:: Assign(Object *object, int position)
{
  // Simply perform an insert, followed by a remove!
  Insert(object, position);
  RemoveFrom(position + 1);
  return;
}


//*********************************************************************
// int HtVector::Remove(Object *object)
//   Remove an object from the list.
//
int HtVector::Remove(Object *object)
{
  return RemoveFrom(Index(object));
}


//*********************************************************************
// int HtVector::RemoveFrom(int position)
//   Remove an object from the list.
//
int HtVector::RemoveFrom(int position)
{
  if (position < 0 || position >= element_count)
    return NOTOK;

  for (int i = position; i < element_count - 1; i++)
    data[i] = data[i+1];
  element_count -= 1;
  return OK;
}


//*********************************************************************
// Object *HtVector::Get_Next()
//   Return the next object in the list.
//
Object *HtVector::Get_Next()
{
  current_index++;
  if (current_index >= element_count)
    return 0;
  return data[current_index];
}


//*********************************************************************
// Object *HtVector::Get_First()
//   Return the first object in the list.
//
Object *HtVector::Get_First()
{
    if (!IsEmpty())
      {
	current_index = 0;
	return data[0];
      }
    else
	return 0;
}


//*********************************************************************
// int HtVector::Index(Object *obj)
//   Return the index of an object in the list.
//
int HtVector::Index(Object *obj)
{
    int			index = 0;

    while (index < element_count && data[index] != obj)
    {
	index++;
    }
    if (index >= element_count)
	return -1;
    else
	return index;
}


//*********************************************************************
// Object *HtVector::Next(Object *prev)
//   Return the next object in the list.  Using this, the list will
//   appear as a circular list.
//
Object *HtVector::Next(Object *prev)
{
  current_index = Index(prev);
  if (current_index == -1)
    return 0;

  current_index++; // We should probably do this with remainders
  if (current_index >= element_count)
    current_index = 0;
  return data[current_index];
}


//*********************************************************************
// Object *HtVector::Previous(Object *next)
//   Return the previous object in the vector.  Using this, the vector will
//   appear as a circular list.
//
Object *HtVector::Previous(Object *next)
{
  current_index = Index(next);
  if (current_index == -1)
    return 0;

  current_index--; // We should probably do this with remainders
  if (current_index < 0)
    current_index = element_count - 1;
  return data[current_index];
}


//*********************************************************************
// Object *HtVector::Copy() const
//   Return a deep copy of the vector.
//
Object *HtVector::Copy() const
{
    HtVector	*vector = new HtVector(allocated);

    for(int i = 0; i < Count(); i++)
      vector->Add(data[i]->Copy());

    return vector;
}


//*********************************************************************
// HtVector &HtVector::operator=(HtVector &vector)
//   Return a deep copy of the list.
//
HtVector &HtVector::operator=(HtVector &vector)
{
    Destroy();

    for(int i = 0; i < vector.Count(); i++)
      Add(vector.data[i]->Copy());

    return *this;
}


//*********************************************************************
// int Allocate(int capacity)
//    Ensure there is at least capacity space in the vector
//
void HtVector::Allocate(int capacity)
{
  if (capacity > allocated) // Darn, we actually have to do work :-)
    {
      Object	**old_data = data;

      // Ensure we have more than the capacity and we aren't
      // always rebuilding the vector (which leads to quadratic behavior)
      while (allocated < capacity)
	allocated *= 2;

      data = new Object *[allocated];

      for (int i = 0; i < element_count; i++)
	{
	  data[i] = old_data[i];
	  old_data[i] = NULL;
	}

      if (old_data)
	delete [] old_data;
    }
}