File: Collection.h

package info (click to toggle)
hatman 0.5.0.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,416 kB
  • ctags: 1,971
  • sloc: cpp: 7,871; sh: 330; yacc: 322; ansic: 260; lex: 210; makefile: 104; awk: 80
file content (187 lines) | stat: -rw-r--r-- 5,387 bytes parent folder | download | duplicates (2)
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
/*C* -*-c++-*-
 *
 * Hatman - The Game of Kings
 * Copyright (C) 1997 James Pharaoh & Timothy Fisken
 *
 * 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 2 of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *C*/

#ifndef util_Collection_h
#define util_Collection_h

#include "debug.h"
#include "File.h"
#include "Object.h"
#include <assert.h>
#include <stdlib.h>

//--------------------------------------------------------------------------------------------------------------------------------

#define foreach(c,i) for(int i=0; i < ((c).length()); i++)
#define every(c,op) for(int i=0; i < ((c).length()); i++) (c).at(i)->op;

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> class Collection : public Object
{
private:
 T** data;
 int space, delta;

 size_t dataSize() { return sizeof(T*) * space; }
 void grow();

protected:
 int pLength;

 int length(int _length) { return pLength = _length; }

public:
 Collection(int _space = 64, int _delta = 64);
 Collection(Collection& object);
 virtual ~Collection();

 int length() { return pLength; }

 inline T* at(int n)
  {
   assert(n >= 0 && n <= length());
   return data[n];
  }
 inline T* operator [] (int n) { return at(n); }

 T* add(T* object);

 inline T* remove(int index) { T* ret = data[index]; data[index] = 0; return ret; }
 inline void removeAll() { pLength = 0; }
 void pack();

 void del(int index) { delete at(index); remove(index); }
 void delAll();
};

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> void Collection<T>::grow()
{
 space += delta;
 data = (T**) realloc(data, dataSize());
 if(!data) fatal("cannot realloc(%d) in Collection::Grow()\n", dataSize());
}

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> Collection<T>::Collection(int _space, int _delta)
{
 assert(_space > 0); assert(_delta > 0);
 space = _space;
 delta = _delta;
 pLength = 0;
 VPRINTF("<collection> allocating collection space=%d delta=%d\n", space, delta);
 data = (T**) malloc(dataSize());
};

template <class T> Collection<T>::Collection(Collection& object)
 : Object(object)
{
 space = object.space;
 delta = object.delta;
 pLength = 0;
 VPRINTF("<collection> duplicating collection space=%d delta=%d\n", space, delta);
 data = (T**) malloc(dataSize());
}

template <class T> Collection<T>::~Collection()
{
 delAll();
 free(data);
};

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> T* Collection<T>::add(T* object)
{
 if(length() == space) grow();
 return data[pLength++] = object;
}

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> void Collection<T>::pack()
{
 int dc = 0;
 for(int sc=0; sc<length(); sc++)
  if(data[sc]) data[dc++] = data[sc];
 length(dc);
}

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> void Collection<T>::delAll()
{
 for(int i=0; i<length(); i++)
  del(i);
 pack();
}

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> class SCollection : public Collection<T>
{
public:
 SCollection(int _space = 64, int _delta = 64) : Collection<T>(_space, _delta) { };
 bool read(File& f);
 bool write(File& f);
 static SCollection* create(File& f);
};

//--------------------------------------------------------------------------------------------------------------------------------

template <class T> bool SCollection<T>::read(File& f)
{
 delAll();
 int l;
 if(!f.read(l)) return false;
 VPRINTF("<collection> reading collection of length %d\n", l);
 pLength = 0;
 for(int n=0; n<l; n++)
  {
   T* s = T::create(f);
   if(!s) { setError("%d out of %d records read: %s", n, l, errStr); return false; }
   add(s);
  }
 VPRINTF("<collection> all %d objects read succesfully\n", pLength);
 return true;
}

template <class T> bool SCollection<T>::write(File& f)
{
 if(!f.write(pLength)) return false;
 for(int i=0; i<pLength; i++)
  if(!at(i)->write(f)) return false;
 return true;
}

template <class T> static SCollection<T>* create(class File& f)
{
 SCollection<T>* sc = new SCollection<T>;
 if(!sc->read(f)) { delete sc; return 0; }
 return sc;
}

//--------------------------------------------------------------------------------------------------------------------------------

#endif