00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
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
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
00081 TabHd& operator=(const TabHd&);
00082 };
00083
00084
00085
00086
00087
00088
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
00330
00331 #define MakeTab(TYPE) typedef Tab<TYPE> __tab_name2(TYPE, Tab);
00332
00333
00334 UtilExport void TabStartRecording();
00335 UtilExport void TabStopRecording();
00336 UtilExport void TabPrintAllocs();
00337 UtilExport void TabAssertAllocNum(int i);
00338