00001 /*
00002 File: ObjArray.h
00003
00004 Function: Defines an array type that manages its own storage space,
00005 and can be used as a stack or a rudimentary list.
00006
00007 Author(s): Andrew Willmott
00008
00009 Copyright: (c) 1995-2000, Andrew Willmott
00010 */
00011
00012 #ifndef __ObjArray__
00013 #define __ObjArray__
00014
00015 #include <iostream.h>
00016 #include "cl/Basics.h"
00017 #include "cl/Action.h"
00018 #include "cl/Object.h"
00019
00020 const Int kFirstObjAlloc = 16; // Default number of items to initially
00021 // allocate to the array
00022
00023 class ObjArray : public Object
00024 {
00025 public:
00026 ObjArray();
00027 ObjArray(Int size, Int alloc = kFirstObjAlloc);
00028 ObjArray(const ObjArray &array);
00029 ~ObjArray();
00030
00031 // ObjArray operators
00032
00033 inline ObjectPtr operator [] (Int i) const; // indexing operator
00034 inline Int NumItems() const; // Number of items in the array
00035 ObjArray &operator >> (Action<ObjectPtr> &a);
00036 ObjArray &operator = (const ObjArray &array);
00037
00038 // Useful for stack implementations
00039
00040 inline ObjectPtr Top(); // Return last item (top-of-stack)
00041 inline Void Pop(); // Delete last item (pop)
00042 inline Void Push(ObjectPtr objPtr); // Add item to end of array
00043
00044 // List Operations
00045
00046 inline Void Set(Int i, ObjectPtr objPtr);
00047 // Change item i to objPtr
00048 inline Void Append(ObjectPtr objPtr);
00049 // Append item to end of array
00050 inline Void Prepend(ObjectPtr objPtr);
00051 // Prepend item to start of array
00052 inline Void Insert(Int i, ObjectPtr objPtr); // Insert item at i
00053 inline Void Delete(Int i); // Delete item at i
00054
00055 Void Clear(); // Delete all items
00056
00057 Void Print(ostream &s) const;
00058 Void Parse(istream &s);
00059 ObjectPtr Clone() const; // Return copy of list
00060 Void Free(); // Free all storage
00061
00062 Void SetSize(Int newSize); // Set array size directly
00063 Void Add(Int n); // Add n items to the array
00064 Void Shrink(Int n); // shrink the array n items
00065 Void Insert(Int i, Int n); // Insert n items at i
00066 Void Delete(Int i, Int n); // Delete n items at i
00067 Void ShrinkWrap();
00068 // Ensure allocated space = space being used.
00069
00070 // Low level access
00071
00072 inline ObjectPtr *Ref(); // Return pointer to array
00073
00074 // Private...
00075
00076 protected:
00077
00078 Void Grow();
00079
00080 ObjectPtr *item; // pointer to array
00081 SInt32 items; // items in the array
00082 SInt32 allocated; // number of items we have space allocated for.
00083 };
00084
00085
00086 // --- Inlines ----------------------------------------------------------------
00087
00088
00089 inline ObjArray::ObjArray() : items(0), item(0), allocated(0)
00090 {
00091 }
00092
00093 inline Int ObjArray::NumItems() const
00094 {
00095 return(items);
00096 }
00097
00098 inline ObjectPtr ObjArray::operator [] (Int i) const
00099 {
00100 CheckRange(i, 0, items, "(ObjArray::[]) index out of range");
00101
00102 return(item[i]);
00103 }
00104
00105 inline ObjectPtr ObjArray::Top()
00106 {
00107 return(item[items - 1]);
00108 }
00109
00110 inline Void ObjArray::Push(ObjectPtr objPtr)
00111 {
00112 if (items >= allocated)
00113 Grow();
00114
00115 item[items++] = objPtr;
00116 }
00117
00118 inline Void ObjArray::Pop()
00119 {
00120 items--;
00121 item[items]->Free();
00122 }
00123
00124 inline Void ObjArray::Set(Int i, ObjectPtr objPtr)
00125 {
00126 CheckRange(i, 0, items, "(ObjArray::Set) index out of range");
00127
00128 item[i]->Free();
00129 item[i] = objPtr;
00130 }
00131
00132 inline Void ObjArray::Append(ObjectPtr objPtr)
00133 {
00134 if (items >= allocated)
00135 Grow();
00136
00137 item[items++] = objPtr;
00138 }
00139
00140 inline Void ObjArray::Prepend(ObjectPtr objPtr)
00141 {
00142 Insert(0, 1);
00143 item[0] = objPtr;
00144 }
00145
00146 inline Void ObjArray::Insert(Int i, ObjectPtr objPtr)
00147 {
00148 Insert(i, 1);
00149 item[i] = objPtr;
00150 }
00151
00152 inline Void ObjArray::Delete(Int i)
00153 {
00154 Delete(i, 1);
00155 }
00156
00157 inline ObjectPtr *ObjArray::Ref()
00158 {
00159 return(item);
00160 }
00161
00162 #endif