File: csvector.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (265 lines) | stat: -rw-r--r-- 5,295 bytes parent folder | download
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
/*
    Crystal Space Vector class
    Copyright (C) 1998,1999,2000 by Andrew Zabolotny <bit@eltech.ru>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <string.h>
#include "cssysdef.h"
#include "csutil/csvector.h"

csBasicVector::csBasicVector (int ilimit, int ithreshold) : count(0), root(0)
{
  limit = (ilimit > 0 ? ilimit : 0);
  threshold = (ithreshold > 0 ? ithreshold : 16);
  if (limit != 0)
    root = (csSome*)malloc (limit * sizeof(csSome));
}

csBasicVector::~csBasicVector ()
{
  if (root)
    free (root);
}

void csBasicVector::SetLength (int n)
{
  count = n;
  if (n > limit || (limit > threshold && n < limit - threshold))
  {
    n = ((n + threshold - 1) / threshold ) * threshold;
    if (!n)
    {
      if (root)
        free (root);
      root = NULL;
    }
    else if (root == 0)
      root = (csSome*)malloc(n * sizeof(csSome));
    else
      root = (csSome*)realloc(root, n * sizeof(csSome));
    limit = n;
  }
}

int csBasicVector::Find (csSome which) const
{
  int i;
  for (i = 0; i < Length (); i++)
    if (root [i] == which)
      return i;
  return -1;
}

bool csBasicVector::Insert (int n, csSome Item)
{
  if (n <= count)
  {
    SetLength (count + 1); // Increments 'count' as a side-effect.
    const int nmove = (count - n - 1);
    if (nmove > 0)
      memmove (&root [n + 1], &root [n], nmove * sizeof (csSome));
    root [n] = Item;
    return true;
  }
  else
   return false;
}

bool csBasicVector::Delete (int n)
{
  if (n >= 0 && n < count)
  {
    const int ncount = count - 1;
    const int nmove = ncount - n;
    if (nmove > 0)
      memmove (&root [n], &root [n + 1], nmove * sizeof (csSome));
    SetLength (ncount);
    return true;
  }
  else
    return false;
}

void csVector::DeleteAll (bool FreeThem)
{
  if (FreeThem)
  {
    int idx;
    for (idx = count - 1; idx >= 0; idx--)
      if (!FreeItem (root [idx]))
        break;
    SetLength (idx + 1);
    while (idx >= 0)
      Delete (idx--);
  }
  else
    SetLength (0);
}

bool csVector::FreeItem (csSome Item)
{
  (void)Item;
  return true;
}

bool csVector::Delete (int n, bool FreeIt)
{
  if (n >= 0 && n < count)
  {
    if (FreeIt)
    {
      if (!FreeItem (root [n]))
        return false;
    }
    return csBasicVector::Delete(n);
  }
  else
    return false;
}

bool csVector::Replace (int n, csSome what, bool FreePrevious)
{
  if (n < count)
  {
    if (FreePrevious)
    {
      if (!FreeItem (root [n]))
        return false;
    }
    root [n] = what;
    return true;
  }
  else
    return false;
}

int csVector::FindKey (csConstSome Key, int Mode) const
{
  int i;
  for (i = 0; i < Length (); i++)
    if (CompareKey (root [i], Key, Mode) == 0)
      return i;
  return -1;
}

int csVector::FindSortedKey (csConstSome Key, int Mode) const
{
  int l = 0, r = Length () - 1;
  while (l <= r)
  {
    int m = (l + r) / 2;
    int cmp = CompareKey (root [m], Key, Mode);

    if (cmp == 0)
      return m;
    else if (cmp < 0)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}

int csVector::InsertSorted (csSome Item, int *oEqual, int Mode)
{
  int m = 0, l = 0, r = Length () - 1;
  while (l <= r)
  {
    m = (l + r) / 2;
    int cmp = Compare (root [m], Item, Mode);

    if (cmp == 0)
    {
      if (oEqual)
        *oEqual = m;
      Insert (++m, Item);
      return m;
    }
    else if (cmp < 0)
      l = m + 1;
    else
      r = m - 1;
  }
  if (r == m)
    m++;
  Insert (m, Item);
  if (oEqual)
    *oEqual = -1;
  return m;
}

void csVector::QuickSort (int Left, int Right, int Mode)
{
recurse:
  int i = Left, j = Right;
  int x = (Left + Right) / 2;
  do
  {
    while ((i != x) && (Compare (Get (i), Get (x), Mode) < 0))
      i++;
    while ((j != x) && (Compare (Get (j), Get (x), Mode) > 0))
      j--;
    if (i < j)
    {
      Exchange (i, j);
      if (x == i)
        x = j;
      else if (x == j)
        x = i;
    }
    if (i <= j)
    {
      i++;
      if (j > Left)
        j--;
    }
  } while (i <= j);

  if (j - Left < Right - i)
  {
    if (Left < j)
      QuickSort (Left, j, Mode);
    if (i < Right)
    {
      Left = i;
      goto recurse;
    }
  }
  else
  {
    if (i < Right)
      QuickSort (i, Right, Mode);
    if (Left < j)
    {
      Right = j;
      goto recurse;
    }
  }
}

int csVector::Compare (csSome Item1, csSome Item2, int Mode) const
{
  (void)Mode;
  return ((int)Item1 > (int)Item2) ? +1 : ((int)Item1 == (int)Item2) ? 0 : -1;
}

int csVector::CompareKey (csSome Item, csConstSome Key, int Mode) const
{
  (void)Mode;
  return (Item > Key) ? +1 : (Item == Key) ? 0 : -1;
}