-
Notifications
You must be signed in to change notification settings - Fork 2
/
Array.h
119 lines (105 loc) · 2.3 KB
/
Array.h
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
#ifndef __ARRAY_H
#define __ARRAY_H
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <malloc.h>
using namespace std;
extern long MEMUSED;
class Array {
protected:
int *theArray;
unsigned int theSize;
unsigned int totSize;
//unsigned int theIncr;
public:
//Array (int sz, int incr);
Array(int sz);
~Array();
int subsequence(Array * ar);
//void add (int, unsigned int);
void add_ext(int val, int off, int *ary)
{
ary[off+theSize] = val;
theSize++;
}
int operator [] (unsigned int index)
{
return theArray[index];
};
void setitem(int pos, int val){
theArray[pos] = val;
};
int totsize()
{
return totSize;
}
void set_totsize(int sz){
totSize = sz;
}
void set_size(int sz){
theSize = sz;
}
void reset()
{
theSize = 0;
}
int *array()
{
return theArray;
}
void set_array(int *ary){
theArray = ary;
}
//int subsequence(Array&);
//int compare(Array&);
friend ostream& operator << (ostream& outputStream, Array& arr);
static int Arraycompare(void * iset1, void *iset2)
{
Array *it1 = (Array *) iset1;
Array *it2 = (Array *) iset2;
return it1->compare(*it2);
}
int compare(Array& ar2);
int item (unsigned int index)
{
return theArray[index];
}
unsigned int size()
{
return theSize;
}
void realloc(int newsz)
{
MEMUSED -= totSize*sizeof(int);
totSize = newsz;
theArray = (int *)::realloc(theArray, totSize*sizeof(int));
if (theArray == NULL){
cout << "MEMORY EXCEEDED\n";
exit(-1);
}
MEMUSED += totSize*sizeof(int);
}
void optadd(int item)
{
theArray[theSize++] = item;
}
void add (int item)
{
if (theSize+1 > totSize){
//totSize = (int) (totSize*1.5);
//cout << " " << MEMUSED;
realloc((int)(totSize*1.5));
//theArray = (int *)realloc(theArray, totSize*sizeof(int));
//if (theArray == NULL){
// cout << "MEMORY EXCEEDED\n";
// exit(-1);
//}
//MEMUSED += totSize*sizeof(int);
//cout << " " << MEMUSED << endl;
}
theArray[theSize] = item;
theSize++;
}
};
#endif //__ARRAY_H