I've come up with a resource manager tree structure that I want advice on.
The concept is simple. Each object has a unique ID. The objects are sorted into a tree using the following:
- If the object ID is greater than the current node's ID, use the current node's right child. If there is no right child, create a new node using the object ID and set it to be the right child of the current node. Add this ID to a double linked list.
- If the object ID is less than the current node's ID, use the current node's left child. If there is no left child, create a new node using the object ID and set it to be the left child of the current node. Add this ID to a double linked list.
The doubly linked list is a simple list of all the ID's that are in the tree. The list is not used to find the object's in the tree but it is used to cache out the least recently used objects. So when a new object is inserted into the tree, it is also inserted into the top of the list. If the list size is larger than a pre-set cache size, the bottommost list member is removed. If the object in the list is requested from the tree and the object is in the tree...and thus in the list...the object in the list is moved to the TOP of the list.
What this amounts to is that the most used objects or resources will be in the list and thus in the tree. I'm using the tree as a very fast way to find objects and I'm using the list as a way to cache them in or out. Worst case is that the requested ID is not in the tree and thus returns NULL at which point the calling function will need to create the object and insert it into the tree (and then this puts it at the top of the list).
I'm storing a pointer to the object in the list in the node so that the object in the list can be re-ordered using the data in the node. The list never needs to be searched and should never be since it is O(n).
Comments?Code:struct TreeNode { CResource *pObj; ListNode *pListPointer; TreeNode *pParent; TreeNode *pLeft; TreeNode *pRight; }; struct ListNode { CResource *pObj; ListNode *Prev; ListNode *Next; }; class CResource { protected: DWORD m_dwID; public: ... ... }; class CResMgr { private: void Insert(CResource *pObj,TreeNode *pCurNode,bool bInserted); CResource *Find(DWORD dwID,TreeNode *pCurNode,bool bFound); protected: TreeNode *pRoot; ListNode *pTop; ListNode *pBottom; public: ... ... CResource *Get(DWORD dwID); DWORD Add(CResource *pObj); ... ... };



LinkBack URL
About LinkBacks






CornedBee