Google

Main Page   Class Hierarchy   Compound List   File List   Compound 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 __CSVECTOR_H__
00021 #define __CSVECTOR_H__
00022 
00023 #include "cstypes.h"
00024 
00031 class csBasicVector
00032 {
00033 protected:
00034   int count,limit,threshold;
00035   csSome *root;
00036 
00037 public:
00042   csBasicVector (int ilimit = 0, int ithreshold = 0);
00043 
00045   virtual ~csBasicVector();
00046 
00048   inline csSome& operator [] (int n);
00050   inline csSome& operator [] (int n) const;
00052   inline csSome& Get (int n) const;
00053 
00055   void SetLength (int n);
00056 
00058   inline int Length () const;
00060   inline int Limit () const;
00061 
00063   bool Delete (int n);
00065   bool Delete (csSome Item);
00066 
00068   inline void Exchange (int n1, int n2);
00070   int Find (csSome which) const;
00071 
00073   inline int Push (csSome what);
00075   inline int PushSmart (csSome what);
00077   inline csSome Pop ();
00079   inline csSome Top () const;
00080 
00082   bool Insert (int n, csSome Item);
00083 };
00084 
00098 class csVector : public csBasicVector
00099 {
00100 public:
00105   csVector (int ilimit = 8, int ithreshold = 16)
00106     : csBasicVector(ilimit, ithreshold) {}
00107 
00109   virtual ~csVector () {}
00110 
00112   int FindKey (csConstSome Key, int Mode = 0) const;
00114   int FindSortedKey (csConstSome Key, int Mode = 0) const;
00116   void QuickSort (int Left, int Right, int Mode = 0);
00118   void QuickSort (int Mode = 0);
00119 
00121   bool Delete (int n, bool FreeIt = true);
00123   bool Delete (csSome Item, bool FreeIt = true);
00125   bool Replace (int n, csSome what, bool FreePrevious = true);
00127   void DeleteAll (bool FreeThem = true);
00128 
00130   int InsertSorted (csSome Item, int *oEqual = NULL, int Mode = 0);
00132   virtual bool FreeItem (csSome Item);
00134   virtual int Compare (csSome Item1, csSome Item2, int Mode) const;
00136   virtual int CompareKey (csSome Item, csConstSome Key, int Mode) const;
00137 };
00138 
00139 inline csSome& csBasicVector::operator [] (int n)
00140 {
00141   CS_ASSERT (n >= 0);
00142   if (n >= count)
00143     SetLength (n + 1);
00144   return (root [n]);
00145 }
00146 
00147 inline csSome& csBasicVector::operator [] (int n) const
00148 {
00149   CS_ASSERT (n >= 0);
00150   CS_ASSERT (n < count);
00151   return (root [n]);
00152 }
00153 
00154 inline csSome& csBasicVector::Get (int n) const
00155 {
00156   CS_ASSERT (n >= 0);
00157   CS_ASSERT (n < count);
00158   return (root [n]);
00159 }
00160 
00161 inline int csBasicVector::Limit () const
00162 {
00163   return (limit);
00164 }
00165 
00166 inline int csBasicVector::Length () const
00167 {
00168   return (count);
00169 }
00170 
00171 inline bool csBasicVector::Delete (csSome Item)
00172 {
00173   int n = Find (Item);
00174   if (n == -1) return false;
00175   else return Delete (n);
00176 }
00177 
00178 inline int csBasicVector::Push (csSome what)
00179 {
00180   SetLength (count + 1);
00181   root [count - 1] = what;
00182   return (count - 1);
00183 }
00184 
00185 inline int csBasicVector::PushSmart (csSome what)
00186 {
00187   int n = Find (what);
00188   return (n == -1) ? Push (what) : n;
00189 }
00190 
00191 inline csSome csBasicVector::Pop ()
00192 {
00193   csSome ret = root [count - 1];
00194   SetLength (count - 1);
00195   return (ret);
00196 }
00197 
00198 inline csSome csBasicVector::Top () const
00199 {
00200   return root [count - 1];
00201 }
00202 
00203 inline void csBasicVector::Exchange (int n1, int n2)
00204 {
00205   csSome tmp = root [n1];
00206   root [n1] = root [n2];
00207   root [n2] = tmp;
00208 }
00209 
00210 inline bool csVector::Delete (csSome Item, bool FreeIt)
00211 {
00212   int n = Find (Item);
00213   if (n == -1) return false;
00214   else return Delete (n, FreeIt);
00215 }
00216 
00217 inline void csVector::QuickSort (int Mode)
00218 {
00219   if (count > 0)
00220     QuickSort (0, count - 1, Mode);
00221 }
00222 
00223 #endif // __CSVECTOR_H__

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