Drizzled Public API Documentation

CSStorage.h

00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-06-15
00023  *
00024  * CORE SYSTEM STORAGE
00025  * Basic storage structures.
00026  *
00027  */
00028 
00029 #pragma once
00030 #ifndef __CSSTORAGE_H__
00031 #define __CSSTORAGE_H__
00032 
00033 #include <stdio.h>
00034 
00035 #include "CSDefs.h"
00036 #include "CSString.h"
00037 
00038 class CSHashTable;
00039 
00040 class CSHashTable : public CSObject {
00041 public:
00042   CSHashTable(): iSize(0), iTable(NULL) { }
00043   virtual ~CSHashTable();
00044 
00045   void setSize(uint32_t size);
00046 
00047   void clear();
00048 
00049   /* Value must be given referenced. */
00050   void add(CSObject *item);
00051 
00052   /* Value is returned NOT referenced. */
00053   CSObject *find(CSObject *key);
00054   
00055   bool remove(CSObject *key);
00056 
00057 private:
00058   uint32_t iSize;
00059 
00060   CSObject **iTable;
00061 };
00062 
00063 #define SC_SORT_LIST_INC_SIZE   20
00064 
00065 class CSSortedList : public CSObject {
00066 public:
00067   CSSortedList(): iListSize(0), iInUse(0), iList(NULL) { }
00068   virtual ~CSSortedList() { clear(); }
00069 
00070   void clear();
00071 
00072   /* Value must be given referenced. */
00073   void add(CSObject *item);
00074   
00075   /* Value is returned NOT referenced. */
00076   CSObject *find(CSObject *key);
00077 
00078   CSObject *itemAt(uint32_t idx);
00079   
00080   CSObject *takeItemAt(uint32_t idx); // Takes item off of list.
00081   
00082   void remove(CSObject *key);
00083   
00084   uint32_t getSize() { return iInUse; }
00085 
00086 
00087 private:
00088   uint32_t iListSize;
00089 
00090   uint32_t iInUse;
00091 
00092   CSObject **iList;
00093 
00094   CSObject *search(CSObject *key, uint32_t& idx);
00095 };
00096 
00097 class CSSyncSortedList : public CSSortedList, public CSSync {
00098 public:
00099   CSSyncSortedList(): CSSortedList(), CSSync() { }
00100 };
00101 
00102 class CSLinkedItem : public CSRefObject {
00103 public:
00104   CSLinkedItem(): CSRefObject(), iNextLink(NULL), iPrevLink(NULL) { }
00105   virtual ~CSLinkedItem() { }
00106 
00107   virtual CSObject *getNextLink() { return iNextLink; }
00108   virtual CSObject *getPrevLink() { return iPrevLink; }
00109   virtual void setNextLink(CSObject *link) { iNextLink = link; }
00110   virtual void setPrevLink(CSObject *link) { iPrevLink = link; }
00111 
00112 private:
00113   CSObject    *iNextLink;
00114   CSObject    *iPrevLink;
00115 };
00116 
00117 /*
00118  * Items are linked so that following the previous pointers
00119  * you move from front to back.
00120  * Following the next pointers you move from back to front.
00121  */
00122 class CSLinkedList : public CSObject {
00123 public:
00124   CSLinkedList(): iSize(0), iListFront(NULL), iListBack(NULL) { }
00125   virtual ~CSLinkedList() { clear(); }
00126 
00127   void clear();
00128   
00129   uint32_t getSize() { return iSize; }
00130 
00131   /* Value must be given referenced. */
00132   void addFront(CSObject *item);
00133   void addBack(CSObject *item);
00134 
00135   bool remove(CSObject *item);
00136 
00137   /* Value is returned referenced. */
00138   CSObject *removeBack();
00139 
00140   /* Value is returned NOT referenced. */
00141   CSObject *getBack();
00142 
00143   /* Value is returned NOT referenced. */
00144   CSObject *getFront();
00145 
00146   /* Value is returned referenced. */
00147   CSObject *removeFront();
00148 private:
00149   uint32_t iSize;
00150   CSObject *iListFront;
00151   CSObject *iListBack;
00152 };
00153 
00154 class CSSyncLinkedList : public CSLinkedList, public CSSync {
00155 public:
00156   CSSyncLinkedList(): CSLinkedList(), CSSync() { }
00157 };
00158 
00159 class CSVector : public CSObject {
00160 public:
00161   CSVector(uint32_t growSize): iGrowSize(growSize), iMaxSize(0), iUsage(0), iArray(NULL) { }
00162   virtual ~CSVector() { free(); }
00163 
00164   void free();
00165 
00166   void clear();
00167 
00168   /*
00169    * Remove and object from the vector, and
00170    * The object is rfemoved from the list.
00171    * return a reference.
00172    */
00173   CSObject *take(uint32_t idx);
00174 
00175   /*
00176    * Remove an object from the vector.
00177    */
00178   void remove(uint32_t idx);
00179 
00180   /*
00181    * Get a reference to an object in the vector.
00182    * A reference to the object remains on the list.
00183    * Value returned is NOT referenced!
00184    */
00185   CSObject *get(uint32_t idx);
00186 
00187   /* Set a specific index: */
00188   void set(uint32_t idx, CSObject *);
00189 
00190   /*
00191    * Add an object to the end of the vector.
00192    * Value must be referenced.
00193    */
00194   void add(CSObject *);
00195 
00196   uint32_t size() { return iUsage; }
00197 
00198 private:
00199   uint32_t iGrowSize;
00200   uint32_t iMaxSize;
00201   uint32_t iUsage;
00202 
00203   CSObject **iArray;
00204 };
00205 
00206 class CSSyncVector : public CSVector, public CSSync {
00207 public:
00208   CSSyncVector(uint32_t growSize): CSVector(growSize), CSSync() { }
00209 };
00210 
00211 typedef struct CSSpareArrayItem {
00212   uint32_t    sa_index;
00213   CSObject  *sa_object;
00214 } CSSpareArrayItemRec, *CSSpareArrayItemPtr;
00215 
00216 class CSSparseArray : public CSObject {
00217 public:
00218   CSSparseArray(uint32_t growSize): iGrowSize(growSize), iMaxSize(0), iUsage(0), iArray(NULL) { }
00219   CSSparseArray(): iGrowSize(10), iMaxSize(0), iUsage(0), iArray(NULL) { }
00220   virtual ~CSSparseArray() { free(); }
00221 
00222   void free();
00223 
00224   void clear();
00225 
00226   CSObject *take(uint32_t sparse_idx);
00227 
00228   void remove(uint32_t sparse_idx);
00229 
00230   void removeFirst();
00231 
00232   CSObject *itemAt(uint32_t idx);
00233 
00234   CSObject *get(uint32_t sparse_idx);
00235   
00236   uint32_t getIndex(uint32_t sparse_idx);
00237   
00238   void set(uint32_t sparse_idx, CSObject *);
00239 
00240   uint32_t size() { return iUsage; }
00241 
00242   uint32_t minIndex() {
00243     if (iUsage == 0)
00244       return 0;
00245     return iArray[0].sa_index;
00246   }
00247 
00248   uint32_t maxIndex() {
00249     if (iUsage == 0)
00250       return 0;
00251     return iArray[iUsage-1].sa_index;
00252   }
00253 
00254   CSObject *first();
00255 
00256   CSObject *last();
00257 
00258 private:
00259   uint32_t        iGrowSize;
00260   uint32_t        iMaxSize;
00261   uint32_t        iUsage;
00262   CSSpareArrayItemPtr iArray;
00263 
00264   CSObject *search(uint32_t idx, uint32_t& pos);
00265 };
00266 
00267 class CSSyncSparseArray : public CSSparseArray, public CSSync {
00268 public:
00269   CSSyncSparseArray(uint32_t growSize): CSSparseArray(growSize), CSSync() { }
00270 };
00271 
00272 class CSOrderKey : public CSObject {
00273 public:
00274   virtual int compareKey(CSOrderKey *key) = 0;
00275   int compareKey(CSObject *key) {return CSObject::compareKey(key);}
00276 };
00277 
00278 typedef struct CSOrderedListItem {
00279   CSOrderKey  *li_key;
00280   CSObject  *li_item;
00281 } CSOrderedListItemRec, *CSOrderedListItemPtr;
00282 
00283 class CSOrderedList : public CSObject {
00284 public:
00285   CSOrderedList(): iListSize(0), iInUse(0), iList(NULL) { }
00286   virtual ~CSOrderedList() { clear(); }
00287 
00288   void clear();
00289 
00290   /* Value must be given referenced. */
00291   void add(CSOrderKey *key, CSObject *item);
00292   
00293   /* Value is returned NOT referenced. */
00294   CSObject *find(CSOrderKey *key);
00295   
00296   /* Value is returned NOT referenced. */
00297   CSObject *itemAt(uint32_t idx);
00298   
00299   void remove(CSOrderKey *key);
00300 
00301 private:
00302   uint32_t iListSize;
00303 
00304   uint32_t iInUse;
00305 
00306   CSOrderedListItemPtr iList;
00307 
00308   CSOrderedListItemPtr search(CSOrderKey *key, uint32_t *idx);
00309 };
00310 
00311 class CSSyncOrderedList : public CSOrderedList, public CSSync {
00312 public:
00313   CSSyncOrderedList(): CSOrderedList(), CSSync() { }
00314 };
00315 
00316 class CSQueue;
00317 
00318 typedef struct CSQueueItem {
00319   CSObject      *qi_object;
00320   CSQueue       *qi_next;
00321 } CSQueueItemRec, *CSQueueItemPtr;
00322 
00323 class CSQueue : public CSObject {
00324 public:
00325   CSQueue(): iQueueSize(0), iHighWater(0), iFront(NULL), iBack(NULL), iFree(NULL) { }
00326   virtual ~CSQueue() { clear(); }
00327 
00328   void clear();
00329 
00330   /* Value must be given referenced. */
00331   void add(CSObject *item);
00332 
00333   /* Returns a referenced value, on a FIFO basis. */
00334   CSObject *remove();
00335 
00336 private:
00337   uint32_t iQueueSize;
00338   uint32_t iHighWater;
00339 
00340   CSQueueItemPtr  iFront;
00341   CSQueueItemPtr  iBack;
00342   CSQueueItemPtr  iFree;
00343 };
00344 
00345 #endif