libStatGen Software 1
Loading...
Searching...
No Matches
IntArray.h
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __INTARRAY_H__
19#define __INTARRAY_H__
20
21#include <stdio.h>
22
24{
25private:
26 int * items;
27 int size, count;
28
29 void Grow(int new_size);
30 static int Compare(int * a, int * b);
31
32public:
33 static int alloc;
34
35 IntArray(int start_size = 0);
36 IntArray(const IntArray & source);
37 ~IntArray();
38
39 IntArray & operator = (const IntArray & rhs);
40
41 int & operator [](int index)
42 {
43 return items[index];
44 }
45 int operator [](int index) const
46 {
47 return items[index];
48 }
49
50 // Suggested by Anthony Berno, 12/28/06, to avoid "ambiguities" that
51 // Visual Studio encountered when handling implicit conversions ...
52 int & operator [](char index)
53 {
54 return items[int(index)];
55 }
56 int operator [](char index) const
57 {
58 return items[int(index)];
59 }
60 // ... who knows whether Visual Studio makes C++ annoying to encourage C#?
61
62 int & operator [](double fraction)
63 {
64 return items[(int)(count * fraction)];
65 }
66 int operator [](double fraction) const
67 {
68 return items[(int)(count * fraction)];
69 }
70
71 int Append(int value);
72 int Append(const IntArray & rhs);
73
74 void Push(int value)
75 {
76 Append(value);
77 }
78 int Pop()
79 {
80 return items[--count];
81 }
82 int Peek() const
83 {
84 return items[count - 1];
85 }
86 int &Last() const
87 {
88 return items[count - 1];
89 }
90
91 void PushIfNew(int value); // used for maintaining list without duplicates
92
93 int Delete(int index);
94 void InsertAt(int index, int value);
95
96 int Find(int value) const;
97 int FastFind(int value) const
98 {
99 return BinarySearch(value);
100 }
101 int BinarySearch(int value) const;
102 void Sort();
103 void Sort(IntArray & freeRider); // Sorts two arrays simultaneously
104
105 void Zero();
106 void Set(int value);
107 void SetSequence(int start = 0, int increment = 1);
108
109 int Length() const
110 {
111 return count;
112 }
113 void Dimension(int new_count)
114 {
115 Grow(new_count);
116 count = new_count;
117 }
118 void Clear()
119 {
120 count = 0;
121 }
122
123 int Sum() const
124 {
125 return Sum(0, count - 1);
126 }
127 int Sum(int start) const
128 {
129 return Sum(start, count - 1);
130 }
131 int Sum(int start, int end) const;
132
133 double dSum() const
134 {
135 return dSum(0, count - 1);
136 }
137 double dSum(int start) const
138 {
139 return dSum(start, count - 1);
140 }
141 double dSum(int start, int end) const;
142
143 int SumProduct(const IntArray & weight) const;
144 double dSumProduct(const IntArray & weight) const;
145
146 int Max() const
147 {
148 return Max(0, count - 1);
149 }
150 int Max(int start) const
151 {
152 return Max(start, count - 1);
153 }
154 int Max(int start, int end) const;
155
156 int Min() const
157 {
158 return Min(0, count - 1);
159 }
160 int Min(int start) const
161 {
162 return Min(start, count - 1);
163 }
164 int Min(int start, int end) const;
165
166 int Count() const
167 {
168 return count;
169 }
170 int CountIfGreater(int treshold) const;
171 int CountIfGreaterOrEqual(int treshold) const;
172
173 void Swap(int i, int j)
174 {
175 int tmp = items[i];
176 items[i] = items[j];
177 items[j] = tmp;
178 }
179
180 void Reverse();
181
182 operator int *()
183 {
184 return items;
185 }
186
187 void Add(int term);
188 void Subtract(int term)
189 {
190 Add(-term);
191 }
192 void Multiply(int factor);
193 void Divide(int denominator);
194
195 void Add(const IntArray & rhs);
196
197 IntArray & operator += (int rhs)
198 {
199 Add(rhs);
200 return *this;
201 }
202
203 IntArray & operator += (const IntArray & rhs)
204 {
205 Add(rhs);
206 return *this;
207 }
208
209 IntArray & operator *= (int rhs)
210 {
211 Multiply(rhs);
212 return *this;
213 }
214
215 IntArray & operator -= (int rhs)
216 {
217 Add(-rhs);
218 return *this;
219 }
220
221 IntArray & operator /= (int rhs)
222 {
223 Divide(rhs);
224 return *this;
225 }
226
227 int InnerProduct(IntArray & v);
228
229 bool operator == (const IntArray & rhs) const;
230 bool operator != (const IntArray & rhs) const;
231
232 bool isAscending();
233 bool isDescending();
234
235 void Stack(const IntArray & rhs);
236
237 void Swap(IntArray & rhs);
238
239 void Print()
240 {
241 Print(stdout);
242 }
243 void Print(const char * label)
244 {
245 Print(stdout, label);
246 }
247 void Print(FILE * output);
248 void Print(FILE * output, const char * label);
249
250 int Product();
251 double DoubleProduct();
252
253 int Hash(int initval = 0);
254};
255
256#endif
257
258