source: golgotha/src/make/table.hh @ 484

Last change on this file since 484 was 80, checked in by Sam Hocevar, 15 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef MK_TABLE_HH
10#define MK_TABLE_HH
11
12#include "error.hh"
13
14template <class T>
15class table
16{
17protected:
18  T *entry;
19  int used,entries,grow;
20public:
21
22  int size() const { return used; }
23  T& operator[](int i) const
24  {
25    assert(i>=0 && i<used, "table::bad array reference");
26    return entry[i];
27  }   
28
29  table(int entries, int grow = 0) : entries(entries), grow(grow), entry(0), used(0)
30  {
31    if (entries>0)
32      entry = (T*)malloc(sizeof(T)*entries);
33    else
34      entry = (T*)malloc(sizeof(T)*grow);
35
36    assert(entry, "table::can't allocate entries");
37  }
38 
39  ~table()
40  {
41    clear();
42    free(entry);
43    entries = 0;
44  }
45 
46  int add(T item,int ref = -1)
47  {
48    assert(item, "table::bad item add");
49
50    if (ref<0)
51      ref += used+1;
52
53    assert(ref>=0 && ref<=used,"table::bad item referenced");
54
55    if (used>=entries)
56    {
57      if (grow)
58      {
59        entries += grow;
60
61        T* new_entry = (T*)realloc(entry, sizeof(T *)*entries);
62       
63        assert(new_entry, "table::out of memory");
64
65        entry = new_entry;
66      }
67      else
68        assert(0, "table::out of entries");
69    }
70
71    for (int i=used; i>ref; i--)
72      entry[i] = entry[i-1];
73    entry[ref] = item;
74    used++;
75
76    return ref;
77  }
78 
79  int add_table(const table& tab,int ref = -1)
80  {
81    if (ref<0)
82      ref += used+1;
83
84    assert(ref>=0 && ref<=used,"table::bad item referenced");
85
86    if (used+tab.size() >= entries)
87    {
88      if (grow)
89      {
90        if (used+tab.size() >= entries+grow)
91          entries = used+tab.size();
92        else
93          entries += grow;
94
95        T* new_entry = (T*)realloc(entry, sizeof(T *)*entries);
96       
97        assert(new_entry, "table::out of memory");
98
99        entry = new_entry;
100      }
101      else
102        assert(0, "table::out of entries");
103    }
104
105    int i;
106
107    for (i=used-1; i>ref; i--)
108      entry[i+tab.size()] = entry[i];
109    for (i=0; i<tab.size(); i++)
110      entry[ref+i] = tab.entry[i];
111
112    used+=tab.size();
113
114    return ref;
115  }
116 
117  void remove(int ref)
118  {
119    assert(ref>=0 && ref<used, "table::bad item deletion");
120
121    used--;
122    for (int i=ref; i<used; i++)
123      entry[i] = entry[i+1];
124  }
125 
126  void clear()
127  {
128    used = 0;
129  }
130};
131
132class name_table : public table<char *>
133{
134public:
135  name_table(int entries = 20, int grow = 100) : table<char*>(entries,grow) {}
136
137  name_table(name_table &other) : table<char*>(20,200)
138  {
139    add_table(other);
140  }
141
142  name_table& operator=(char *item) { clear(); add(item);  return *this; }
143  name_table& operator=(const name_table &tab) { clear(); add_table(tab);  return *this; }
144  name_table& operator+(char *item) { add(item);  return *this; }
145  name_table& operator+(const name_table &tab) { add_table(tab);  return *this; }
146  name_table& operator+=(char *item) { add(item);  return *this; }
147  name_table& operator+=(const name_table &tab) { add_table(tab);  return *this; }
148  name_table& clear() { table<char*>::clear(); return *this; }
149  int find(char *item)
150  {
151    for (int i=0; i<size(); i++)
152      if (strcmp(item, entry[i])==0)
153        return i;
154    return -1;
155  }
156
157};
158
159typedef name_table list;
160
161#endif
162
Note: See TracBrowser for help on using the repository browser.