aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/bfc/tlist.h
blob: 14e591bf9abe822dab1c034a169a4936bc0dc1f6 (plain) (blame)
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
//PORTABLE
#ifndef _TLIST_H
#define _TLIST_H

#include <bfc/common.h>
#include <bfc/wasabi_std.h>
#include <bfc/named.h>

/*

A generic std::vector wannabe...

NOTE: you must only use this with basic types! constructors and destructors
will not be called.

*/

#include <bfc/bfc_assert.h>
#include <bfc/std_mem.h>

#define TLIST_INCREMENT 32

template<class T>
class TList {
public:
  TList() {
    nitems = 0;
    nslots = 0;
    items = NULL;
  }
  virtual ~TList() {
    FREE(items);
  }

  int getNumItems() const { return nitems; };

  // CAN'T PROVIDE BOUNDS CHECKING!!! (except by ASSERT() :( )
  T enumItem(int n) const {
    ASSERT(items != NULL);
    ASSERT(n >= 0 && n < nitems);
    return items[n];
  }
  T operator[](int n) const { return enumItem(n); }

  T getFirst() const { return enumItem(0); }
  T getLast() const { return enumItem(getNumItems()-1); }

  T *enumRef(int n) const {
    ASSERT(items != NULL);
    ASSERT(n >= 0 && n < nitems);
    return items+n;
  }

  T *addItem(const T &item) {
    ASSERT(nitems <= nslots);
    if (items == NULL) {
      nslots = TLIST_INCREMENT;
      items = (T *)MALLOC(sizeof(T) * nslots);
      nitems = 0;
    } else if (nslots == nitems) {	// time to expand
      T *newitems;
      nslots += TLIST_INCREMENT;
      newitems = (T *)MALLOC(sizeof(T) * nslots);
      MEMCPY(newitems, items, nitems * sizeof(T));
      FREE(items);
      items = newitems;
    }
    items[nitems++] = item;

    return &items[nitems-1];
  }

  T *insertItem(const T &item, int pos) { // position to insert
    ASSERT(pos >= 0 && pos <= nitems);
    T *tmp=addItem(item);
    int n=nitems-1;
    while (n > pos) {
      items[n]=items[n-1];
      n--;
    }
    tmp=items+pos;
    *tmp=item;
    return tmp;
  }

  T *replaceItemByPos(const T &item, int pos) {
    ASSERT(items != NULL);
    ASSERT(nitems != 0);
    ASSERT(pos >= 0 && pos < nitems);
    items[pos]=item;
    return &items[pos];
  }

  void setItem(int pos, const T &newval) {
    if (pos < 0) return;
    if (pos >= nitems) return;
    items[pos] = newval;
  }

  int delItem(const T &item) {
    int c = 0;

    ASSERT(items != NULL);
    ASSERT(nitems != 0);

    T *src = items;
    T *dst = items;
    for (int i = 0; i < nitems; i++) {
      if (*src != item) {
        *dst++ = *src;
        c++;
      }
      src++;
    }
    nitems -= c;

    return c;
  }

  int delByPos(int pos) {
    if (pos < 0 || pos >= nitems) return 0;
    --nitems;
    if (pos == nitems) return 1;	// last one? nothing to copy over
    MEMCPY(items+pos, items+(pos+1), sizeof(T)*(nitems-pos));  // CT:not (nitems-(pos+1)) as nitems has been decremented earlier
    return 1;
  }

  void removeAll() {
    FREE(items); items = NULL;
    nitems = 0;
    nslots = 0;
  }

  void freeAll() {
    for (int i = 0; i < nitems; i++)
      FREE(items[i]);
    removeAll();
  }

  int getItemPos(const T &it) const {
    int n = getNumItems();
    for (int i=0;i<n;i++)
      if(!MEMCMP(&items[i],&it,sizeof(T))) return i; // CT     if (items[i] == it) return i;
    return -1;
  }
  int haveItem(const T &it) const {
    return (getItemPos(it) != -1);
  }

private:
  int nitems, nslots;
  T *items;
};


#endif