b311480e |
1 | // Created on: 2000-05-26 |
2 | // Created by: Peter KURNEV |
3 | // Copyright (c) 2000-2012 OPEN CASCADE SAS |
4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
9 | // |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
12 | // |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
19 | |
7fd59977 |
20 | |
21 | //======================================================================= |
22 | //function : IntTools_CArray1 |
23 | //purpose : |
24 | //======================================================================= |
b311480e |
25 | IntTools_CArray1::IntTools_CArray1 (const Standard_Integer Length): |
7fd59977 |
26 | myStart(NULL), |
27 | myLength(0), |
28 | myIsAllocated(Standard_False) |
29 | { |
30 | Resize(Length); |
31 | } |
32 | |
33 | //======================================================================= |
34 | //function : IntTools_CArray1 |
35 | //purpose : |
36 | //======================================================================= |
37 | IntTools_CArray1::IntTools_CArray1 (const Array1Item& Item, |
38 | const Standard_Integer Length): |
39 | myLength(Length), |
40 | myIsAllocated(Standard_False) |
41 | { |
42 | Standard_ConstructionError_Raise_if(Length < 0,"IntTools_CArray1:: Length < 0"); |
43 | myStart = (void*)(&Item); |
44 | } |
45 | |
46 | //======================================================================= |
47 | //function : Init |
48 | //purpose : |
49 | //======================================================================= |
50 | void IntTools_CArray1::Init (const Array1Item& V) |
51 | { |
52 | Array1Item* p = (Array1Item*) myStart; |
53 | for(Standard_Integer i = 0; i < Length() ; i++) { |
54 | *p++ = V; |
55 | } |
56 | } |
57 | |
58 | //======================================================================= |
59 | //function : Destroy |
60 | //purpose : |
61 | //======================================================================= |
62 | void IntTools_CArray1::Destroy() |
63 | { |
64 | if (myIsAllocated) { |
65 | delete [] (Array1Item *)myStart; |
66 | myIsAllocated = Standard_False; |
67 | } |
68 | myStart = NULL; |
69 | } |
70 | |
71 | //======================================================================= |
72 | //function : IsEqual |
73 | //purpose : |
74 | //======================================================================= |
75 | Standard_Boolean IntTools_CArray1::IsEqual(const IntTools_CArray1& Other) const |
76 | { |
77 | if (&Other == this) |
78 | return Standard_True; |
79 | else if (Length() != Other.Length()) |
80 | return Standard_False; |
81 | else if (Length() == 0) |
82 | return Standard_True; |
83 | // |
84 | return Standard_False; |
85 | } |
86 | |
87 | |
88 | //======================================================================= |
89 | //function : Resize |
90 | //purpose : |
91 | //======================================================================= |
92 | void IntTools_CArray1::Resize(const Standard_Integer theNewLength) |
93 | { |
94 | Standard_ConstructionError_Raise_if(theNewLength < 0,"IntTools_CArray1: length < 0"); |
95 | |
96 | Array1Item* p = NULL; |
97 | |
98 | Destroy(); |
99 | |
100 | myLength = theNewLength; |
101 | |
102 | if (theNewLength > 0) { |
103 | // default creator called for each item of the array |
104 | p = new Array1Item[theNewLength]; |
105 | if (!p) Standard_OutOfMemory::Raise("IntTools_CArray1 : Allocation failed."); |
106 | myIsAllocated = Standard_True; |
107 | } |
108 | |
109 | myStart = (void*) p; |
110 | } |
111 | |
112 | //======================================================================= |
113 | //function : Append |
114 | //purpose : |
115 | //======================================================================= |
116 | void IntTools_CArray1::Append (const Array1Item& Value) |
117 | { |
118 | const Standard_Integer theNewLength=myLength+1; |
119 | |
120 | Array1Item* p = NULL; |
121 | |
122 | if (theNewLength > 0) { |
123 | // default creator called for each item of the array |
124 | p = new Array1Item[theNewLength]; |
125 | if (!p) Standard_OutOfMemory::Raise("IntTools_CArray1 : Allocation failed."); |
126 | |
127 | if (myLength!=0) { |
128 | Standard_Integer aBytesPerItem=sizeof(Array1Item); |
129 | memcpy (p, myStart, myLength*aBytesPerItem); |
130 | } |
131 | |
132 | *(p+myLength)=Value; |
133 | Destroy(); |
134 | myLength = theNewLength; |
135 | myIsAllocated = Standard_True; |
136 | } |
137 | |
138 | myStart = (void*) p; |
139 | } |
140 | //======================================================================= |
141 | //function : Value |
142 | //purpose : |
143 | //======================================================================= |
144 | const Array1Item& IntTools_CArray1::Value(const Standard_Integer Index) const |
145 | { |
146 | if (myLength <1 || Index < 0 || Index >= myLength) |
147 | Standard_OutOfRange::Raise("IntTools_CArray1::Value"); |
148 | |
149 | return ((Array1Item *)myStart)[Index]; |
150 | } |
151 | |
152 | //======================================================================= |
153 | //function : SetValue |
154 | //purpose : |
155 | //======================================================================= |
156 | void IntTools_CArray1::SetValue (const Standard_Integer Index, |
157 | const Array1Item& Value) |
158 | { |
159 | ChangeValue(Index) = Value; |
160 | } |
161 | |
162 | |
163 | //======================================================================= |
164 | //function : ChangeValue |
165 | //purpose : |
166 | //======================================================================= |
167 | Array1Item& IntTools_CArray1::ChangeValue(const Standard_Integer Index) |
168 | { |
169 | if (myLength < 1 || Index < 0 || Index >= myLength) |
170 | Standard_OutOfRange::Raise("IntTools_CArray1::ChangeValue"); |
171 | |
172 | return ((Array1Item *)myStart)[Index]; |
173 | } |
174 | |