00001 /*
00002 File: Array.h
00003
00004 Function: Defines an array type that manages its own storage space,
00005 and can be used as a stack or a list.
00006
00007 Author(s): Andrew Willmott
00008
00009 Copyright: (c) 1995-2000, Andrew Willmott
00010 */
00011
00012 #ifndef __Array__
00013 #define __Array__
00014
00015 #include <iostream.h>
00016 #include "cl/Basics.h"
00017 #include <stdio.h>
00018
00019 #define TMPLArray template<class T>
00020 #define TArray Array<T>
00021
00022 const Int kFirstAllocation = 16; // Default number of items to initially
00023 // allocate to the array
00024
00025 TMPLArray class Array
00026 {
00027 public:
00028 Array();
00029 Array(UInt32 size, Int alloc = kFirstAllocation);
00030 Array(const TArray &array);
00031 ~Array();
00032
00033 // Array operators
00034
00035 inline T &operator [] (Int i);
00036 inline const T &operator [] (Int i) const;
00037 inline Int NumItems() const;
00038
00039 TArray &operator = (const TArray &array);
00040
00041 // Useful for stack implementations
00042
00043 inline T &Top();
00044 inline Void Pop();
00045 inline Void Push(const T &t);
00046
00047 // List Operations
00048
00049 inline Void Append(const T &t);
00050 inline T &Last();
00051 Void Clear();
00052
00053 Void PreAllocate(UInt32 numItems);
00054 Void SetSize(Int newSize);
00055 Void Add(Int n = 1);
00056 Void Shrink(Int n = 1);
00057 Void Insert(Int i, Int n = 1);
00058 Void Delete(Int i, Int n = 1);
00059 Void ShrinkWrap();
00061 Void ClearTo(const T &t);
00062
00063 Void Append(const TArray &a);
00064 Void SwapWith(TArray &a);
00065 Void Replace(TArray &a);
00068 const T &Item(Int i) const
00069 { return(SELF[i]); };
00070 T &Item(Int i)
00071 { return(SELF[i]); };
00072
00073 // Low level access
00074
00075 inline T *Ref() const;
00076 inline T *Detach();
00079 Void Attach(T *itemsPtr, Int numItems, Bool shared);
00084 Void WriteFile(const Char *filename);
00085 Void ReadFile(const Char *filename);
00086
00087 Int FWrite(FILE *file);
00088 Int FRead(FILE *file);
00089
00090 // Private...
00091
00092 protected:
00093 T *item;
00094 UInt32 items;
00095 UInt32 allocated;
00096
00097 Void Grow();
00098 };
00099
00100 TMPLArray ostream &operator << (ostream &s, TArray &array);
00101 TMPLArray istream &operator >> (istream &s, TArray &array);
00102
00103
00104 // --- Inlines ----------------------------------------------------------------
00105
00106
00107 TMPLArray inline TArray::Array() : item(0), items(0), allocated(0)
00108 {
00109 }
00110
00111 TMPLArray inline Int TArray::NumItems() const
00112 {
00113 return(items);
00114 }
00115
00116 TMPLArray inline T &TArray::operator [] (Int i)
00117 {
00118 CheckRange(i, 0, items, "(Array::[]) index out of range");
00119
00120 return(item[i]);
00121 }
00122
00123 TMPLArray inline const T &TArray::operator [] (Int i) const
00124 {
00125 CheckRange(i, 0, items, "(Array::[]) index out of range");
00126
00127 return(item[i]);
00128 }
00129
00130 TMPLArray inline T &TArray::Top()
00131 {
00132 return(item[items - 1]);
00133 }
00134
00135 TMPLArray inline T &TArray::Last()
00136 {
00137 return(item[items - 1]);
00138 }
00139
00140 TMPLArray inline Void TArray::Push(const T &t)
00141 {
00142 if (items >= allocated)
00143 Grow();
00144
00145 item[items++] = t;
00146 }
00147
00148 TMPLArray inline Void TArray::Append(const T &t)
00149 {
00150 if (items >= allocated)
00151 Grow();
00152
00153 item[items++] = t;
00154 }
00155
00156 TMPLArray inline Void TArray::Pop()
00157 {
00158 items--;
00159 }
00160
00161 TMPLArray inline Void TArray::Clear()
00162 {
00163 items = 0;
00164 allocated = 0;
00165 delete[] item;
00166 item = 0;
00167 }
00168
00169 TMPLArray inline T *TArray::Ref() const
00170 {
00171 return(item);
00172 }
00173
00174 TMPLArray inline T *TArray::Detach()
00175 {
00176 T* result = item;
00177
00178 items = 0;
00179 allocated = 0;
00180 item = 0;
00181
00182 return(result);
00183 }
00184
00185 TMPLArray inline Void TArray::ClearTo(const T &t)
00186 {
00187 for (Int i = 0; i < items; i++)
00188 item[i] = t;
00189 }
00190
00191 #ifndef __ArrayTmpl__
00192 #include "cl/Array.cc"
00193 #endif
00194
00195 #endif