1 /* Ultimate Othello 1678
2  * Copyright (C) 2002  Florent Boudet <flobo@ifrance.com>
3  * iOS Software <http://www.ios-software.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  *
20  */
21 
22 #include "IosVector.h"
23 #include "IosException.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 
IosVector()27 IosVector::IosVector()
28 {
29 	vectorData = (void **) malloc(sizeof(void *) * vectorInitialSize);
30 	vectorCapacity = vectorInitialSize;
31 	vectorSize = 0;
32 }
33 
IosVector(int size)34 IosVector::IosVector(int size)
35 {
36 	vectorData = (void **) malloc(sizeof(void *) * size);
37 	vectorCapacity = size;
38 	vectorSize = 0;
39 }
40 
41 
~IosVector()42 IosVector::~IosVector()
43 {
44 }
45 
addElement(void * element)46 void IosVector::addElement(void *element)
47 {
48 	if (vectorSize >= vectorCapacity)
49 		increaseVectorSize();
50 	vectorData[vectorSize++] = element;
51 }
52 
getElementAt(const int index) const53 void * IosVector::getElementAt(const int index) const
54 {
55 	if (index >= vectorSize)
56 		throw new Exception("Vector index out of bounds");
57 	return vectorData[index];
58 }
59 
removeElementAt(const int index)60 void IosVector::removeElementAt(const int index)
61 {
62 	if (index >= vectorSize)
63 		throw new Exception("Vector index out of bounds");
64 	vectorSize--;
65 	if (index == vectorSize)
66 		return;
67 	for (int i = index ; i < vectorSize ; i++) {
68 		vectorData[i] = vectorData[i+1];
69 	}
70 }
71 
removeElement(void * element)72 void IosVector::removeElement(void *element)
73 {
74 	for (int i = 0 ; i < vectorSize ; i++) {
75 		if (vectorData[i] == element) {
76 			removeElementAt(i--);
77 		}
78 	}
79 }
80 
removeAllElements()81 void IosVector::removeAllElements()
82 {
83 	vectorSize = 0;
84 }
85 
getSize() const86 int IosVector::getSize() const
87 {
88 	return vectorSize;
89 }
90 
getCapacity() const91 int IosVector::getCapacity() const
92 {
93 	return vectorCapacity;
94 }
95 
increaseVectorSize()96 void IosVector::increaseVectorSize()
97 {
98 	vectorData = (void **) realloc(vectorData, sizeof(void *) * (vectorSize + vectorSizeIncrement));
99 	vectorCapacity += vectorSizeIncrement;
100 }
101 
dumpVector() const102 void IosVector::dumpVector() const {
103 	fprintf(stderr, "Size: %d\n", getSize());
104 	for (int i = 0, j = getSize() ; i < j ; i++)
105 		fprintf(stderr, "elt[%d]=%p ", i, getElementAt(i));
106 	fprintf(stderr, "\n");
107 }
108