home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWDISKTREE_H__
- #define __RWDISKTREE_H__
- pragma push_align_members(64);
-
- /*
- * Declarations for B-Trees on disk.
- *
- * $Header: E:/vcs/rw/disktree.h_v 1.3 18 Feb 1992 09:54:16 KEFFER $
- *
- ****************************************************************************
- *
- * Rogue Wave
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-3010 FAX: (503) 757-6650
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/disktree.h_v $
- *
- * Rev 1.3 18 Feb 1992 09:54:16 KEFFER
- *
- * Rev 1.2 28 Oct 1991 09:08:12 keffer
- * Changed inclusions to <rw/xxx.h>
- *
- * Rev 1.1 28 Jul 1991 12:36:04 keffer
- * No longer uses macro "Const"
- *
- * Rev 1.0 28 Jul 1991 08:14:14 keffer
- * Tools.h++ V4.0.5 PVCS baseline version
- *
- */
-
- #include "rw/tooldefs.h"
- #include "rw/filemgr.h"
-
- // Order is half the maximum number of keys per mode.
- // Setting it higher will reduce disk accesses, but increase
- // stack size requirements and CPU computations. Suggested size is 10.
- const int order = 10;
- const int twoTimesOrder = 2*order;
- const int KEY_SIZE = 16;
-
- typedef void (*applyRWBTreeOnDisk)(const char*, storedValue, void*);
-
- class RWDiskTreeNode {
- friend class RWExport RWBTreeOnDisk;
-
- // Private constructors:
- RWDiskTreeNode(); // Construct a node with no contents.
- RWDiskTreeNode(const char* key, storedValue); // Construct node with one item.
-
- // Private data:
- int counter; // How many of the [twoTimesOrder] fields are used.
- nodeOffset next[twoTimesOrder+1]; // Array of offsets to children nodes.
- storedValue item[twoTimesOrder]; // Array of offsets to stored objects.
- char key[twoTimesOrder][KEY_SIZE]; // Array of keys.
-
- // Private member function:
- int binarySearch(const char*) const; // Binary search for insertion.
- };
-
- class RWExport RWBTreeOnDisk {
- private:
- RWDiskTreeNode root; // Root node (in memory).
- nodeOffset rootOffset; // Offset to root node on disk.
- RWFileManager* file; // File manager for B-tree on disk.
- RWDiskTreeNode workNode; // Work space for node (in memory).
- nodeOffset workOffset; // Offset to work node on disk. Flag for node in work space
- storedValue newa; // Place holder to handle node over- and underflow.
- char newk[KEY_SIZE]; // Place holder to handle node over- and underflow.
- nodeOffset newn; // Place holder to handle node over- and underflow.
- private:
- void countChildren(nodeOffset, int& nitems); // Count children.
- void deleteChildren(nodeOffset); // Delete nodes.
- void diskTreeErr(RWErrNo) const;
- void freeNode(nodeOffset); // Free up space on disk;
- RWDiskTreeNode* getNode(nodeOffset);
- int insertInChildren(const char* key, storedValue, nodeOffset);
- void operateOnChildren(nodeOffset, applyRWBTreeOnDisk, void*);
- void putNode(RWDiskTreeNode*);
- void readNode(RWDiskTreeNode*, nodeOffset p=RWNIL);
- int removeFromChildren(const char* key, nodeOffset);// Remove key from tree.
- // Write at position p. Return offset, or RWNIL if unsuccessful.
- nodeOffset writeNode(RWDiskTreeNode*, nodeOffset p=RWNIL);
- void writeRootOffset();
- RWBTreeOnDisk(const RWBTreeOnDisk&); // Cannot have 2 RWBTreeOnDisk for the same file.
- void operator=(const RWBTreeOnDisk&);
- public:
- RWBTreeOnDisk(RWFileManager&);
-
- void clear(); // Remove all nodes.
- int entries(); // Return number of items.
- void applyToKeyAndValue(applyRWBTreeOnDisk, void*); // Operate on all items
- storedValue find(const char* key); // Return item where compare = 0
- void findKeyAndValue(const char* key, storedValue&); // Alternative find.
- int height(); // Return height of tree.
- int insertKeyAndValue(const char* key, storedValue p); // Add key & value.
- int isEmpty() const {return rootOffset==RWNIL;}// Return 1 for empty tree.
- void remove(const char* key); // Remove item where compare = 0.
- };
-
- pragma pop_align_members();
- #endif /* __RWDISKTREE_H__ */
-