mi32/btreefun.h

Go to the documentation of this file.
00001 /**
00002  * \file btreefun.h <mi32/btreefun.h>
00003  * \brief Definitions for BTree functions
00004  *
00005  * \if NODOC
00006  * $Log: btreefun.h_v $
00007  * Revision 1.30  2006/09/18 14:35:27  scowan
00008  * Added necessary include file.
00009  *
00010  * Revision 1.29  2005/12/29 21:35:22  scowan
00011  * Removed the reference.
00012  *
00013  * Revision 1.28  2005/12/29 20:31:58  scowan
00014  * Added code to allocate a test and delete when done.
00015  *
00016  * Revision 1.27  2005/08/29 19:14:07  scowan
00017  * Fixed deprecated item.
00018  *
00019  * Revision 1.26  2003/12/18 13:59:55  mju
00020  * Remove deprecated items.
00021  *
00022  * Revision 1.25  2003/09/15 13:49:56  fileserver!dwilliss
00023  * Doxygen
00024  *
00025  * Revision 1.24  2003/09/08 20:33:39  dwilliss
00026  * Added doxygen tags for start/end of function groups
00027  *
00028  * Revision 1.23  2003/05/12 14:05:21  mju
00029  * Fix deprecated macro.
00030  *
00031  * Revision 1.22  2003/05/12 13:48:05  mju
00032  * Deprecate obsolete Init/Stop fns.
00033  *
00034  * Revision 1.21  2003/04/09 15:10:17  scowan
00035  * Fixed g++ 3.0 unix compiler error for mac osx.
00036  *
00037  * Revision 1.20  2003/03/20 15:48:17  scowan
00038  * Added is valid method.
00039  *
00040  * Revision 1.19  2003/01/27 17:16:22  scowan
00041  * Fixed more typo's.
00042  *
00043  * Revision 1.18  2003/01/27 16:44:56  scowan
00044  * Fixed wrapper class issues.
00045  *
00046  * Revision 1.17  2003/01/22 16:54:16  scowan
00047  * Fixed compile errors with wrapper class.
00048  *
00049  * Revision 1.16  2003/01/22 15:59:13  scowan
00050  * Added inline template wrapper, do not know if it works.
00051  *
00052  * Revision 1.15  2001/01/23 16:48:13  dwilliss
00053  * Had forgotten to export new function from the DLL
00054  * \endif
00055 **/
00056 
00057 #ifndef  INC_MI32_BTREEFUN_H
00058 #define  INC_MI32_BTREEFUN_H
00059 
00060 #ifndef  INC_MI32_STDDEFNS_H
00061 #include <mi32/stddefns.h>
00062 #endif
00063 
00064 #ifndef  INC_MI32_ERRCODES_H
00065 #include <mi32/errcodes.h>
00066 #endif
00067 
00068 #ifndef  INC_MI32_ERRHANDLER_H
00069 #include <mi32/errhandler.h>
00070 #endif
00071 
00072 #ifdef GEOMDLL
00073    #define GEOMLIBEXPORT MI_DLLEXPORT
00074 #else
00075    #define GEOMLIBEXPORT MI_DLLIMPORT
00076 #endif
00077 
00078 //----------------------------------------------------------------------------
00079 //!      Function prototypes
00080 //----------------------------------------------------------------------------
00081 
00082 //!:Associate with "Btree Functions"
00083 //!\addtogroup btree Btree Functions
00084 //!@{
00085 
00086 //! Tree comparison function used in searching/adding.
00087 //!
00088 //! @return >, ==, < 0.
00089 typedef int (*BTreeTestFunc)(
00090    void*, 
00091    void*, 
00092    void*
00093    );
00094 
00095 //! Callback used to return data. 
00096 typedef int (*BTreeDumpFunc)(
00097    void *data,                         //!< Data stored in the tree
00098    INT32 *tagnum,                      //!< Pointer to tag number
00099    void *username                      //!< Pointer to sent data
00100    );
00101 
00102 #if defined(__cplusplus)
00103 extern "C" {
00104 #endif
00105 
00106 //! Allocate a "balanced tree".
00107 //!
00108 //! @return Handle to tree instance.                              
00109 //!
00110 //! Note.  At the current time, it is assumed that the test function may
00111 //! modify the data, forcing the tree to do more work that it should
00112 //! have to.  You should not rely on this assumption, as it was an obscure,
00113 //! undocumented side effect and likely to change some day.  Instead, you
00114 //! should call BTreeSetTestFunc() and tell it if your test function can
00115 //! modify the data.
00116 //!
00117 GEOMLIBEXPORT void *BTreeAlloc (
00118    int numitems,                       //!< Number of items to store per node 
00119    int datasize,                       //!< Size of data to tree in bytes
00120    BTreeTestFunc testfunc,                      //!< Comparison function used in searching/adding
00121    void *userdata                      //!<  Data passed to comparison function
00122    );
00123 
00124 //! Clears item from "balanced tree".
00125 GEOMLIBEXPORT int BTreeClear (
00126    void *item                          //!<  Item to be cleared from tree
00127    );
00128 
00129 //! Dump a "balanced tree" in "random" order.
00130 //!
00131 //! NOTE: This function transverses the tree as it is stored, no order is at all guaranteed!
00132 GEOMLIBEXPORT int BTreeDump (
00133    void *tree,                         //!< Handle to tree instance
00134    BTreeDumpFunc,                      //!< Callback used to return data
00135    void *userdata                      //!< User data passed to Dump()
00136    );
00137 
00138 //! Dump a "balanced tree" in "traversal" order.
00139 //!
00140 //! NOTE: This function transverses the tree and dumps the data in the
00141 //!   order that the comparison function (see BTreeAlloc()) stipulated.
00142 GEOMLIBEXPORT int BTreeDumpT (
00143    void *tree,                         //!< Handle to tree instance
00144    BTreeDumpFunc,                      //!< Callback used to return data
00145    void *userdata                      //!< User data passed to Dump()
00146    );
00147 
00148 //! Search "balanced tree" for a specified key, add if no match found.
00149 //!
00150 //! @return tag number assigned to the data if (>=0), else error
00151 GEOMLIBEXPORT INT32 BTreeFindKey (
00152    void *tree,                         //!< Handle to tree instance
00153    void *data,                         //!< Data to find and add if necessary
00154    INT32 tagnum,                       //!< Number to tag data with
00155    int *add                            //!< Set if data was added to tree
00156    );
00157 
00158 //! Free a "balanced tree".
00159 GEOMLIBEXPORT int BTreeFree (
00160    void *tree                          //!< Handle to tree instance
00161    );
00162 
00163 //! Get the userdata associated with 'tree'
00164 GEOMLIBEXPORT void* BTreeGetUserData (
00165    void *tree                          //!< Handle to tree instance
00166    );
00167 
00168 //! Change test function for a "balanced tree".
00169 GEOMLIBEXPORT int BTreeNewFunc (
00170    void *tree,                         //!< Handle to tree instance
00171    BTreeTestFunc,                      //!< Comparison function used in searching/adding
00172    void *userdata                      //!< Data passed to comparison function
00173    );
00174 
00175 //! Search "balanced tree" for a specified key.
00176 //!
00177 //! @return -1 = Not found, < -1 Error, else the tag number associated with the data.
00178 //!   See BTreeFindKey(). 
00179 GEOMLIBEXPORT INT32 BTreeSearchKey (
00180    void *tree,                         //!< Handle to tree instance
00181    void *data                          //!< Data to find if exists
00182    );
00183 
00184 
00185 //! Change test function for a "balanced tree".
00186 //! This is similar to BTreeNewFunc, but allows you to tell the BTree that the
00187 //! function won't modify the data it's passed.
00188 //! The default behaviour for btrees is to assume that the test function
00189 //! can modify the data passed.
00190 GEOMLIBEXPORT void BTreeSetTestFunc (
00191    void *btree,
00192    BTreeTestFunc Test,
00193    void *userdata,
00194    bool TestFuncIsConst
00195    );
00196 
00197 #if defined(__cplusplus)
00198 } // extern "C"
00199 #endif
00200 
00201 //!@}
00202 
00203 //! Wrapper class to the BTree API
00204 template <typename _BT>
00205 class BALANCEDTREE {
00206    public:
00207       //! Interface class to "dump" an item from the tree, used in DumpTree() and TraverseTree()
00208       class DUMP {
00209          public:
00210             virtual ERRVALUE v_DumpItem (const _BT& item, INT32 TagNum) = 0;
00211          };
00212    
00213       //! Interface class to test two tree items, can use default if _T::operator<() defined
00214       class TEST {
00215          public:
00216             virtual ~TEST () {}
00217             virtual int v_TestItems (const _BT& p1, const _BT& p2) { return ((p1 < p2) ? -1 : ((p2 < p1) ? 1 : 0)); }
00218          };
00219    
00220    private:
00221       #ifndef GENERATING_DOXYGEN_OUTPUT
00222       static int DumpFunc (void* p1, INT32* tagnum, void* ud) {
00223          return (static_cast<DUMP*>(ud)->v_DumpItem(*static_cast<_BT*>(p1), *tagnum));
00224          }
00225    
00226       static int TestFunc (void* p1, void* p2, void* ud) {
00227          return (static_cast<TEST*>(ud)->v_TestItems(*static_cast<_BT*>(p1), *static_cast<_BT*>(p2)));
00228          }
00229       #endif // GENERATING_DOXYGEN_OUTPUT
00230    
00231    public:
00232 
00233       //! Constructor   
00234       BALANCEDTREE (
00235          ) : m_BTreeHandle(0) {}
00236          
00237       //! Destructor 
00238       ~BALANCEDTREE (
00239          ) { 
00240          if (m_BTreeHandle != 0) {
00241             delete static_cast<TEST*>(BTreeGetUserData(m_BTreeHandle));
00242             BTreeFree(m_BTreeHandle); 
00243             }
00244          }
00245 
00246       //! Initialize the balanced tree giving an initial number of items
00247       ERRVALUE Allocate (
00248          INT32 InitialNumItems,
00249          TEST Test = TEST()
00250          ) {
00251          m_BTreeHandle = BTreeAlloc(InitialNumItems, sizeof(_BT), TestFunc, new TEST(Test));
00252          if (m_BTreeHandle == 0) return (SetErrPosnC(EOutOfMemory));
00253          return (0);
00254          }
00255          
00256       //! Clear the balanced tree of all items
00257       ERRVALUE Clear (
00258          ) { return (BTreeClear(m_BTreeHandle)); }
00259          
00260       //! Dump the balanced tree in no particular order
00261       ERRVALUE DumpTree (
00262          DUMP& Dump
00263          ) { return (BTreeDump(m_BTreeHandle, DumpFunc, &Dump)); }
00264          
00265       //! Locate an item in the tree and return its key value.
00266       //! If the item does not exist, add it to the tree
00267       INT32 FindAndInsertKey (
00268          _BT& Item,
00269          INT32 CurTagNum,        //!< Tag number to use if 'Item' is added to the tree
00270          bool& AddedItem         //!< Set to 'true' if 'Item' is added to the tree
00271          ) { 
00272          int add = 0;
00273          INT32 retval = BTreeFindKey(m_BTreeHandle, &Item, CurTagNum, &add);
00274          AddedItem = (add != 0);
00275          return (retval);
00276          }
00277 
00278       //! Determine if the balanced tree Allocate() method has been called
00279       bool IsAllocated (
00280          ) const { return (m_BTreeHandle != 0); }
00281          
00282       //! Locate an item in the tree and return its key value
00283       INT32 LocateKey (
00284          const _BT& Item
00285          ) { return (BTreeSearchKey(m_BTreeHandle, const_cast<_BT*>(&Item))); } 
00286          
00287       //! Set a new test function for locating tree items
00288       void SetTestFunc (
00289          TEST& NewTest
00290          ) { 
00291          delete static_cast<TEST*>(BTreeGetUserData(m_BTreeHandle));
00292          BTreeSetTestFunc(m_BTreeHandle, TestFunc, new TEST(NewTest), true); 
00293          }
00294          
00295       //! Dump the balanced tree in sorted order
00296       ERRVALUE TraverseTree (
00297          DUMP& Dump
00298          ) { return (BTreeDumpT(m_BTreeHandle, DumpFunc, &Dump)); }
00299          
00300    private:
00301       #ifndef GENERATING_DOXYGEN_OUTPUT
00302       void *m_BTreeHandle; 
00303 
00304       BALANCEDTREE (const BALANCEDTREE& rhs);
00305       BALANCEDTREE& operator=(const BALANCEDTREE& rhs);
00306       #endif // GENERATING_DOXYGEN_OUTPUT
00307    };
00308 
00309 #endif
00310 

Generated on Thu Apr 26 04:44:53 2007 for TNTsdk by  doxygen 1.5.2