-
Notifications
You must be signed in to change notification settings - Fork 4
/
Array.cc
92 lines (80 loc) · 1.79 KB
/
Array.cc
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
#include <stdio.h>
#include <errno.h>
#include "Array.h"
using namespace std;
Array::Array (int sz){
totSize = sz;
theSize = 0;
//theIncr = incr;
theArray = NULL;
if (sz > 0){
theArray = (int *) malloc (totSize*sizeof(int));
//theArray = new int [totSize];
if (theArray == NULL){
perror("memory:: Array");
exit(errno);
}
MEMUSED += totSize*sizeof(int);
}
MEMUSED += sizeof(Array);
}
Array::~Array(){
if (theArray) {
free(theArray);
//delete [] theArray;
MEMUSED -= totSize*sizeof(int);
//cout << "CAME HERE " << MEMUSED <<endl;
}
theArray = NULL;
MEMUSED -= sizeof(Array);
}
ostream& operator << (ostream& outputStream, Array& arr){
for (int i=0; i < arr.theSize; i++)
outputStream << arr[i] << " ";
return outputStream;
}
int Array::subsequence(Array * ar)
{
int i,j;
int sz1, sz2;
Array *ar1, *ar2;
int retval;
if (theSize <= ar->theSize){
sz1 = theSize;
sz2 = ar->theSize;
ar1 = this;
ar2 = ar;
retval = 1;
}
else{
sz1 = ar->theSize;
sz2 = theSize;
ar1 = ar;
ar2 = this;
retval = -1;
}
int start = 0;
for(i=0; i < sz1; i++){
for(j=start; j < sz2; j++){
if ((ar1->theArray)[i] == (ar2->theArray)[j]){
start = j+1;
break;
}
}
if (j >= ar2->theSize) return 0;
}
return retval;
}
int Array::compare(Array& ar2)
{
int len;
if (size() <= ar2.size()) len = size();
else len = ar2.size();
for(int i=0; i < len; i++){
if ((theArray)[i] > (ar2.theArray)[i]) return 1;
else if ((theArray)[i] < (ar2.theArray)[i]) return -1;
}
if (size() < ar2.size()) return -1;
else if (size() > ar2.size()) return 1;
else return 0;
}