uLib  User mode C/C++ extended API library for Win32 programmers.
DynArray.h
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode library.
3 // Module: DynArray -- Dynamic arrays in C++
4 // Author: Copyright (c) Love Nystrom 1999
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 // Credits: Unknown @ Borland International (concept and original Pascal class).
7 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 
9 #ifndef _DynArray_h_incl_
10 #define _DynArray_h_incl_
11 
12 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) // 3 idents I've seen
13 #include <uLib/UtilFunc.h>
14 #else // Non-Windows compilation
15 #error "STOP: This version of DynArray requires the HeapAlloc API"
16 #endif
17 
19 
20 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 
34 
35 #if USE_DYNARRAY_LIMIT
36  #ifndef MAX_DYNITEM
37  #define MAX_DYNITEM 262144L // Limit items pointer block to 1 MB (2 MB on x64)
38  //#define MAX_DYNITEM 1048576L // Max 4 MB of pointers (8 MB on x64)
39  #endif
40 #endif
41 
42 typedef class DynArray * PDynArray;
43 typedef bool (CALLBACK *DYN_ITEMFUNC)( PVOID Item, PVOID UserData );
44 
45 //++ Class DynArray -----------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 
51 class DynArray {
52 public:
53  DWORD const & Count;
54  DWORD const & Size;
55  DynArray const & Items;
56 
58  DynArray( DWORD Size=16, WORD Delta=16 );
61 
62  virtual ~DynArray();
64 
65  void Init( DWORD Size, WORD Delta );
66  void Done();
67 
68  PVOID operator +=( PVOID Item ) { return Insert( Item ); }
69  PVOID operator -=( PVOID Item ) { return Remove( Item ); }
70 
71  PVOID Insert( PVOID Item );
75 
76  PVOID InsertAt( DWORD Index, PVOID Item );
82 
86 
87  PVOID Push( PVOID Item );
88  PVOID Pull( void );
89  PVOID Pop( void );
90 
91  virtual void DeleteItem( PVOID & Item );
95 
96  virtual void DeleteAll( void ); // AFAIK: Must be virtual to follow DeleteItem
98 
99  void Delete( PVOID& Item );
100  void Delete( DWORD Index );
103 
104  PVOID Remove( PVOID Item );
105  PVOID Remove( DWORD Index );
109 
110  LONG IndexOf( PVOID Item ) const;
112 
113  PVOID At( DWORD Index ) const;
115 
116  PVOID FirstThat( DYN_ITEMFUNC Match, PVOID userData ) const;
122 
123  PVOID LastThat( DYN_ITEMFUNC Match, PVOID userData ) const;
125 
126  bool ForEach( DYN_ITEMFUNC Action, PVOID userData ) const;
132 
133  PVOID operator[]( DWORD Index ) const { return At( Index ); }
135 
137 protected:
138  DWORD _count, _size;
139  WORD _delta;
140  PVOID* _items; // array of pointers
141 private:
142  // DynArray uses private allocation method for consistency and control.
143  // Uses mem_Alloc et alumni.
144  static void* __alloc( size_t bytes );
145  static void* __realloc( void* blk, size_t bytes );
146  static void* __free( void* blk );
148 };
149 
150 //-----------------------------------------------------------------------------
151 // SortDynArray -- Sorted dynamic array.
152 //-----------------------------------------------------------------------------
153 
154 typedef class SortDynArray * PSortDynArray;
156 class SortDynArray : public DynArray {
157 public:
158  bool Duplicates;
159 
160  SortDynArray( DWORD Size=16, WORD Delta=16 );
162 
163  PVOID operator +=( PVOID Item ) { return Insert( Item ); }
165 
166  virtual PVOID KeyOf( PVOID Item );
169 
170  virtual short Compare( PVOID Key1, PVOID Key2 );
178 
179  bool Search( PVOID Key, DWORD& Index );
185 
186  PVOID Insert( PVOID Item );
189 };
190 
193 #endif //ndef _DynArray_h_incl_
194 // EOF
unsigned long DWORD
Definition: Common.h:414
unsigned short WORD
Definition: Common.h:413
DWORD const & Size
Definition: DynArray.h:54
#define END_NAMESPACE(name)
Definition: Common.h:225
PVOID operator[](DWORD Index) const
Definition: DynArray.h:133
bool(CALLBACK * DYN_ITEMFUNC)(PVOID Item, PVOID UserData)
Definition: DynArray.h:43
Definition: DynArray.h:18
DWORD const & Count
Definition: DynArray.h:53
#define BEGIN_NAMESPACE(name)
Definition: Common.h:224
class DynArray * PDynArray
Definition: DynArray.h:42
PVOID operator -=(PVOID Item)
Definition: DynArray.h:69
class SortDynArray * PSortDynArray
Definition: DynArray.h:154
DynArray const & Items
Definition: DynArray.h:55