File: IntArray.cpp

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (425 lines) | stat: -rw-r--r-- 8,462 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "IntArray.h"
#include "Error.h"
#include "Hash.h"
#include "Sort.h"

#include <string.h>

int IntArray::alloc = 4;

IntArray::IntArray(int start_size)
{
    count = start_size;
    size = (count + alloc) / alloc * alloc;
    items = new int [size];
}

IntArray::IntArray(const IntArray & source)
{
    count = source.count;
    size = source.size;
    items = new int [size];

    for (int i = 0; i < count; i++)
        items[i] = source.items[i];
}

IntArray::~IntArray()
{
    delete [] items;
}

void IntArray::Grow(int new_size)
{
    if (new_size > size)
    {
        if ((new_size >> 1) >= size)
            size = (new_size + alloc) / alloc * alloc;
        else
        {
            size = alloc;
            while (size <= new_size)
                size *= 2;
        }

        int * new_items = new int [size];
        for (int i = 0; i < count; i++)
            new_items[i] = items[i];
        delete [] items;
        items = new_items;
    }
}

int IntArray::Append(int value)
{
    Grow(count + 1);
    items[count++] = value;
    return count;
}

int IntArray::Append(const IntArray & rhs)
{
    Grow(count + rhs.count);
    for (int i = 0; i < rhs.count; i++)
        items[count + i] = rhs.items[i];
    count += rhs.count;
    return count;
}

void IntArray::Set(int value)
{
    for (int i = 0; i < count; i++)
        items[i] = value;
}

void IntArray::SetSequence(int start, int increment)
{
    for (int i = 0; i < count; i++, start += increment)
        items[i] = start;
}

int IntArray::Delete(int index)
{
    count--;
    if (count - index)
        memmove(items + index, items + index + 1, sizeof(int) *(count - index));
    return count;
}

void IntArray::InsertAt(int index, int value)
{
    Grow(count + 1);
    if (count - index)
        memmove(items + index + 1, items + index, sizeof(int) *(count - index));
    items[index] = value;
    count++;
}

IntArray & IntArray::operator = (const IntArray & rhs)
{
    Grow(rhs.count);
    count = rhs.count;
    for (int i = 0; i < count; i++)
        items[i] = rhs.items[i];
    return *this;
}

int IntArray::Sum(int start, int end) const
{
    int result = 0;

    for (int i = start; i <= end; i++)
        result += items[i];

    return result;
}

double IntArray::dSum(int start, int end) const
{
    double result = 0;

    for (int i = start; i <= end; i++)
        result += items[i];

    return result;
}

int IntArray::Max(int start, int end) const
{
    if (start >= count) return 0;

    int result = items[start];

    for (int i = start + 1; i <= end; i++)
        if (result < items[i])
            result = items[i];

    return result;
}

int IntArray::Min(int start, int end) const
{
    if (start >= count) return 0;

    int result = items[start];

    for (int i = start + 1; i <= end; i++)
        if (result > items[i])
            result = items[i];

    return result;
}

int IntArray::Find(int value) const
{
    for (int i = 0; i < count; i++)
        if (value == items[i])
            return i;
    return -1;
}

int IntArray::BinarySearch(int value) const
{
    int start = 0;
    int stop = count - 1;

    while (start <= stop)
    {
        int mid = (start + stop) / 2;

        if (items[mid] == value)
            return mid;

        if (items[mid] > value)
            stop = mid - 1;
        else
            start = mid + 1;
    }

    return -1;
}

void IntArray::Zero()
{
    for (int i = 0; i < count; i++)
        items[i] = 0;
}

int IntArray::Compare(int * a, int * b)
{
    return *a - *b;
}

void IntArray::Sort()
{
    QuickSort(items, count, sizeof(int), COMPAREFUNC Compare);
}

void IntArray::Sort(IntArray & freeRider)
{
    QuickSort2(items, freeRider.items, count, sizeof(int), COMPAREFUNC Compare);
}


void IntArray::Reverse()
{
    for (int i = 0, j = count - 1; i < j; i++, j--)
        Swap(i, j);
}

int IntArray::CountIfGreater(int threshold) const
{
    int result = 0;

    for (int i = 0; i < count; i++)
        if (items[i] > threshold)
            result++;

    return result;
}

int IntArray::CountIfGreaterOrEqual(int treshold) const
{
    int result = 0;

    for (int i = 0; i < count; i++)
        if (items[i] >= treshold)
            result++;

    return result;
}

void IntArray::Add(int term)
{
    for (int i = 0; i < count; i++)
        items[i] += term;
}

void IntArray::Multiply(int factor)
{
    for (int i = 0; i < count; i++)
        items[i] *= factor;
}

void IntArray::Divide(int denominator)
{
    for (int i = 0; i < count; i++)
        items[i] /= denominator;
}

void IntArray::Stack(const IntArray & a)
{
    int end = count;

    Dimension(count + a.count);

    for (int i = 0; i < a.count; i++)
        items[i + end] = a[i];
}

bool IntArray::operator == (const IntArray & rhs) const
{
    if (count != rhs.count)
        return false;

    for (int i = 0; i < rhs.count; i++)
        if (items[i] != rhs.items[i])
            return false;

    return true;
}

bool IntArray::operator != (const IntArray & rhs) const
{
    return !(*this == rhs);
}

// Check if all values are in ascending or descending order
//

bool IntArray::isAscending()
{
    for (int i = 1; i < count; i++)
        if (items[i] < items[i - 1])
            return false;
    return true;
}

bool IntArray::isDescending()
{
    for (int i = 1; i < count; i++)
        if (items[i] > items[i - 1])
            return false;
    return true;
}

void IntArray::Add(const IntArray & v)
{
    if (Length() != v.Length())
        error("IntArray::Add - vectors have different lengths\n"
              "IntArrays     - Left[%d] += Right[%d] ",
              Length(), v.Length());

    for (int i = 0; i < Length(); i++)
        items[i] += v[i];
}

int IntArray::InnerProduct(IntArray & v)
{
    if (Length() != v.Length())
        error("IntArray::InnerProduct - vectors have different dimensions\n"
              "IntArrays              - Left[%d] * Right[%d] ",
              Length(), v.Length());

    int sum = 0;
    for (int i = 0; i < Length(); i++)
        sum += items[i] * v[i];

    return sum;
}

void IntArray::Swap(IntArray & rhs)
{
    int * temp = rhs.items;
    rhs.items = items;
    items = temp;

    int swap = rhs.count;
    rhs.count = count;
    count = swap;

    swap = rhs.size;
    rhs.size = size;
    size = swap;
}

void IntArray::Print(FILE * output)
{
    Print(output, "Array of Integers");
}

void IntArray::Print(FILE * output, const char * label)
{
    fprintf(output, "%s [%d elements]: ", label, count);

    for (int i = 0; i < count; i++)
        fprintf(output, "%d ", items[i]);

    fprintf(output, "\n");
}

void IntArray::PushIfNew(int value)
{
    for (int i = 0; i < count; i++)
        if (items[i] == value)
            return;

    Push(value);
}

int IntArray::Product()
{
    int product = 1;

    for (int i = 0; i < count; i++)
        product *= items[i];

    return product;
}

double IntArray::DoubleProduct()
{
    double product = 1.0;

    for (int i = 0; i < count; i++)
        product *= items[i];

    return product;
}

int IntArray::Hash(int initval)
{
    return hash((unsigned char *) items, sizeof(int) * count, initval);
}

int IntArray::SumProduct(const IntArray & weight) const
{
    if (count != weight.count)
        error("IntArray::SumProduct called with different sized arrays\n");

    int sum = 0;
    for (int i = 0; i < count; i++)
        sum += items[i] * weight[i];

    return sum;
}

double IntArray::dSumProduct(const IntArray & weight) const
{
    if (count != weight.count)
        error("IntArray::dSumProduct called with different sized arrays\n");

    double sum = 0.0;
    for (int i = 0; i < count; i++)
        sum += items[i] * weight[i];

    return sum;
}