-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGArray.cc
91 lines (79 loc) · 1.85 KB
/
GArray.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
#include <stdio.h>
#include <errno.h>
#include "GArray.h"
GArray::GArray (int sz){
totSize = sz;
theSize = 0;
//theIncr = incr;
theArray = NULL;
if (sz > 0){
theArray = (unsigned char *) malloc (totSize*sizeof(unsigned char));
//theArray = new unsigned char [totSize];
if (theArray == NULL){
perror("memory:: Array");
exit(errno);
}
//MEMUSED += totSize*sizeof(unsigned char);
}
//MEMUSED += sizeof(GArray);
}
GArray::~GArray(){
if (theArray) {
free(theArray);
//delete [] theArray;
//MEMUSED -= totSize*sizeof(unsigned char);
//cout << "CAME HERE " << MEMUSED <<endl;
}
theArray = NULL;
//MEMUSED -= sizeof(GArray);
}
ostream& operator << (ostream& outputStream, GArray& arr){
for (int i=0; i < arr.theSize; i++)
outputStream << (int) arr[i] << " ";
return outputStream;
}
int GArray::subsequence(GArray * ar)
{
int i,j;
int sz1, sz2;
GArray *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 GArray::compare(GArray& 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;
}