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 | //{{{ Table Template
|
---|
10 | //
|
---|
11 | // Expanding Array Class
|
---|
12 | //
|
---|
13 | //$Id: table.hh,v 1.3 1997/09/29 04:29:52 oliy Exp $
|
---|
14 | //}}}
|
---|
15 |
|
---|
16 | #ifndef I4_TABLE_HH
|
---|
17 | #define I4_TABLE_HH
|
---|
18 |
|
---|
19 | #include "arch.hh"
|
---|
20 | #include "error/error.hh"
|
---|
21 | #include "memory/malloc.hh"
|
---|
22 |
|
---|
23 | //#define I4_TABLE_NO_GROWTH
|
---|
24 |
|
---|
25 | template <class T>
|
---|
26 | class i4_table
|
---|
27 | {
|
---|
28 | protected:
|
---|
29 | T *entry;
|
---|
30 | w16 used,entries;
|
---|
31 | #ifndef I4_TABLE_NO_GROWTH
|
---|
32 | w16 grow;
|
---|
33 | #endif
|
---|
34 | public:
|
---|
35 |
|
---|
36 | int size() const { return used; }
|
---|
37 | T& operator[](int i) const
|
---|
38 | //{{{
|
---|
39 | {
|
---|
40 | I4_ASSERT(i>=0 && i<used, "table::bad array reference");
|
---|
41 | return entry[i];
|
---|
42 | }
|
---|
43 | //}}}
|
---|
44 |
|
---|
45 | i4_table(int entries, int grow = 0) : entries(entries), entry(0), used(0)
|
---|
46 | #ifndef I4_TABLE_NO_GROWTH
|
---|
47 | ,grow(grow)
|
---|
48 | #endif
|
---|
49 | //{{{
|
---|
50 | {
|
---|
51 | if (entries>0)
|
---|
52 | entry = (T*)i4_malloc(sizeof(T)*entries,"table");
|
---|
53 |
|
---|
54 | I4_ASSERT(entry, "table::can't allocate entries");
|
---|
55 | }
|
---|
56 | //}}}
|
---|
57 |
|
---|
58 | ~i4_table()
|
---|
59 | //{{{
|
---|
60 | {
|
---|
61 | clear();
|
---|
62 | i4_free(entry);
|
---|
63 | entries = 0;
|
---|
64 | }
|
---|
65 | //}}}
|
---|
66 |
|
---|
67 | int add(T item,int ref = -1)
|
---|
68 | //{{{
|
---|
69 | {
|
---|
70 | I4_ASSERT(item, "table::bad item add");
|
---|
71 |
|
---|
72 | if (ref<0)
|
---|
73 | ref += used+1;
|
---|
74 |
|
---|
75 | I4_ASSERT(ref>=0 && ref<=used,"table::bad item referenced");
|
---|
76 |
|
---|
77 | if (used>=entries)
|
---|
78 | {
|
---|
79 | #ifndef I4_TABLE_NO_GROWTH
|
---|
80 | if (grow)
|
---|
81 | {
|
---|
82 | entries += grow;
|
---|
83 |
|
---|
84 | T* new_entry = (T*)i4_realloc(entry, sizeof(T *)*entries, "table");
|
---|
85 |
|
---|
86 | I4_ASSERT(new_entry, "table::out of memory");
|
---|
87 |
|
---|
88 | entry = new_entry;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | #endif
|
---|
92 | I4_ASSERT(0, "table::out of entries");
|
---|
93 | }
|
---|
94 |
|
---|
95 | for (int i=used; i>ref; i--)
|
---|
96 | entry[i] = entry[i-1];
|
---|
97 | entry[ref] = item;
|
---|
98 | used++;
|
---|
99 |
|
---|
100 | return ref;
|
---|
101 | }
|
---|
102 | //}}}
|
---|
103 |
|
---|
104 | int add_table(const i4_table& tab,int ref = -1)
|
---|
105 | //{{{
|
---|
106 | {
|
---|
107 | if (ref<0)
|
---|
108 | ref += used+1;
|
---|
109 |
|
---|
110 | I4_ASSERT(ref>=0 && ref<=used,"table::bad item referenced");
|
---|
111 |
|
---|
112 | if (used+tab.size() >= entries)
|
---|
113 | {
|
---|
114 | #ifndef I4_TABLE_NO_GROWTH
|
---|
115 | if (grow)
|
---|
116 | {
|
---|
117 | if (used+tab.size() >= entries+grow)
|
---|
118 | entries = used+tab.size();
|
---|
119 | else
|
---|
120 | entries += grow;
|
---|
121 |
|
---|
122 | T* new_entry = (T*)i4_realloc(entry, sizeof(T *)*entries, "table");
|
---|
123 |
|
---|
124 | I4_ASSERT(new_entry, "table::out of memory");
|
---|
125 |
|
---|
126 | entry = new_entry;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | #endif
|
---|
130 | I4_ASSERT(0, "table::out of entries");
|
---|
131 | }
|
---|
132 |
|
---|
133 | int i;
|
---|
134 |
|
---|
135 | for (i=used-1; i>ref; i--)
|
---|
136 | entry[i+tab.size()] = entry[i];
|
---|
137 | for (i=0; i<tab.size(); i++)
|
---|
138 | entry[ref+i] = tab.entry[i];
|
---|
139 |
|
---|
140 | used+=tab.size();
|
---|
141 |
|
---|
142 | return ref;
|
---|
143 | }
|
---|
144 | //}}}
|
---|
145 |
|
---|
146 | void remove(int ref)
|
---|
147 | //{{{
|
---|
148 | {
|
---|
149 | I4_ASSERT(ref>=0 && ref<used, "table::bad item deletion");
|
---|
150 |
|
---|
151 | used--;
|
---|
152 | for (int i=ref; i<used; i++)
|
---|
153 | entry[i] = entry[i+1];
|
---|
154 | }
|
---|
155 | //}}}
|
---|
156 |
|
---|
157 | void clear()
|
---|
158 | //{{{
|
---|
159 | {
|
---|
160 | used = 0;
|
---|
161 | }
|
---|
162 | //}}}
|
---|
163 | };
|
---|
164 |
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | //{{{ Emacs Locals
|
---|
168 | // Local Variables:
|
---|
169 | // folded-file: t
|
---|
170 | // End:
|
---|
171 | //}}}
|
---|