00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_GARRAY_H__
00021 #define __CS_GARRAY_H__
00022
00023
00024 #define CS_TYPEDEF_GROWING_ARRAY_EXT(Name, Type, ExtraConstructor, Extra) \
00025 class Name \
00026 { \
00027 typedef Type ga_type; \
00028 ga_type *root; \
00029 int limit; \
00030 int length; \
00031 public: \
00032 int Limit () const \
00033 { return limit; } \
00034 void SetLimit (int iLimit) \
00035 { \
00036 if (limit == iLimit) return; \
00037 if ((limit = iLimit)!=0) \
00038 root = (ga_type *)realloc (root, limit * sizeof (ga_type)); \
00039 else \
00040 { if (root) { free (root); root = NULL; } } \
00041 } \
00042 Name () \
00043 { limit = length = 0; root = NULL; ExtraConstructor; } \
00044 ~Name () \
00045 { SetLimit (0); } \
00046 int Length () const \
00047 { return length; } \
00048 void SetLength (int iLength, int iGrowStep = 8) \
00049 { \
00050 length = iLength; \
00051 int newlimit = ((length + (iGrowStep - 1)) / iGrowStep) * iGrowStep;\
00052 if (newlimit != limit) SetLimit (newlimit); \
00053 } \
00054 ga_type &operator [] (int n) \
00055 { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
00056 const ga_type &operator [] (int n) const \
00057 { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
00058 ga_type &Get (int n) \
00059 { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
00060 void Delete (int n) \
00061 { CS_ASSERT (n >= 0 && n < limit); \
00062 memmove (root + n, root + n + 1, (limit - n - 1) * sizeof (ga_type)); \
00063 SetLength (length-1); } \
00064 ga_type *GetArray () \
00065 { return root; } \
00066 int Push (const ga_type &val, int iGrowStep = 8) \
00067 { \
00068 SetLength (length + 1, iGrowStep); \
00069 memcpy (root + length - 1, &val, sizeof (ga_type)); \
00070 return length-1; \
00071 } \
00072 void Insert (int pos, const ga_type &val, int iGrowStep = 8) \
00073 { \
00074 CS_ASSERT (pos>=0 && pos<=length); \
00075 SetLength (length + 1, iGrowStep); \
00076 memmove (root+pos+1, root+pos, sizeof(ga_type) * (length-pos-1)); \
00077 memcpy (root + pos, &val, sizeof (ga_type)); \
00078 } \
00079 Extra \
00080 }
00081
00101 #define CS_TYPEDEF_GROWING_ARRAY(Name, Type) \
00102 CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, ;, ;)
00103
00115 #define CS_TYPEDEF_GROWING_ARRAY_REF(Name, Type) \
00116 CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, RefCount = 0, \
00117 int RefCount; \
00118 void IncRef () \
00119 { RefCount++; } \
00120 void DecRef () \
00121 { \
00122 if (RefCount == 1) SetLimit (0); \
00123 RefCount--; \
00124 })
00125
00136 #define CS_DECLARE_GROWING_ARRAY(Name, Type) \
00137 CS_TYPEDEF_GROWING_ARRAY(__##Name##_##Type,Type) Name
00138
00142 #define CS_DECLARE_GROWING_ARRAY_REF(Name, Type) \
00143 CS_TYPEDEF_GROWING_ARRAY_REF(__##Name,Type) Name
00144
00145 #endif // __CS_GARRAY_H__