aboutsummaryrefslogtreecommitdiff
blob: b8d380b54e14638cdf1f530eb003db24ac9a201c (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* Copyright (C) 2021-2023 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#ifndef _PERFAN_VEC_H
#define _PERFAN_VEC_H

#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>

// This package implements a vector of items.

#define Destroy(x)      if (x) { (x)->destroy(); delete (x); (x) = NULL; }
#define VecSize(x)      ((x) ? (x)->size() : 0)

void destroy (void *vec); // Free up the "two-dimension" Vectors

typedef int (*CompareFunc)(const void*, const void*);
typedef int (*ExtCompareFunc)(const void*, const void*, const void*);
typedef int (*SearchFunc)(char*, char*);

extern "C"
{
  typedef int (*StdCompareFunc)(const void*, const void*);
}

enum Search_type
{
  LINEAR,
  BINARY,
  HASH
};

enum Direction
{
  FORWARD,
  REVERSE
};

enum VecType
{
  VEC_VOID = 0,
  VEC_INTEGER,
  VEC_CHAR,
  VEC_BOOL,
  VEC_DOUBLE,
  VEC_LLONG,
  VEC_VOIDARR,
  VEC_STRING,
  VEC_INTARR,
  VEC_BOOLARR,
  VEC_LLONGARR,
  VEC_STRINGARR,
  VEC_DOUBLEARR
};

template <class ITEM> void
qsort (ITEM *, size_t, ExtCompareFunc, void *);

template <typename ITEM> class Vector
{
public:

  Vector ()
  {
    count = 0;
    data = NULL;
    limit = 0;
    sorted = false;
  };

  Vector (long sz);

  virtual
  ~Vector ()
  {
    free (data);
  }

  void append (const ITEM item);
  void addAll (Vector<ITEM> *vec);
  Vector<ITEM> *copy ();        // Return a copy of "this".

  ITEM
  fetch (long index)
  {
    return data[index];
  }

  ITEM
  get (long index)
  {
    return data[index];
  }

  // Return the first index in "this" that equals "item".
  // Return -1 if "item" is not found.
  long find (const ITEM item);
  long find_r (const ITEM item);

  // Insert "item" into "index"'th slot of "this",
  // moving everything over by 1.
  void insert (long index, const ITEM item);

  // Insert "item" after locating its appropriate index
  void incorporate (const ITEM item, CompareFunc func);

  // Remove and return the "index"'th item from "this",
  // moving everything over by 1.
  ITEM remove (long index);

  // Swap two items in "this",
  void swap (long index1, long index2);

  long
  size ()
  {
    return count;
  }

  // Store "item" into the "index"'th slot of "this".
  void store (long index, const ITEM item);

  void
  put (long index, const ITEM item)
  {
    store (index, item);
  }

  // Sort the vector according to compare
  void
  sort (CompareFunc compare, void *arg = NULL)
  {
    qsort (data, count, (ExtCompareFunc) compare, arg);
    sorted = true;
  }

  // Binary search, vector must be sorted
  long bisearch (long start, long end, void *key, CompareFunc func);
  void destroy ();      // delete all vector elements (must be pointers!)

  void
  reset ()
  {
    count = 0;
    sorted = false;
  }

  bool
  is_sorted ()
  {
    return sorted;
  }

  virtual VecType
  type ()
  {
    return VEC_VOID;
  }

  virtual void
  dump (const char * /* msg */)
  {
    return;
  }

private:

  void resize (long index);

  ITEM *data;   // Pointer to data vector
  long count;   // Number of items
  long limit;   // Vector length (power of 2)
  bool sorted;
};

template<> VecType Vector<int>::type ();
template<> VecType Vector<unsigned>::type ();
template<> VecType Vector<char>::type ();
template<> VecType Vector<bool>::type ();
template<> VecType Vector<double>::type ();
template<> VecType Vector<long long>::type ();
template<> VecType Vector<uint64_t>::type ();
template<> VecType Vector<void*>::type ();
template<> VecType Vector<char*>::type ();
template<> VecType Vector<Vector<int>*>::type ();
template<> VecType Vector<Vector<char*>*>::type ();
template<> VecType Vector<Vector<long long>*>::type ();
template<> void Vector<char *>::destroy ();

#define KILOCHUNK   1024
#define MEGACHUNK   1024*1024
#define GIGACHUNK   1024*1024*1024

// A standard looping construct:
#define Vec_loop(ITEM, vec, index, item) \
if (vec != NULL) \
    for (index = 0, item = ((vec)->size() > 0) ? (vec)->fetch(0) : (ITEM)0; \
	 index < (vec)->size(); \
	 item = (++index < (vec)->size()) ? (vec)->fetch(index) : (ITEM)0)

template <typename ITEM>
Vector<ITEM>::Vector (long sz)
{
  count = 0;
  limit = sz > 0 ? sz : KILOCHUNK; // was 0;
  data = limit ? (ITEM *) malloc (sizeof (ITEM) * limit) : NULL;
  sorted = false;
}

template <typename ITEM> void
Vector<ITEM>
::resize (long index)
{
  if (index < limit)
    return;
  if (limit < 16)
    limit = 16;
  while (index >= limit)
    {
      if (limit > GIGACHUNK)
	limit += GIGACHUNK; // Deoptimization for large experiments
      else
	limit = limit * 2;
    }
  data = (ITEM *) realloc (data, limit * sizeof (ITEM));
}

template <typename ITEM> void
Vector<ITEM>::append (const ITEM item)
{
  // This routine will append "item" to the end of "this".
  if (count >= limit)
    resize (count);
  data[count++] = item;
}

template <typename ITEM> void
Vector<ITEM>::addAll (Vector<ITEM> *vec)
{
  if (vec)
    for (int i = 0, sz = vec->size (); i < sz; i++)
      append (vec->fetch (i));
}

template <typename ITEM> Vector<ITEM> *
Vector<ITEM>::copy ()
{
  // This routine will return a copy of "this".
  Vector<ITEM> *vector;
  vector = new Vector<ITEM>;
  vector->count = count;
  vector->limit = limit;
  vector->data = (ITEM *) malloc (sizeof (ITEM) * limit);
  (void) memcpy ((char *) vector->data, (char *) data, sizeof (ITEM) * count);
  return vector;
}

template <typename ITEM> long
Vector<ITEM>::find (const ITEM match_item)
{
  for (long i = 0; i < size (); i++)
    if (match_item == get (i))
      return i;
  return -1;
}

template <typename ITEM> long
Vector<ITEM>::find_r (const ITEM match_item)
{
  for (long i = size () - 1; i >= 0; i--)
    if (match_item == get (i))
      return i;
  return -1;
}

template <typename ITEM> void
Vector<ITEM>::insert (long index, const ITEM item)
{
  // This routine will insert "item" into the "index"'th slot of "this".
  // An error occurs if "index" > size().
  // "index" is allowed to be equal to "count" in the case that
  // you are inserting past the last element of the vector.
  // In that case, the bcopy below becomes a no-op.
  assert (index >= 0);
  assert (index <= count);
  append (item);
  (void) memmove (((char *) (&data[index + 1])), (char *) (&data[index]),
		  (count - index - 1) * sizeof (ITEM));
  data[index] = item;
}

template <typename ITEM> ITEM
Vector<ITEM>::remove (long index)
{
  // This routine will remove the "index"'th item from "this" and
  // return it.  An error occurs if "index" >= size();.
  assert (index >= 0);
  assert (index < count);
  ITEM item = data[index];
  for (long i = index + 1; i < count; i++)
    data[i - 1] = data[i];
  count--;
  // Bad code that works good when ITEM is a pointer type
  data[count] = item;
  return data[count];
}

template <typename ITEM> void
Vector<ITEM>::swap (long index1, long index2)
{
  ITEM item;
  item = data[index1];
  data[index1] = data[index2];
  data[index2] = item;
}

template <typename ITEM> void
Vector<ITEM>::store (long index, const ITEM item)
{
  if (index >= count)
    {
      resize (index);
      memset (&data[count], 0, (index - count) * sizeof (ITEM));
      count = index + 1;
    }
  data[index] = item;
}

// This routine performs a binary search across
// the entire vector, with "start" being the low boundary.
// It is assumed that the vector is SORTED in
// ASCENDING ORDER by the same criteria as the
// compare function.
// If no match is found, -1 is returned.
template <typename ITEM> long
Vector<ITEM>::bisearch (long start, long end, void *key, CompareFunc compare)
{
  ITEM *itemp;
  if (end == -1)
    end = count;
  if (start >= end)
    return -1; // start exceeds limit
  itemp = (ITEM *) bsearch ((char *) key, (char *) &data[start],
			  end - start, sizeof (ITEM), (StdCompareFunc) compare);
  if (itemp == (ITEM *) 0)
    return -1; // not found
  return (long) (itemp - data);
}

template <typename ITEM> void
Vector<ITEM>::incorporate (const ITEM item, CompareFunc compare)
{
  long lt = 0;
  long rt = count - 1;
  while (lt <= rt)
    {
      long md = (lt + rt) / 2;
      if (compare (data[md], item) < 0)
	lt = md + 1;
      else
	rt = md - 1;
    }
  if (lt == count)
    append (item);
  else
    insert (lt, item);
}

#define QSTHRESH 6

template <typename ITEM> void
qsort (ITEM *base, size_t nelem, ExtCompareFunc qcmp, void *arg)
{
  for (;;)
    {
      // For small arrays use insertion sort
      if (nelem < QSTHRESH)
	{
	  for (size_t i = 1; i < nelem; i++)
	    {
	      ITEM *p = base + i;
	      ITEM *q = p - 1;
	      if (qcmp (q, p, arg) > 0)
		{
		  ITEM t = *p;
		  *p = *q;
		  while (q > base && qcmp (q - 1, &t, arg) > 0)
		    {
		      *q = *(q - 1);
		      --q;
		    }
		  *q = t;
		}
	    }
	  return;
	}

      ITEM *last = base + nelem - 1;
      ITEM *mid = base + nelem / 2;
      // Sort the first, middle, and last elements
      ITEM *a1 = base, *a2, *a3;
      if (qcmp (base, mid, arg) > 0)
	{
	  if (qcmp (mid, last, arg) > 0)
	    { // l-m-b
	      a2 = last;
	      a3 = last;
	    }
	  else if (qcmp (base, last, arg) > 0)
	    { // l-b-m
	      a2 = mid;
	      a3 = last;
	    }
	  else
	    { // m-b-l
	      a2 = mid;
	      a3 = mid;
	    }
	}
      else if (qcmp (mid, last, arg) > 0)
	{
	  a1 = mid;
	  a3 = last;
	  if (qcmp (base, last, arg) > 0)  // m-l-b
	    a2 = base;
	  else  // b-l-m
	    a2 = a3;
	}
      else // b-m-l
	a3 = a2 = a1;
      if (a1 != a2)
	{
	  ITEM t = *a1;
	  *a1 = *a2;
	  if (a2 != a3)
	    *a2 = *a3;
	  *a3 = t;
	}

      // Partition
      ITEM *i = base + 1;
      ITEM *j = last - 1;
      for (;;)
	{
	  while (i < mid && qcmp (i, mid, arg) <= 0)
	    i++;
	  while (j > mid && qcmp (mid, j, arg) <= 0)
	    j--;
	  if (i == j)
	    break;
	  ITEM t = *i;
	  *i = *j;
	  *j = t;
	  if (i == mid)
	    {
	      mid = j;
	      i++;
	    }
	  else if (j == mid)
	    {
	      mid = i;
	      j--;
	    }
	  else
	    {
	      i++;
	      j--;
	    }
	}

      // Compare two partitions. Do the smaller one by recursion
      // and loop over the larger one.
      size_t nleft = mid - base;
      size_t nright = nelem - nleft - 1;
      if (nleft <= nright)
	{
	  qsort (base, nleft, qcmp, arg);
	  base = mid + 1;
	  nelem = nright;
	}
      else
	{
	  qsort (mid + 1, nright, qcmp, arg);
	  nelem = nleft;
	}
    }
}

template<> inline void
Vector<char*>::destroy ()
{
  for (long i = 0; i < count; i++)
    free (data[i]);
  count = 0;
}

template <typename ITEM> inline void
Vector<ITEM>::destroy ()
{
  for (long i = 0; i < count; i++)
    delete data[i];
  count = 0;
}

#endif /* _VEC_H */