Google

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 
00026 #define CS_GARRAY_GROWSTEP 16
00027 
00032 #define CS_GARRAY_SHRINKLIMIT 1000
00033 
00034 // Common macro for declarations below
00035 #define CS_TYPEDEF_GROWING_ARRAY_EXT(Name, Type, ExtraConstructor, Extra) \
00036   class Name                                                            \
00037   {                                                                     \
00038     typedef Type ga_type;                                               \
00039     ga_type *root;                                                      \
00040     int limit;                                                          \
00041     int length;                                                         \
00042   public:                                                               \
00043     int Limit () const                                                  \
00044     { return limit; }                                                   \
00045     void SetLimit (int inlimit)                                         \
00046     {                                                                   \
00047       if (limit == inlimit) return;                                     \
00048       if ((limit = inlimit)!=0)                                         \
00049         root = (ga_type *)realloc (root, limit * sizeof (ga_type));     \
00050       else                                                              \
00051       { if (root) { free (root); root = NULL; } }                       \
00052     }                                                                   \
00053     Name ()                                                             \
00054     { limit = length = 0; root = NULL; ExtraConstructor; }              \
00055     ~Name ()                                                            \
00056     { SetLimit (0); }                                                   \
00057     int Length () const                                                 \
00058     { return length; }                                                  \
00059     void SetLength (int inlength, int growstep = CS_GARRAY_GROWSTEP)    \
00060     {                                                                   \
00061       length = inlength;                                                \
00062       int newlimit = ((length + (growstep - 1)) / growstep) * growstep; \
00063       if (newlimit > limit || newlimit < limit-CS_GARRAY_SHRINKLIMIT)   \
00064         SetLimit (newlimit);                                            \
00065     }                                                                   \
00066     ga_type &operator [] (int n)                                        \
00067     { CS_ASSERT (n >= 0 && n < limit); return root [n]; }               \
00068     const ga_type &operator [] (int n) const                            \
00069     { CS_ASSERT (n >= 0 && n < limit); return root [n]; }               \
00070     ga_type &Get (int n)                                                \
00071     { CS_ASSERT (n >= 0 && n < limit); return root [n]; }               \
00072     void Delete (int n)                                                 \
00073     { CS_ASSERT (n >= 0 && n < limit);                                  \
00074       memmove (root + n, root + n + 1, (limit - n - 1) * sizeof (ga_type)); \
00075       SetLength (length-1); }                                           \
00076     ga_type *GetArray ()                                                \
00077     { return root; }                                                    \
00078     int Push (const ga_type &val, int growstep = CS_GARRAY_GROWSTEP)    \
00079     {                                                                   \
00080       SetLength (length + 1, growstep);                                 \
00081       memcpy (root + length - 1, &val, sizeof (ga_type));               \
00082       return length-1;                                                  \
00083     }                                                                   \
00084     void Insert (int pos, const ga_type &val, int growstep=CS_GARRAY_GROWSTEP)\
00085     {                                                                   \
00086       CS_ASSERT (pos>=0 && pos<=length);                                \
00087       SetLength (length + 1, growstep);                                 \
00088       memmove (root+pos+1, root+pos, sizeof(ga_type) * (length-pos-1)); \
00089       memcpy (root + pos, &val, sizeof (ga_type));                      \
00090     }                                                                   \
00091     Extra                                                               \
00092   }
00093 
00113 #define CS_TYPEDEF_GROWING_ARRAY(Name, Type)                            \
00114   CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, ;,  ;)
00115 
00127 #define CS_TYPEDEF_GROWING_ARRAY_REF(Name, Type)                        \
00128   CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, RefCount = 0,               \
00129     int RefCount;                                                       \
00130     void IncRef ()                                                      \
00131     { RefCount++; }                                                     \
00132     void DecRef ()                                                      \
00133     {                                                                   \
00134       if (RefCount == 1) SetLimit (0);                                  \
00135       RefCount--;                                                       \
00136     })
00137 
00148 #define CS_DECLARE_GROWING_ARRAY(Name, Type)                            \
00149   CS_TYPEDEF_GROWING_ARRAY(__##Name##_##Type,Type) Name
00150 
00154 #define CS_DECLARE_GROWING_ARRAY_REF(Name, Type)                        \
00155   CS_TYPEDEF_GROWING_ARRAY_REF(__##Name,Type) Name
00156 
00157 #endif // __CS_GARRAY_H__

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