libStatGen Software  1
IntArray.h
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
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     // Suggested by Anthony Berno, 12/28/06, to avoid "ambiguities" that
00051     // Visual Studio encountered when handling implicit conversions ...
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     // ... who knows whether Visual Studio makes C++ annoying to encourage C#?
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);    // used for maintaining list without duplicates
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); // Sorts two arrays simultaneously
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     double  dSum() const
00134     {
00135         return dSum(0, count - 1);
00136     }
00137     double  dSum(int start) const
00138     {
00139         return dSum(start, count - 1);
00140     }
00141     double  dSum(int start, int end) const;
00142 
00143     int     SumProduct(const IntArray & weight) const;
00144     double  dSumProduct(const IntArray & weight) const;
00145 
00146     int  Max() const
00147     {
00148         return Max(0, count - 1);
00149     }
00150     int  Max(int start) const
00151     {
00152         return Max(start, count - 1);
00153     }
00154     int  Max(int start, int end) const;
00155 
00156     int  Min() const
00157     {
00158         return Min(0, count - 1);
00159     }
00160     int  Min(int start) const
00161     {
00162         return Min(start, count - 1);
00163     }
00164     int  Min(int start, int end) const;
00165 
00166     int  Count() const
00167     {
00168         return count;
00169     }
00170     int  CountIfGreater(int treshold) const;
00171     int  CountIfGreaterOrEqual(int treshold) const;
00172 
00173     void Swap(int i, int j)
00174     {
00175         int tmp = items[i];
00176         items[i] = items[j];
00177         items[j] = tmp;
00178     }
00179 
00180     void Reverse();
00181 
00182     operator int *()
00183     {
00184         return items;
00185     }
00186 
00187     void Add(int term);
00188     void Subtract(int term)
00189     {
00190         Add(-term);
00191     }
00192     void Multiply(int factor);
00193     void Divide(int denominator);
00194 
00195     void Add(const IntArray & rhs);
00196 
00197     IntArray & operator += (int rhs)
00198     {
00199         Add(rhs);
00200         return *this;
00201     }
00202 
00203     IntArray & operator += (const IntArray & rhs)
00204     {
00205         Add(rhs);
00206         return *this;
00207     }
00208 
00209     IntArray & operator *= (int rhs)
00210     {
00211         Multiply(rhs);
00212         return *this;
00213     }
00214 
00215     IntArray & operator -= (int rhs)
00216     {
00217         Add(-rhs);
00218         return *this;
00219     }
00220 
00221     IntArray & operator /= (int rhs)
00222     {
00223         Divide(rhs);
00224         return *this;
00225     }
00226 
00227     int  InnerProduct(IntArray & v);
00228 
00229     bool operator == (const IntArray & rhs) const;
00230     bool operator != (const IntArray & rhs) const;
00231 
00232     bool isAscending();
00233     bool isDescending();
00234 
00235     void Stack(const IntArray & rhs);
00236 
00237     void Swap(IntArray & rhs);
00238 
00239     void Print()
00240     {
00241         Print(stdout);
00242     }
00243     void Print(const char * label)
00244     {
00245         Print(stdout, label);
00246     }
00247     void Print(FILE * output);
00248     void Print(FILE * output, const char * label);
00249 
00250     int    Product();
00251     double DoubleProduct();
00252 
00253     int  Hash(int initval = 0);
00254 };
00255 
00256 #endif
00257 
00258 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends