0026515: Exponential memory usage problems with BOPDS_IndexedMapOfPaveBlock and NColl...
[occt.git] / src / IntTools / IntTools_CArray1.gxx
1 // Created on: 2000-05-26
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 //=======================================================================
17 //function : IntTools_CArray1
18 //purpose  : 
19 //=======================================================================
20 IntTools_CArray1::IntTools_CArray1 (const Standard_Integer Length): 
21   myStart(NULL), 
22   myLength(0), 
23   myIsAllocated(Standard_False)
24 {
25   Resize(Length);
26 }
27
28 //=======================================================================
29 //function : IntTools_CArray1
30 //purpose  : 
31 //=======================================================================
32   IntTools_CArray1::IntTools_CArray1 (const Array1Item& Item,
33                                       const Standard_Integer Length):
34   myLength(Length), 
35   myIsAllocated(Standard_False)
36 {
37   Standard_ConstructionError_Raise_if(Length < 0,"IntTools_CArray1:: Length < 0");
38   myStart = (void*)(&Item); 
39 }
40
41 //=======================================================================
42 //function : Init
43 //purpose  : 
44 //=======================================================================
45   void IntTools_CArray1::Init (const Array1Item& V) 
46 {
47   Array1Item* p = (Array1Item*) myStart;
48   for(Standard_Integer i = 0; i < Length() ; i++) {
49     *p++ = V;
50   }
51 }
52
53 //=======================================================================
54 //function : Destroy
55 //purpose  : 
56 //=======================================================================
57   void IntTools_CArray1::Destroy() 
58 {
59   if (myIsAllocated) {
60     delete [] (Array1Item *)myStart;
61     myIsAllocated = Standard_False;
62   }
63   myStart = NULL;
64 }
65
66 //=======================================================================
67 //function : IsEqual
68 //purpose  : 
69 //=======================================================================
70   Standard_Boolean IntTools_CArray1::IsEqual(const IntTools_CArray1& Other) const
71 {
72   if (&Other == this)
73     return Standard_True;
74   else if (Length() != Other.Length()) 
75     return Standard_False;
76   else if (Length() == 0) 
77     return Standard_True;
78   //
79   return Standard_False;
80 }
81
82
83 //=======================================================================
84 //function : Resize
85 //purpose  : 
86 //=======================================================================
87   void IntTools_CArray1::Resize(const Standard_Integer theNewLength)
88 {
89   Standard_ConstructionError_Raise_if(theNewLength < 0,"IntTools_CArray1: length < 0");
90   
91   Array1Item* p = NULL;
92   
93   Destroy(); 
94
95   myLength = theNewLength;
96   
97   if (theNewLength > 0) {
98     // default creator called for each item of the array
99     p =  new Array1Item[theNewLength];
100     if (!p) Standard_OutOfMemory::Raise("IntTools_CArray1 : Allocation failed.");
101     myIsAllocated = Standard_True;
102   }
103   
104   myStart = (void*) p;
105 }
106
107 //=======================================================================
108 //function : Append
109 //purpose  : 
110 //=======================================================================
111   void IntTools_CArray1::Append (const Array1Item& Value)
112 {
113   const Standard_Integer theNewLength=myLength+1;
114
115   Array1Item* p = NULL;
116   
117   if (theNewLength > 0) {
118     // default creator called for each item of the array
119     p =  new Array1Item[theNewLength];
120     if (!p) Standard_OutOfMemory::Raise("IntTools_CArray1 : Allocation failed.");
121     
122     if (myLength!=0) {
123       Standard_Integer aBytesPerItem=sizeof(Array1Item);
124       memcpy (p, myStart, myLength*aBytesPerItem);
125     }
126
127     *(p+myLength)=Value;
128     Destroy();
129     myLength = theNewLength;
130     myIsAllocated = Standard_True;
131   }
132   
133   myStart = (void*) p;
134 }
135 //=======================================================================
136 //function : Value
137 //purpose  : 
138 //=======================================================================
139   const Array1Item& IntTools_CArray1::Value(const Standard_Integer Index) const
140 {
141   if (myLength <1 || Index < 0 || Index >= myLength)
142     Standard_OutOfRange::Raise("IntTools_CArray1::Value");
143   
144   return ((Array1Item *)myStart)[Index];
145 }
146
147 //=======================================================================
148 //function : SetValue
149 //purpose  : 
150 //=======================================================================
151   void IntTools_CArray1::SetValue (const Standard_Integer Index,
152                                    const Array1Item& Value)
153 {
154   ChangeValue(Index) = Value;
155 }
156
157
158 //=======================================================================
159 //function : ChangeValue
160 //purpose  : 
161 //=======================================================================
162   Array1Item& IntTools_CArray1::ChangeValue(const Standard_Integer Index)
163 {
164   if (myLength < 1 || Index < 0 || Index >= myLength)
165     Standard_OutOfRange::Raise("IntTools_CArray1::ChangeValue");
166
167   return ((Array1Item *)myStart)[Index];
168 }
169