Open-Source-Software-Entwicklung und Downloads

Browse Subversion Repository

Diff of /trunk/TTProxy/YCL/include/YCL/Vector.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3221 by maya, Tue Mar 24 09:37:20 2009 UTC revision 3227 by maya, Tue Mar 24 15:10:33 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * $Id: Vector.h,v 1.4 2007-08-18 08:52:18 maya Exp $   * $Id: Vector.h,v 1.4 2007-08-18 08:52:18 maya Exp $
3   */   */
4    
5  #ifndef _YCL_VECTOR_H_  #ifndef _YCL_VECTOR_H_
6  #define _YCL_VECTOR_H_  #define _YCL_VECTOR_H_
7    
8  #if _MSC_VER >= 1000  #if _MSC_VER >= 1000
9  #pragma once  #pragma once
10  #endif // _MSC_VER >= 1000  #endif // _MSC_VER >= 1000
11    
12  #include <YCL/common.h>  #include <YCL/common.h>
13    
14  #include <YCL/ValueCtrl.h>  #include <YCL/ValueCtrl.h>
15  #include <YCL/Pointer.h>  #include <YCL/Pointer.h>
16  #include <YCL/Enumeration.h>  #include <YCL/Enumeration.h>
17  #include <YCL/Array.h>  #include <YCL/Array.h>
18    
19  namespace yebisuya {  namespace yebisuya {
20    
21  template<class TYPE, class CTRL = ValueCtrl<TYPE> >  template<class TYPE, class CTRL = ValueCtrl<TYPE> >
22  class Vector {  class Vector {
23  private:  private:
24          enum {          enum {
25                  BOUNDARY = 8,                  BOUNDARY = 8,
26          };          };
27          class EnumElements : public Enumeration<TYPE> {          class EnumElements : public Enumeration<TYPE> {
28          private:          private:
29                  TYPE* array;                  TYPE* array;
30                  int size;                  int size;
31                  mutable int index;                  mutable int index;
32          public:          public:
33                  EnumElements(TYPE* array, int size):array(array), size(size), index(0) {                  EnumElements(TYPE* array, int size):array(array), size(size), index(0) {
34                  }                  }
35                  virtual bool hasMoreElements()const {                  virtual bool hasMoreElements()const {
36                          return index < size;                          return index < size;
37                  }                  }
38                  virtual TYPE nextElement()const {                  virtual TYPE nextElement()const {
39                          return array[index++];                          return array[index++];
40                  }                  }
41          };          };
42    
43          TYPE* array;          TYPE* array;
44          int arraySize;          int arraySize;
45          int arrayLength;          int arrayLength;
46    
47          Vector(Vector<TYPE,CTRL>&);          Vector(Vector<TYPE,CTRL>&);
48          void operator=(Vector<TYPE,CTRL>&);          void operator=(Vector<TYPE,CTRL>&);
49  public:  public:
50          Vector():array(NULL), arraySize(0), arrayLength(0) {          Vector():array(NULL), arraySize(0), arrayLength(0) {
51          }          }
52          Vector(int size):array(NULL), arraySize(0), arrayLength(0) {          Vector(int size):array(NULL), arraySize(0), arrayLength(0) {
53                  setSize(size);                  setSize(size);
54          }          }
55          ~Vector() {          ~Vector() {
56                  removeAll();                  removeAll();
57          }          }
58          int size()const {          int size()const {
59                  return arraySize;                  return arraySize;
60          }          }
61          TYPE get(int index)const {          TYPE get(int index)const {
62                  TYPE result;                  TYPE result;
63                  if (0 <= index && index < arraySize) {                  if (0 <= index && index < arraySize) {
64                          result = array[index];                          result = array[index];
65                  }else{                  }else{
66                          CTRL::initialize(result);                          CTRL::initialize(result);
67                  }                  }
68                  return result;                  return result;
69          }          }
70    
71          void setSize(int newSize) {          void setSize(int newSize) {
72                  if (newSize != arraySize) {                  if (newSize != arraySize) {
73                          if (newSize > arrayLength) {                          if (newSize > arrayLength) {
74                                  int newLength = (newSize + 7) & ~7;                                  int newLength = (newSize + 7) & ~7;
75                                  TYPE* newArray = new TYPE[newLength];                                  TYPE* newArray = new TYPE[newLength];
76                                  int i;                                  int i;
77                                  for (i = 0; i < arraySize; i++) {                                  for (i = 0; i < arraySize; i++) {
78                                          newArray[i] = array[i];                                          newArray[i] = array[i];
79                                  }                                  }
80                                  for (; i < newSize; i++) {                                  for (; i < newSize; i++) {
81                                          CTRL::initialize(newArray[i]);                                          CTRL::initialize(newArray[i]);
82                                  }                                  }
83                                  arrayLength = newLength;                                  arrayLength = newLength;
84                                  delete[] array;                                  delete[] array;
85                                  array = newArray;                                  array = newArray;
86                          }                          }
87                          for (int i = newSize; i < arraySize && i < arrayLength; i++) {                          for (int i = newSize; i < arraySize && i < arrayLength; i++) {
88                                  CTRL::initialize(array[i]);                                  CTRL::initialize(array[i]);
89                          }                          }
90                          arraySize = newSize;                          arraySize = newSize;
91                  }                  }
92          }          }
93          void set(int index, const TYPE& value) {          void set(int index, const TYPE& value) {
94                  if (0 <= index && index < arraySize) {                  if (0 <= index && index < arraySize) {
95                          array[index] = value;                          array[index] = value;
96                  }                  }
97          }          }
98          void add(const TYPE& value) {          void add(const TYPE& value) {
99                  add(arraySize, value);                  add(arraySize, value);
100          }          }
101          void add(int index, const TYPE& value) {          void add(int index, const TYPE& value) {
102                  if (index < 0)                  if (index < 0)
103                          index = 0;                          index = 0;
104                  if (index > arraySize)                  if (index > arraySize)
105                          index = arraySize;                          index = arraySize;
106                  setSize(arraySize + 1);                  setSize(arraySize + 1);
107                  for (int i = arraySize - 1; i > index; i--) {                  for (int i = arraySize - 1; i > index; i--) {
108                          array[i] = array[i - 1];                          array[i] = array[i - 1];
109                  }                  }
110                  array[index] = value;                  array[index] = value;
111          }          }
112          Pointer<Enumeration<TYPE> > elements()const {          Pointer<Enumeration<TYPE> > elements()const {
113                  return new EnumElements(array, arraySize);                  return new EnumElements(array, arraySize);
114          }          }
115          int indexOf(const TYPE& value)const {          int indexOf(const TYPE& value)const {
116                  return indexOf(value, 0);                  return indexOf(value, 0);
117          }          }
118          int indexOf(const TYPE& value, int index)const {          int indexOf(const TYPE& value, int index)const {
119                  for (int i = index; i < arraySize; i++) {                  for (int i = index; i < arraySize; i++) {
120                          if (array[i] == value)                          if (array[i] == value)
121                                  return i;                                  return i;
122                  }                  }
123                  return -1;                  return -1;
124          }          }
125          int lastIndexOf(const TYPE& value)const {          int lastIndexOf(const TYPE& value)const {
126                  return indexOf(value, arraySize - 1);                  return indexOf(value, arraySize - 1);
127          }          }
128          int lastIndexOf(const TYPE& value, int index)const {          int lastIndexOf(const TYPE& value, int index)const {
129                  for (int i = index; i >= 0; i--) {                  for (int i = index; i >= 0; i--) {
130                          if (array[i] == value)                          if (array[i] == value)
131                                  return i;                                  return i;
132                  }                  }
133                  return -1;                  return -1;
134          }          }
135          bool isEmpty()const {          bool isEmpty()const {
136                  return arraySize == 0;                  return arraySize == 0;
137          }          }
138          TYPE firstElement()const {          TYPE firstElement()const {
139                  return get(0);                  return get(0);
140          }          }
141          TYPE lastElement()const {          TYPE lastElement()const {
142                  return get(arraySize - 1);                  return get(arraySize - 1);
143          }          }
144          TYPE remove(int index) {          TYPE remove(int index) {
145                  TYPE removed = get(index);                  TYPE removed = get(index);
146                  for (int i = index; i < arraySize - 1; i++) {                  for (int i = index; i < arraySize - 1; i++) {
147                          array[i] = array[i + 1];                          array[i] = array[i + 1];
148                  }                  }
149                  setSize(arraySize - 1);                  setSize(arraySize - 1);
150                  return removed;                  return removed;
151          }          }
152          bool remove(const TYPE& value) {          bool remove(const TYPE& value) {
153                  int index = indexOf(value);                  int index = indexOf(value);
154                  if (index < 0)                  if (index < 0)
155                          return false;                          return false;
156                  remove(index);                  remove(index);
157                  return true;                  return true;
158          }          }
159          void removeAll() {          void removeAll() {
160                  delete[] array;                  delete[] array;
161                  array = NULL;                  array = NULL;
162                  arraySize = 0;                  arraySize = 0;
163                  arrayLength = 0;                  arrayLength = 0;
164          }          }
165          PointerArray<TYPE> toArray()const {          PointerArray<TYPE> toArray()const {
166                  return toArray(new Array<TYPE>(arraySize));                  return toArray(new Array<TYPE>(arraySize));
167          }          }
168          PointerArray<TYPE> toArray(PointerArray<TYPE> copyto)const {          PointerArray<TYPE> toArray(PointerArray<TYPE> copyto)const {
169                  int i;                  int i;
170                  for (i = 0; i < (int) copyto->length && i < arraySize; i++) {                  for (i = 0; i < (int) copyto->length && i < arraySize; i++) {
171                          copyto[i] = array[i];                          copyto[i] = array[i];
172                  }                  }
173                  for (; i < (int) copyto->length; i++) {                  for (; i < (int) copyto->length; i++) {
174                          CTRL::initialize(copyto[i]);                          CTRL::initialize(copyto[i]);
175                  }                  }
176                  return copyto;                  return copyto;
177          }          }
178          void trimToSize() {          void trimToSize() {
179                  TYPE* newArray = array;                  TYPE* newArray = array;
180                  if (arraySize == 0) {                  if (arraySize == 0) {
181                          newArray = NULL;                          newArray = NULL;
182                  }else if (arraySize < arrayLength) {                  }else if (arraySize < arrayLength) {
183                          newArray = new TYPE[arraySize];                          newArray = new TYPE[arraySize];
184                          for (int i = 0; i < arraySize; i++) {                          for (int i = 0; i < arraySize; i++) {
185                                  newArray[i] = array[i];                                  newArray[i] = array[i];
186                          }                          }
187                          arrayLength = arraySize;                          arrayLength = arraySize;
188                  }                  }
189                  if (newArray != array) {                  if (newArray != array) {
190                          delete[] array;                          delete[] array;
191                          array = newArray;                          array = newArray;
192                  }                  }
193          }          }
194          TYPE operator[](int index)const {          TYPE operator[](int index)const {
195                  return get(index);                  return get(index);
196          }          }
197  };  };
198    
199  }  }
200    
201  #endif//_YCL_VECTOR_H_  #endif//_YCL_VECTOR_H_

Legend:
Removed from v.3221  
changed lines
  Added in v.3227

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26