~drizzle-trunk/drizzle/development

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
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Original author: Paul McCullagh (H&G2JCtL)
 * Continued development: Barry Leslie
 *
 * 2007-06-15
 *
 * CORE SYSTEM STORAGE
 * Basic storage structures.
 *
 */

#ifndef __CSSTORAGE_H__
#define __CSSTORAGE_H__

#include <stdio.h>

#include "CSDefs.h"
#include "CSString.h"

class CSHashTable;

class CSHashTable : public CSObject {
public:
	CSHashTable(): iSize(0), iTable(NULL) { }
	virtual ~CSHashTable();

	void setSize(uint32_t size);

	void clear();

	/* Value must be given referenced. */
	void add(CSObject *item);

	/* Value is returned NOT referenced. */
	CSObject *find(CSObject *key);
	
	bool remove(CSObject *key);

private:
	uint32_t iSize;

	CSObject **iTable;
};

#define SC_SORT_LIST_INC_SIZE		20

class CSSortedList : public CSObject {
public:
	CSSortedList(): iListSize(0), iInUse(0), iList(NULL) { }
	virtual ~CSSortedList() { clear(); }

	void clear();

	/* Value must be given referenced. */
	void add(CSObject *item);
	
	/* Value is returned NOT referenced. */
	CSObject *find(CSObject *key);

	CSObject *itemAt(uint32_t idx);
	
	CSObject *takeItemAt(uint32_t idx); // Takes item off of list.
	
	void remove(CSObject *key);
	
	uint32_t getSize() { return iInUse; }


private:
	uint32_t iListSize;

	uint32_t iInUse;

	CSObject **iList;

	CSObject *search(CSObject *key, uint32_t& idx);
};

class CSSyncSortedList : public CSSortedList, public CSSync {
public:
	CSSyncSortedList(): CSSortedList(), CSSync() { }
};

class CSLinkedItem : public CSRefObject {
public:
	CSLinkedItem(): CSRefObject(), iNextLink(NULL), iPrevLink(NULL) { }
	virtual ~CSLinkedItem() { }

	virtual CSObject *getNextLink() { return iNextLink; }
	virtual CSObject *getPrevLink() { return iPrevLink; }
	virtual void setNextLink(CSObject *link) { iNextLink = link; }
	virtual void setPrevLink(CSObject *link) { iPrevLink = link; }

private:
	CSObject		*iNextLink;
	CSObject		*iPrevLink;
};

/*
 * Items are linked so that following the previous pointers
 * you move from front to back.
 * Following the next pointers you move from back to front.
 */
class CSLinkedList : public CSObject {
public:
	CSLinkedList(): iSize(0), iListFront(NULL), iListBack(NULL) { }
	virtual ~CSLinkedList() { clear(); }

	void clear();
	
	uint32_t getSize() { return iSize; }

	/* Value must be given referenced. */
	void addFront(CSObject *item);
	void addBack(CSObject *item);

	bool remove(CSObject *item);

	/* Value is returned referenced. */
	CSObject *removeBack();

	/* Value is returned NOT referenced. */
	CSObject *getBack();

	/* Value is returned NOT referenced. */
	CSObject *getFront();

	/* Value is returned referenced. */
	CSObject *removeFront();
private:
	uint32_t iSize;
	CSObject *iListFront;
	CSObject *iListBack;
};

class CSSyncLinkedList : public CSLinkedList, public CSSync {
public:
	CSSyncLinkedList(): CSLinkedList(), CSSync() { }
};

class CSVector : public CSObject {
public:
	CSVector(uint32_t growSize): iGrowSize(growSize), iMaxSize(0), iUsage(0), iArray(NULL) { }
	virtual ~CSVector() { free(); }

	void free();

	void clear();

	/*
	 * Remove and object from the vector, and
	 * The object is rfemoved from the list.
	 * return a reference.
	 */
	CSObject *take(uint32_t idx);

	/*
	 * Remove an object from the vector.
	 */
	void remove(uint32_t idx);

	/*
	 * Get a reference to an object in the vector.
	 * A reference to the object remains on the list.
	 * Value returned is NOT referenced!
	 */
	CSObject *get(uint32_t idx);

	/* Set a specific index: */
	void set(uint32_t idx, CSObject *);

	/*
	 * Add an object to the end of the vector.
	 * Value must be referenced.
	 */
	void add(CSObject *);

	uint32_t size() { return iUsage; }

private:
	uint32_t iGrowSize;
	uint32_t iMaxSize;
	uint32_t iUsage;

	CSObject **iArray;
};

class CSSyncVector : public CSVector, public CSSync {
public:
	CSSyncVector(uint32_t growSize): CSVector(growSize), CSSync() { }
};

typedef struct CSSpareArrayItem {
	uint32_t		sa_index;
	CSObject	*sa_object;
} CSSpareArrayItemRec, *CSSpareArrayItemPtr;

class CSSparseArray : public CSObject {
public:
	CSSparseArray(uint32_t growSize): iGrowSize(growSize), iMaxSize(0), iUsage(0), iArray(NULL) { }
	CSSparseArray(): iGrowSize(10), iMaxSize(0), iUsage(0), iArray(NULL) { }
	virtual ~CSSparseArray() { free(); }

	void free();

	void clear();

	CSObject *take(uint32_t sparse_idx);

	void remove(uint32_t sparse_idx);

	void removeFirst();

	CSObject *itemAt(uint32_t idx);

	CSObject *get(uint32_t sparse_idx);
	
	uint32_t getIndex(uint32_t sparse_idx);
	
	void set(uint32_t sparse_idx, CSObject *);

	uint32_t size() { return iUsage; }

	uint32_t minIndex() {
		if (iUsage == 0)
			return 0;
		return iArray[0].sa_index;
	}

	uint32_t maxIndex() {
		if (iUsage == 0)
			return 0;
		return iArray[iUsage-1].sa_index;
	}

	CSObject *first();

	CSObject *last();

private:
	uint32_t				iGrowSize;
	uint32_t				iMaxSize;
	uint32_t				iUsage;
	CSSpareArrayItemPtr	iArray;

	CSObject *search(uint32_t idx, uint32_t& pos);
};

class CSSyncSparseArray : public CSSparseArray, public CSSync {
public:
	CSSyncSparseArray(uint32_t growSize): CSSparseArray(growSize), CSSync() { }
};

class CSOrderKey : public CSObject {
public:
	virtual int compareKey(CSOrderKey *key) = 0;
	int compareKey(CSObject *key) {return CSObject::compareKey(key);}
};

typedef struct CSOrderedListItem {
	CSOrderKey	*li_key;
	CSObject	*li_item;
} CSOrderedListItemRec, *CSOrderedListItemPtr;

class CSOrderedList : public CSObject {
public:
	CSOrderedList(): iListSize(0), iInUse(0), iList(NULL) { }
	virtual ~CSOrderedList() { clear(); }

	void clear();

	/* Value must be given referenced. */
	void add(CSOrderKey *key, CSObject *item);
	
	/* Value is returned NOT referenced. */
	CSObject *find(CSOrderKey *key);
	
	/* Value is returned NOT referenced. */
	CSObject *itemAt(uint32_t idx);
	
	void remove(CSOrderKey *key);

private:
	uint32_t iListSize;

	uint32_t iInUse;

	CSOrderedListItemPtr iList;

	CSOrderedListItemPtr search(CSOrderKey *key, uint32_t *idx);
};

class CSSyncOrderedList : public CSOrderedList, public CSSync {
public:
	CSSyncOrderedList(): CSOrderedList(), CSSync() { }
};

class CSQueue;

typedef struct CSQueueItem {
	CSObject			*qi_object;
	CSQueue				*qi_next;
} CSQueueItemRec, *CSQueueItemPtr;

class CSQueue : public CSObject {
public:
	CSQueue(): iQueueSize(0), iHighWater(0), iFront(NULL), iBack(NULL), iFree(NULL) { }
	virtual ~CSQueue() { clear(); }

	void clear();

	/* Value must be given referenced. */
	void add(CSObject *item);

	/* Returns a referenced value, on a FIFO basis. */
	CSObject *remove();

private:
	uint32_t iQueueSize;
	uint32_t iHighWater;

	CSQueueItemPtr	iFront;
	CSQueueItemPtr	iBack;
	CSQueueItemPtr	iFree;
};

#endif