CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

csvector.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_CSVECTOR_H__
00021 #define __CS_CSVECTOR_H__
00022 
00023 #include "cstypes.h"
00024 
00025 // XXX: We should enable that but too many things in CS are still using the
00026 // csVector classes
00027 //#ifdef __GNUC__
00028 //# warning The csVector classes are deprecated. Please use csArray
00029 //# warning csGrowingArray, csRefArray or csPArray
00030 //#endif
00031 
00041 class csBasicVector
00042 {
00043 protected:
00044   int count,limit,threshold;
00045   void** root;
00046 
00047 public:
00052   csBasicVector (int ilimit = 0, int ithreshold = 0);
00053 
00055   virtual ~csBasicVector();
00056 
00058   inline void* & operator [] (int n);
00060   inline void* & operator [] (int n) const;
00062   inline void* & Get (int n) const;
00063 
00065   void SetLength (int n);
00066 
00068   inline int Length () const;
00070   inline int Limit () const;
00071 
00073   bool Delete (int n);
00075   bool DeleteChunk (int n,int size);
00077   bool Delete (void* Item);
00078 
00080   inline void Exchange (int n1, int n2);
00082   int Find (void* which) const;
00083 
00085   inline int Push (void* what);
00087   inline int PushSmart (void* what);
00089   inline void* Pop ();
00091   inline void* Top () const;
00092 
00094   bool Insert (int n, void* Item);
00096   bool InsertChunk (int n, int size, void** Item);
00097 };
00098 
00114 class csVector : public csBasicVector
00115 {
00116 public:
00121   csVector (int ilimit = 8, int ithreshold = 16)
00122     : csBasicVector(ilimit, ithreshold) {}
00123 
00125   virtual ~csVector () {}
00126 
00128   int FindKey (const void* Key, int Mode = 0) const;
00130   int FindSortedKey (const void* Key, int Mode = 0) const;
00132   void QuickSort (int Left, int Right, int Mode = 0);
00134   void QuickSort (int Mode = 0);
00135 
00137   bool Delete (int n, bool FreeIt = true);
00139   bool Delete (void* Item, bool FreeIt = true);
00141   bool Replace (int n, void* what, bool FreePrevious = true);
00143   void DeleteAll (bool FreeThem = true);
00144 
00146   int InsertSorted (void* Item, int *oEqual = NULL, int Mode = 0);
00148   virtual bool FreeItem (void* Item);
00150   virtual int Compare (void* Item1, void* Item2, int Mode) const;
00152   virtual int CompareKey (void* Item, const void* Key, int Mode) const;
00153 };
00154 
00155 inline void* & csBasicVector::operator [] (int n)
00156 {
00157   CS_ASSERT (n >= 0);
00158   if (n >= count)
00159     SetLength (n + 1);
00160   return (root [n]);
00161 }
00162 
00163 inline void* & csBasicVector::operator [] (int n) const
00164 {
00165   CS_ASSERT (n >= 0);
00166   CS_ASSERT (n < count);
00167   return (root [n]);
00168 }
00169 
00170 inline void* & csBasicVector::Get (int n) const
00171 {
00172   CS_ASSERT (n >= 0);
00173   CS_ASSERT (n < count);
00174   return (root [n]);
00175 }
00176 
00177 inline int csBasicVector::Limit () const
00178 {
00179   return (limit);
00180 }
00181 
00182 inline int csBasicVector::Length () const
00183 {
00184   return (count);
00185 }
00186 
00187 inline bool csBasicVector::Delete (void* Item)
00188 {
00189   int n = Find (Item);
00190   if (n == -1) return false;
00191   else return Delete (n);
00192 }
00193 
00194 inline int csBasicVector::Push (void* what)
00195 {
00196   SetLength (count + 1);
00197   root [count - 1] = what;
00198   return (count - 1);
00199 }
00200 
00201 inline int csBasicVector::PushSmart (void* what)
00202 {
00203   int n = Find (what);
00204   return (n == -1) ? Push (what) : n;
00205 }
00206 
00207 inline void* csBasicVector::Pop ()
00208 {
00209   if (count<=0)
00210     return NULL;
00211 
00212   void* ret = root [count - 1];
00213   SetLength (count - 1);
00214   return (ret);
00215 }
00216 
00217 inline void* csBasicVector::Top () const
00218 {
00219   return root [count - 1];
00220 }
00221 
00222 inline void csBasicVector::Exchange (int n1, int n2)
00223 {
00224   void* tmp = root [n1];
00225   root [n1] = root [n2];
00226   root [n2] = tmp;
00227 }
00228 
00229 inline bool csVector::Delete (void* Item, bool FreeIt)
00230 {
00231   int n = Find (Item);
00232   if (n == -1) return false;
00233   else return Delete (n, FreeIt);
00234 }
00235 
00236 inline void csVector::QuickSort (int Mode)
00237 {
00238   if (count > 0)
00239     QuickSort (0, count - 1, Mode);
00240 }
00241 
00242 #endif // __CS_CSVECTOR_H__

Generated for Crystal Space by doxygen 1.2.14