Main Page   Class Hierarchy   Compound List   File List   Compound Members  

garray.h

00001 /*
00002     Crystal Space utility library: vector class interface
00003     Copyright (C) 1998,1999,2000 by Andrew Zabolotny <bit@eltech.ru>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_GARRAY_H__
00021 #define __CS_GARRAY_H__
00022 
00023 // Common macro for declarations below
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__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000