OSDN Git Service

Compiler warnings removal Numerous tweaks to remove compiler warnings where solution...
[tortoisegit/TortoiseGitJp.git] / ext / scintilla / src / SVector.h
1 // Scintilla source code edit control\r
2 /** @file SVector.h\r
3  ** A simple expandable vector.\r
4  **/\r
5 // Copyright 1998-2001 by Neil Hodgson <neilh@hare.net.au>\r
6 // The License.txt file describes the conditions under which this software may be distributed.\r
7 \r
8 #ifndef SVECTOR_H\r
9 #define SVECTOR_H\r
10 \r
11 #ifdef SCI_NAMESPACE\r
12 namespace Scintilla {\r
13 #endif\r
14 \r
15 /**\r
16  * A simple expandable integer vector.\r
17  * Storage not allocated for elements until an element is used.\r
18  * This makes it very lightweight unless used so is a good match for optional features.\r
19  */\r
20 class SVector {\r
21         enum { allocSize = 4000 };\r
22         \r
23         int *v;                         ///< The vector\r
24         unsigned int size;      ///< Number of elements allocated\r
25         unsigned int len;       ///< Number of elements used in vector\r
26         bool allocFailure;      ///< A memory allocation call has failed\r
27         \r
28         /** Internally allocate more elements than the user wants\r
29          * to avoid thrashing the memory allocator. */\r
30         void SizeTo(int newSize) {\r
31                 if (newSize < allocSize)\r
32                         newSize += allocSize;\r
33                 else \r
34                         newSize = (newSize * 3) / 2;\r
35                 int* newv = new int[newSize];\r
36                 if (!newv) {\r
37                         allocFailure = true;\r
38                         return;\r
39                 }\r
40                 size = newSize;\r
41                 unsigned int i=0;\r
42                 for (; i<len; i++) {\r
43                         newv[i] = v[i];\r
44                 }\r
45                 for (; i<size; i++) {\r
46                         newv[i] = 0;\r
47                 }\r
48                 delete []v;\r
49                 v = newv;\r
50         }\r
51         \r
52 public:\r
53         SVector() {\r
54                 allocFailure = false;\r
55                 v = 0;\r
56                 len = 0;\r
57                 size = 0;\r
58         }\r
59         ~SVector() {\r
60                 Free();\r
61         }\r
62         /// Constructor from another vector.\r
63         SVector(const SVector &other) {\r
64                 allocFailure = false;\r
65                 v = 0;\r
66                 len = 0;\r
67                 size = 0;\r
68                 if (other.Length() > 0) {\r
69                         SizeTo(other.Length());\r
70                         if (!allocFailure) {\r
71                                 for (int i=0;i<other.Length();i++)\r
72                                         v[i] = other.v[i];\r
73                                 len = other.Length();\r
74                         }\r
75                 }\r
76         }\r
77         /// Copy constructor.\r
78         SVector &operator=(const SVector &other) {\r
79                 if (this != &other) {\r
80                         delete []v;\r
81                         allocFailure = false;\r
82                         v = 0;\r
83                         len = 0;\r
84                         size = 0;\r
85                         if (other.Length() > 0) {\r
86                                 SizeTo(other.Length());\r
87                                 if (!allocFailure) {\r
88                                         for (int i=0;i<other.Length();i++)\r
89                                                 v[i] = other.v[i];\r
90                                 }\r
91                                 len = other.Length();\r
92                         }\r
93                 }\r
94                 return *this;\r
95         }\r
96         /** @brief Accessor.\r
97          * Allows to access values from the list, and grows it if accessing\r
98          * outside the current bounds. The returned value in this case is 0. */\r
99         int &operator[](unsigned int i) {\r
100                 if (i >= len) {\r
101                         if (i >= size) {\r
102                                 SizeTo(i);\r
103                         }\r
104                         len = i+1;\r
105                 }\r
106                 return v[i];\r
107         }\r
108         /// Reset vector.\r
109         void Free() {\r
110                 delete []v;\r
111                 v = 0;\r
112                 size = 0;\r
113                 len = 0;\r
114         }\r
115         /** @brief Grow vector size.\r
116          * Doesn't allow a vector to be shrinked. */\r
117         void SetLength(unsigned int newLength) {\r
118                 if (newLength > len) {\r
119                         if (newLength >= size) {\r
120                                 SizeTo(newLength);\r
121                         }\r
122                 }\r
123                 len = newLength;\r
124         }\r
125         /// Get the current length (number of used elements) of the vector.\r
126         int Length() const {\r
127                 return len;\r
128         }\r
129 };\r
130 \r
131 #ifdef SCI_NAMESPACE\r
132 }\r
133 #endif\r
134 \r
135 #endif\r