IntArray.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __INTARRAY_H__
00019 #define __INTARRAY_H__
00020
00021 #include <stdio.h>
00022
00023 class IntArray
00024 {
00025 private:
00026 int * items;
00027 int size, count;
00028
00029 void Grow(int new_size);
00030 static int Compare(int * a, int * b);
00031
00032 public:
00033 static int alloc;
00034
00035 IntArray(int start_size = 0);
00036 IntArray(const IntArray & source);
00037 ~IntArray();
00038
00039 IntArray & operator = (const IntArray & rhs);
00040
00041 int & operator [](int index)
00042 {
00043 return items[index];
00044 }
00045 int operator [](int index) const
00046 {
00047 return items[index];
00048 }
00049
00050
00051
00052 int & operator [](char index)
00053 {
00054 return items[int(index)];
00055 }
00056 int operator [](char index) const
00057 {
00058 return items[int(index)];
00059 }
00060
00061
00062 int & operator [](double fraction)
00063 {
00064 return items[(int)(count * fraction)];
00065 }
00066 int operator [](double fraction) const
00067 {
00068 return items[(int)(count * fraction)];
00069 }
00070
00071 int Append(int value);
00072 int Append(const IntArray & rhs);
00073
00074 void Push(int value)
00075 {
00076 Append(value);
00077 }
00078 int Pop()
00079 {
00080 return items[--count];
00081 }
00082 int Peek() const
00083 {
00084 return items[count - 1];
00085 }
00086 int &Last() const
00087 {
00088 return items[count - 1];
00089 }
00090
00091 void PushIfNew(int value);
00092
00093 int Delete(int index);
00094 void InsertAt(int index, int value);
00095
00096 int Find(int value) const;
00097 int FastFind(int value) const
00098 {
00099 return BinarySearch(value);
00100 }
00101 int BinarySearch(int value) const;
00102 void Sort();
00103 void Sort(IntArray & freeRider);
00104
00105 void Zero();
00106 void Set(int value);
00107 void SetSequence(int start = 0, int increment = 1);
00108
00109 int Length() const
00110 {
00111 return count;
00112 }
00113 void Dimension(int new_count)
00114 {
00115 Grow(new_count);
00116 count = new_count;
00117 }
00118 void Clear()
00119 {
00120 count = 0;
00121 }
00122
00123 int Sum() const
00124 {
00125 return Sum(0, count - 1);
00126 }
00127 int Sum(int start) const
00128 {
00129 return Sum(start, count - 1);
00130 }
00131 int Sum(int start, int end) const;
00132
00133 int Max() const
00134 {
00135 return Max(0, count - 1);
00136 }
00137 int Max(int start) const
00138 {
00139 return Max(start, count - 1);
00140 }
00141 int Max(int start, int end) const;
00142
00143 int Min() const
00144 {
00145 return Min(0, count - 1);
00146 }
00147 int Min(int start) const
00148 {
00149 return Min(start, count - 1);
00150 }
00151 int Min(int start, int end) const;
00152
00153 int Count() const
00154 {
00155 return count;
00156 }
00157 int CountIfGreater(int treshold) const;
00158 int CountIfGreaterOrEqual(int treshold) const;
00159
00160 void Swap(int i, int j)
00161 {
00162 int tmp = items[i];
00163 items[i] = items[j];
00164 items[j] = tmp;
00165 }
00166
00167 void Reverse();
00168
00169 operator int *()
00170 {
00171 return items;
00172 }
00173
00174 void Add(int term);
00175 void Subtract(int term)
00176 {
00177 Add(-term);
00178 }
00179 void Multiply(int factor);
00180 void Divide(int denominator);
00181
00182 void Add(const IntArray & rhs);
00183
00184 IntArray & operator += (int rhs)
00185 {
00186 Add(rhs);
00187 return *this;
00188 }
00189
00190 IntArray & operator += (const IntArray & rhs)
00191 {
00192 Add(rhs);
00193 return *this;
00194 }
00195
00196 IntArray & operator *= (int rhs)
00197 {
00198 Multiply(rhs);
00199 return *this;
00200 }
00201
00202 IntArray & operator -= (int rhs)
00203 {
00204 Add(-rhs);
00205 return *this;
00206 }
00207
00208 IntArray & operator /= (int rhs)
00209 {
00210 Divide(rhs);
00211 return *this;
00212 }
00213
00214 int InnerProduct(IntArray & v);
00215
00216 bool operator == (const IntArray & rhs) const;
00217 bool operator != (const IntArray & rhs) const;
00218
00219 bool isAscending();
00220 bool isDescending();
00221
00222 void Stack(const IntArray & rhs);
00223
00224 void Swap(IntArray & rhs);
00225
00226 void Print()
00227 {
00228 Print(stdout);
00229 }
00230 void Print(const char * label)
00231 {
00232 Print(stdout, label);
00233 }
00234 void Print(FILE * output);
00235 void Print(FILE * output, const char * label);
00236
00237 int Product();
00238 double DoubleProduct();
00239
00240 int Hash(int initval = 0);
00241 };
00242
00243 #endif
00244
00245