-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleArray.tpp
More file actions
123 lines (105 loc) · 2.34 KB
/
SimpleArray.tpp
File metadata and controls
123 lines (105 loc) · 2.34 KB
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
#ifndef __SIMPLEARRAY_TPP_INCLUDED__
#define __SIMPLEARRAY_TPP_INCLUDED__
#include <stdio.h>
template <class T>
SimpleArray<T>::SimpleArray() {
printf("SIMPLEARRAY CONSTRUCTOR\n");
numElements = maxIndex = 0;
maxSize = MAX_ARRAY_SIZE;
}
template <class T>
SimpleArray<T>::~SimpleArray() {
//TODO: ADD a cout statement to see what executes. Do it in the Dynamic Set destroyer too.
printf("SIMPLEARRAY DESTRUCTOR\n");
}
template <class T>
void SimpleArray<T>::swapElements(int i,int j) {
Node<T> temp = elements[i];
elements[i] = elements[j];
elements[j] = temp;
}
template <class T>
bool SimpleArray<T>::search(T x) {
for(int i = 0; i < maxIndex; i++)
if(elements[i].val == x)
return true;
return false;
}
template <class T>
long long SimpleArray<T>::getCount(T x) {
for(int i = 0; i < maxIndex; i++)
if(elements[i].val == x)
return elements[i].count;
return 0;
}
template <class T>
long long SimpleArray<T>::getSize() {
return numElements;
}
template <class T>
T SimpleArray<T>::getMax() {
T maxElement = elements[0].val;
unsigned int i;
for(i = 0; i < numElements; i++)
if(maxElement < elements[i].val)
maxElement = elements[i].val;
return maxElement;
}
template <class T>
T SimpleArray<T>::getMin(){
T minElement = elements[0].val;
unsigned int i;
for(i = 0; i < maxIndex; i++)
if(minElement < elements[i].val)
minElement = elements[i].val;
return minElement;
}
template <class T>
void SimpleArray<T>::insert(T x){
unsigned int i = 0;
while(i < maxIndex && elements[i].val != x)
i++;
if(i < maxIndex) {
elements[i].count++;
numElements++;
}
else if(i == maxIndex && i < MAX_ARRAY_SIZE) {
elements[maxIndex].val = x;
elements[maxIndex].count = 1;
maxIndex++;
numElements++;
}
}
template <class T>
void SimpleArray<T>::remove(T x){
unsigned int i = 0,j = 0;
if(maxIndex > 0) {
while(i < maxIndex && elements[i].val != x)
i++;
if(i < maxIndex) {
if(elements[i].count > 1) {
elements[i].count--;
}
else {
j = maxIndex - 1;
swapElements(i,j);
maxIndex--;
}
numElements--;
}
}
}
template <class T>
std::string SimpleArray<T>::objType() {
std::string result = "SimpleArray";
return result;
};
template <class T>
void SimpleArray<T>::clear() {
maxIndex = 0;
numElements = 0;
for(long long i = 0; i < maxSize; i++) {
elements[i].count = 0;
}
}
#endif