Home directory for Malawi's wwwroot
Duncan Ewan
2021-02-19 3e758c29e0fde36fc088efcfc88f9a3014432b64
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
/* 
 * ----------------------------------------------------
 * Class - ArrayList 
 * ----------------------------------------------------
 */    
 ArrayList = function(){
     /* Properties */
     this.increment = 10;
     this.size = 0;
     this.data = new Array(this.increment);
 }
 
 ArrayList.EMPTY_LIST = new ArrayList();    
 ArrayList.prototype.type = "ArrayList";    
 
 // reverse the ArrayList
 ArrayList.prototype.reverse = function(){
     var newData = new Array(this.size);
     for (var i = 0; i < this.size; i++) {
         newData[i] = this.data[this.size - i - 1];
     }
     this.data = newData;
 }
 
 // getCapacity() -- returns the number of elements the vector can hold
 ArrayList.prototype.getCapacity = function(){
     return this.data.length;
 }
 
 // getSize() -- returns the current size of the vector
 ArrayList.prototype.getSize = function(){
     return this.size;
 }
 
 // isEmpty() -- checks to see if the Vector has any elements
 ArrayList.prototype.isEmpty = function(){
     return this.getSize() == 0;
 }
 
 // getLastElement() -- returns the last element
 ArrayList.prototype.getLastElement = function(){
     if (this.data[this.getSize() - 1] != null) {
         return this.data[this.getSize() - 1];
     }
 }
 
 // getFirstElement() -- returns the first element
 ArrayList.prototype.getFirstElement = function(){
     if (this.data[0] != null) {
         return this.data[0];
     }
 }
 
 // getElementAt() -- returns an element at a specified index
 ArrayList.prototype.get = function(/*:int*/i){
     return this.data[i];
 }
 
 // add() -- adds a element at the end of the Vector
 ArrayList.prototype.add = function(obj){
     if (this.getSize() == this.data.length) {
         this.resize();
     }
     this.data[this.size++] = obj;
 }
 
 // add() -- adds a element at the end of the Vector
 ArrayList.prototype.addAll = function(obj){
     for (var i = 0; i < obj.getSize(); i++) {
         this.add(obj.get(i));
     }
 }
 
 // indexOf() -- returns the index of a searched element
 ArrayList.prototype.remove = function(obj){
     var index = this.indexOf(obj);
     if (index >= 0) 
         return this.removeElementAt(index);
     return null;
 }
     
 // insertElementAt() -- inserts an element at a given position
 ArrayList.prototype.insertElementAt = function(obj, index){
     if (this.size == this.capacity) {
         this.resize();
     }
     
     for (var i = this.getSize(); i > index; i--) {
         this.data[i] = this.data[i - 1];
     }
     this.data[index] = obj;
     this.size++;
 }
 
 // removeElementAt() -- removes an element at a specific index
 ArrayList.prototype.removeElementAt = function(index){
     var element = this.data[index];
     
     for (var i = index; i < (this.getSize() - 1); i++) {
         this.data[i] = this.data[i + 1];
     }
     
     this.data[this.getSize() - 1] = null;
     this.size--;
     return element;
 }
 
 // removeAllElements() -- removes all elements in the Vector
 ArrayList.prototype.removeAllElements = function(){
     this.size = 0;
     
     for (var i = 0; i < this.data.length; i++) {
         this.data[i] = null;
     }
 }
 
 // indexOf() -- returns the index of a searched element
 ArrayList.prototype.indexOf = function(obj){
     for (var i = 0; i < this.getSize(); i++) {
         if (this.data[i] == obj) {
             return i;
         }
     }
     return -1;
 }
 
 // contains() -- returns true if the element is in the Vector, otherwise false
 ArrayList.prototype.contains = function(obj){
     for (var i = 0; i < this.getSize(); i++) {
         if (this.data[i] == obj) {
             return true;
         }
     }
     return false;
 }
 
 // resize() -- increases the size of the Vector
 ArrayList.prototype.resize = function(){
     newData = new Array(this.data.length + this.increment);
     
     for (var i = 0; i < this.data.length; i++) {
         newData[i] = this.data[i];
     }
     
     this.data = newData;
 }
     
 // trimToSize() -- trims the vector down to it's size
 ArrayList.prototype.trimToSize = function(){
     // nothing to do
     if (this.data.length == this.size) 
         return;
     
     var temp = new Array(this.getSize());
     
     for (var i = 0; i < this.getSize(); i++) {
         temp[i] = this.data[i];
     }
     this.size = temp.length;
     this.data = temp;
 }
 
 // sort() - sorts the collection based on a field name - f
 ArrayList.prototype.sort = function(f){
     var i, j;
     var currentValue;
     var currentObj;
     var compareObj;
     var compareValue;
     
     for (i = 1; i < this.getSize(); i++) {
         currentObj = this.data[i];
         currentValue = currentObj[f];
         
         j = i - 1;
         compareObj = this.data[j];
         compareValue = compareObj[f];
         
         while (j >= 0 && compareValue > currentValue) {
             this.data[j + 1] = this.data[j];
             j--;
             if (j >= 0) {
                 compareObj = this.data[j];
                 compareValue = compareObj[f];
             }
         }
         this.data[j + 1] = currentObj;
     }
 }
 
 // clone() -- copies the contents of a Vector to another Vector returning the new Vector.
 ArrayList.prototype.clone = function(){
     var newVector = new ArrayList(this.size);
     
     for (var i = 0; i < this.size; i++) {
         newVector.add(this.data[i]);
     }
     
     return newVector;
 }
 
 // overwriteElementAt() - overwrites the element with an object at the specific index.
 ArrayList.prototype.overwriteElementAt = function(obj, index){
     this.data[index] = obj;
 }
 
 ArrayList.prototype.getPersistentAttributes = function(){
     return {
         data: this.data,
         increment: this.increment,
         size: this.getSize()
     };
 }
/* 
 * ----------------------------------------------------
 * End - ArrayList 
 * ----------------------------------------------------
 */