Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

Database.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001-2005 Replica Software
00005 
00006 This program file is copyright (C) Replica Software and can only be used under license.
00007 For more information visit: http://www.replicanet.com/
00008 Or email: info@replicanet.com
00009 
00010 END_LICENSE_HEADER */
00011 #ifndef __DATABASE_H__
00012 #define __DATABASE_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 
00015 namespace RNReplicaNet
00016 {
00017 
00018 typedef int t_HashFromDataFunc(void *pData);
00019 typedef int t_HashFromKeyFunc(void *pKey);
00020 typedef bool t_CompareDataWithKeyFunc(void *pData,void *pKey);
00021 
00022 // This should always be 32 to use the full 32 bits of a 32 bit integer as a bit array
00023 const int kSubNodes = 32;
00024 
00025 class DatabaseNode;
00026 struct DatabaseLinkNode;
00027 class DatabaseLinkNodePool;
00028 
00029 class REPNETEXPORTAPI Database
00030 {
00031 friend class DatabaseNode;
00032 public:
00033     enum Direction
00034     {
00035         kForward=0,
00036         kBackward,
00037         kUndefined
00038     };
00039 
00040     Database();
00041     virtual ~Database();
00042 
00043     void *AddItem(void *data);
00044     void *AddItemHead(void *data);
00045     void *FindItem(void *keyData);
00046     bool RemoveItem(void *data);
00047     bool RemoveItem(void);
00048 
00049     void BeginIterate(void);
00050     void EndIterate(void);
00051     void *Iterate(void);
00052 
00053     void SetHashFromDataFunc(t_HashFromDataFunc *pfunc);
00054     void SetHashFromKeyFunc(t_HashFromKeyFunc *pfunc);
00055     void SetCompareDataWithKeyFunc(t_CompareDataWithKeyFunc *pfunc);
00056 
00057     void SetEnableFastFind(bool enable = false);
00058     bool GetEnableFastFind(void)
00059     {
00060         return mEnableFastFind;
00061     }
00062 
00063     int Tidy(void);
00064 
00065 private:
00066     bool mEnableFastFind;
00067     Direction mDirection;
00068     bool mUsed;
00069 
00070     DatabaseNode *MatchNode(int objectHash,bool create);
00071     void SemiFreeDatabaseLinkNode (DatabaseLinkNode *theLinkNode);
00072 
00073     t_HashFromDataFunc *mHashFromDataFunc;
00074     t_HashFromKeyFunc *mHashFromKeyFunc;
00075     t_CompareDataWithKeyFunc *mCompareDataWithKeyFunc;
00076 
00077     // The start of the root node TREE, the mParent should always be null for example
00078     DatabaseNode *mTreeDatabaseNodeRoot;
00079     // The start of the link list of allocated root nodes, handy for stepping through when freeing for example
00080     // Depending on the order of allocation the mParent might not be null
00081     DatabaseNode *mLinkDatabaseNodeRoot;
00082 
00083     // This pointer is used in the Tidy() routine that goes through the list looking for nodes to tidy
00084     // To avoid too much time being used up it only does a certain number of nodes per Tody() call
00085     DatabaseNode *mDatabaseNodeTidyIterator;
00086 
00087     // The iterator values used in the iterator
00088     DatabaseLinkNode *mIterator;
00089 
00090     // The backup previous values for the iterator
00091     DatabaseLinkNode *mPrevIterator;
00092 
00093     // The last find puts the entries in here so a subsequent remove (data*) is quicker
00094     DatabaseNode *mFindCache;
00095     int mFindCacheCounter;
00096 
00097     // The actual list head and tail used for the iterate LinkNode iterate
00098     // A totally empty list should have null for both
00099     DatabaseLinkNode *mLinkNodeRoot;
00100     DatabaseLinkNode *mLinkNodeTail;
00101 
00102     // The head list pointers of cached pools to avoid too many small allocs that std::list uses
00103     // A totally empty list should have null pointers for both
00104     DatabaseLinkNodePool *mPoolRootNode;
00105     DatabaseLinkNodePool *mPoolFreeRootNode;
00106 
00107     // The number of nodes not immediatly deleted and that contain no entries...
00108     int mNumNodesWithZeroEntries;
00109 };
00110 
00111 class REPNETEXPORTAPI DatabaseNode
00112 {
00113 friend class Database;
00114 public:
00115     DatabaseNode(DatabaseNode *parent,Database *theDatabase);
00116     virtual ~DatabaseNode();
00117 
00118 protected:
00119     void AddItem(void *data,DatabaseLinkNode *theLinkNode);
00120 
00121     int mHash;
00122     DatabaseNode *mParent;
00123     DatabaseNode *mGreaterThan;
00124     DatabaseNode *mLessThan;
00125     DatabaseNode *mSame;
00126 
00127     DatabaseNode *mLinkNext;
00128     DatabaseNode *mLinkPrev;
00129 
00130     Database *mDatabase;
00131 
00132     unsigned int mBitMask;
00133     void *mData[kSubNodes];
00134     DatabaseLinkNode *mTheLinkNode[kSubNodes];
00135 };
00136 
00137 struct REPNETEXPORTAPI DatabaseLinkNode
00138 {
00139     DatabaseLinkNode *mNext;
00140     DatabaseLinkNode *mPrev;
00141     void *mData;
00142 
00143     // The pools of link list entries
00144     DatabaseLinkNodePool *mOwnerPool;
00145     int mOwnerEntry;
00146 
00147     // The values for the database lookup tree
00148     DatabaseNode *mDatabaseNode;
00149     int mDatabaseEntry;
00150 };
00151 
00152 class REPNETEXPORTAPI DatabaseLinkNodePool
00153 {
00154 public:
00155     DatabaseLinkNodePool();
00156     virtual ~DatabaseLinkNodePool();
00157 
00158     DatabaseLinkNodePool *mNext;
00159     DatabaseLinkNodePool *mPrev;
00160 
00161     DatabaseLinkNodePool *mNextFree;
00162     DatabaseLinkNodePool *mPrevFree;
00163 
00164     unsigned int mBitMask;
00165     DatabaseLinkNode mNodes[kSubNodes];
00166 };
00167 
00168 } // namespace RNReplicaNet
00169 
00170 
00171 #endif

Generated on Sun Oct 30 01:12:31 2005 for Platform by  doxygen 1.4.1