summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/HtVectorGenericCode.h
blob: ac809f3b6167c4f4505ad550674149f2bc87e7cf (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
//
// HtVectorGenericCode.h
//
// HtVectorGeneric: A Vector class which holds objects of type GType.
//           (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: HtVectorGenericCode.h,v 1.5 2004/05/28 13:15:21 lha Exp $
//


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


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


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



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



//*********************************************************************
// void HtVectorGType::Insert(GType object, int position)
//   Add an object into the list.
//
void HtVectorGType::Insert(const GType &object, int position)
{
  if (position < 0) {CheckBounds(position);}
  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;
}


//*********************************************************************
// int HtVectorGType::RemoveFrom(int position)
//   Remove an object from the list.
//
void HtVectorGType::RemoveFrom(int position)
{
    CheckBounds(position);

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


//*********************************************************************
// GType HtVectorGType::Get_Next()
//   Return the next object in the list.
//
GType &HtVectorGType::Get_Next()
{
  current_index++;
  CheckBounds(current_index);
  return data[current_index];
}


//*********************************************************************
// GType HtVectorGType::Get_First()
//   Return the first object in the list.
//
GType &HtVectorGType::Get_First()
{
    CheckBounds(0);
    return data[0];
}

#ifndef HTVECTORGENERIC_NOTCOMPARABLE

//*********************************************************************
// int HtVectorGType::Index(GType obj)
//   Return the index of an object in the list.
//
int HtVectorGType::Index(const GType &obj)
{
    int			index0 = 0;

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


//*********************************************************************
// GType HtVectorGType::Next(GType prev)
//   Return the next object in the list.  Using this, the list will
//   appear as a circular list.
//
GType &HtVectorGType::Next(const GType & prev)
{
  current_index = Index(prev);
  CheckBounds(current_index);

  current_index++; // We should probably do this with remainders
  return Nth(current_index);
}

//*********************************************************************
// GType HtVectorGType::Previous(GType next)
//   Return the previous object in the vector.  Using this, the vector will
//   appear as a circular list.
//
GType &HtVectorGType::Previous(const GType & next)
{
  current_index = Index(next);
  CheckBounds(current_index);

  current_index--; // We should probably do this with remainders
  return Nth(current_index);
}

//*********************************************************************
// int HtVectorGType::Remove(GType object)
//   Remove an object from the list.
//
void HtVectorGType::Remove(const GType &object)
{
    int pos = Index(object);
    CheckBounds(pos);
    RemoveFrom(pos);
}
#endif

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

    for(int i = 0; i < Count(); i++)
{
#ifdef HTVECTORGENERIC_OBJECTPTRTYPE
      vector->Add(data[i]->Copy());
#else
      vector->Add(data[i]);
#endif
}
    return vector;
}


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

    for(int i = 0; i < vector.Count(); i++)
    {
	Add(vector.data[i]);
    }
    return *this;
}


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

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

      data = new GType[allocated];

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

      if (old_data)
	delete [] old_data;
    }
}


#ifdef  HTVECTORGENERIC_NOTCOMPARABLE
#undef  HTVECTORGENERIC_NOTCOMPARABLE
#endif

#undef HtVectorGType
#undef GType