tab.h

Go to the documentation of this file.
00001 //**************************************************************************/
00002 // Copyright (c) 1998-2006 Autodesk, Inc.
00003 // All rights reserved.
00004 // 
00005 // These coded instructions, statements, and computer programs contain
00006 // unpublished proprietary information written by Autodesk, Inc., and are
00007 // protected by Federal copyright law. They may not be disclosed to third
00008 // parties or copied or duplicated in any form, in whole or in part, without
00009 // the prior written consent of Autodesk, Inc.
00010 //**************************************************************************/
00011 // DESCRIPTION: Declaration of a generic "table" class
00012 // AUTHOR: Dan Silva, September 13, 1994
00013 //***************************************************************************/
00014 
00015 
00016 /*-------------------------------------------------------------------------------
00017 
00018  A Generic "Table" class.      
00019     
00020     (DSilva 9-13-94)
00021 
00022     This is a type-safe variable length array which also supports list-like
00023     operations of insertion, appending and deleting.  Two instance variables
00024     are maintained: "nalloc" is the number items allocated in the
00025     array; "count" is the number actual used. (count<=nalloc).
00026     Allocation is performed automatically when Insert or Append operations
00027     are performed.  It can also be done manually by calling Resize or Shrink.
00028     Note: Delete does not resize the storage: to do this call Shrink().  
00029     If you are going to do a sequence of Appends, it's more efficient to 
00030     first call Resize to make room for them.  Beware of using the Addr 
00031     function: it returns a pointer which may be invalid after subsequent 
00032     Insert, Append, Delete, Resize, or Shrink operations.  
00033     
00034     
00035     The implementation minimizes the storage of empty Tables: they are
00036     represented by a single NULL pointer.  Also, the major part of the
00037     code is generic, shared by different Tabs for different types of items.
00038 
00039 ------------------------------------------------------------------------------*/
00040 
00041 #pragma once
00042 
00043 #include "maxheap.h"
00044 #include <malloc.h>
00045 #include <stdlib.h>
00046 #include <wtypes.h>
00047 #include "utilexp.h"
00048 #include "assert1.h"
00049 
00050 typedef int CNT;
00051 
00052 struct TabHdr: public MaxHeapOperators 
00053 {
00054     CNT count;
00055     CNT nalloc;
00056 };
00057 
00059 // Functions for internal use only: Clients should never call these.
00060 //
00061 UtilExport int TBMakeSize(TabHdr** pth, int num, int elsize); 
00062 UtilExport int TBInsertAt(TabHdr** pth, int at, int num, void *el, int elsize, int extra); 
00063 UtilExport int TBCopy(TabHdr** pth, int at, int num, void *el, int elsize); 
00064 UtilExport int TBDelete(TabHdr** pth, int starting, int num, int elsize);
00065 UtilExport void TBSetCount(TabHdr** pth, int n, int elsize, BOOL resize);
00066 UtilExport void zfree(void**p);
00068 
00069 #define NoExport
00070         
00071 template <class T> class NoExport TabHd: public MaxHeapOperators 
00072 {
00073     public:
00074         CNT count;
00075         CNT nalloc;
00076         T data[100];
00077         TabHd() { 
00078             count = 0; nalloc = 0; 
00079         }
00080         // Unimplemented assignment operator
00081         TabHd& operator=(const TabHd&);
00082 };
00083 
00084 
00085 // Type of function to pass to Sort.
00086 // Note: Sort just uses the C lib qsort function. If we restricted
00087 // all Tab items to have well defined <,>,== then we wouldn't need
00088 // this callback function.
00089 typedef int( __cdecl *CompareFnc) ( const void *elem1, const void *elem2 );
00090 
00092 
00154 template <class T> class NoExport Tab: public MaxHeapOperators 
00155 {
00156     private:
00157         TabHd<T> *th;
00158 
00159     public:
00161         Tab() : th(0) { }
00162         
00164 
00166         Tab(const Tab& tb) : th(0) {  
00167             TBCopy((TabHdr**)&th, 0, tb.Count(), (tb.th ? &tb.th->data : NULL), sizeof(T)); 
00168         }
00169 
00171 
00174         ~Tab() {
00175             zfree((void**)&th); 
00176         }
00177 
00179 
00182         void Init() {
00183             th = 0;
00184         }
00185 
00187 
00189         int Count() const 
00190         { 
00191             if (th) {
00192                 return (th->count); 
00193             }
00194             return 0; 
00195         }  
00196 
00198 
00201         void ZeroCount() 
00202         { 
00203             if (th) {
00204                 th->count = 0; 
00205             }
00206         }
00207         
00209 
00212         void SetCount(int n, BOOL resize=TRUE) { 
00213             TBSetCount((TabHdr **)&th, n, sizeof(T), resize); 
00214         }
00215 
00217 
00222         T* Addr(const INT_PTR i) const {             
00223             DbgAssert(th);
00224             DbgAssert(i < th->count); 
00225             return (&th->data[i]); 
00226         }
00227 
00229 
00234         int Insert(int at, int num, T *el) {
00235             return (TBInsertAt((TabHdr**)&th, at, num, (void *)el, sizeof(T), 0));
00236         }
00237 
00239 
00245         int Append(int num, T *el, int allocExtra=0) {
00246             return (TBInsertAt((TabHdr**)&th, (th ? th->count : 0), num, (void *)el, sizeof(T), allocExtra)); 
00247         }
00248         
00250 
00254         int Delete(int start, int num) { 
00255             return (TBDelete((TabHdr**)&th, start, num, sizeof(T)));
00256         } 
00257         
00259 
00268         int Resize(int num) { 
00269             return (TBMakeSize((TabHdr**)&th, num, sizeof(T)));
00270         }   
00271         
00273         void Shrink() {
00274             TBMakeSize((TabHdr**)&th, (th ? th->count : 0), sizeof(T)); 
00275         }
00276 
00278 
00297         void Sort(CompareFnc cmp) 
00298         {
00299             if (th) {
00300                 qsort(th->data, th->count, sizeof(T), cmp);
00301             }
00302         }
00303 
00305 
00310         Tab& operator=(const Tab& tb) {
00311             TBCopy((TabHdr**)&th, 0, tb.Count(), (tb.th ? &tb.th->data : NULL), sizeof(T)); 
00312             return *this;
00313         }
00314 
00316 
00319         T& operator[](const INT_PTR i) const {       
00320             DbgAssert(th);
00321             DbgAssert(i < th->count); 
00322             return (th->data[i]); 
00323         }
00324 };
00325 
00326 #ifndef __tab_name2
00327 #define __tab_name2(a, b) a##b
00328 #endif
00329 //#pragma deprecated("__tab_name2")
00330 
00331 #define MakeTab(TYPE) typedef Tab<TYPE> __tab_name2(TYPE, Tab);                                                             
00332 //#pragma deprecated("MakeTab")
00333 
00334 UtilExport void TabStartRecording();
00335 UtilExport void TabStopRecording();
00336 UtilExport void TabPrintAllocs();
00337 UtilExport void TabAssertAllocNum(int i);
00338