summaryrefslogtreecommitdiff
path: root/inc/BOPTColStd_CArray1.gxx
blob: 6b13fba59dc3e947da72b56a88cbd883e3cfab8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// File:	BOPTools_CArray1.gxx
// Created:	Tue Nov 14 11:18:24 2000
// Author:	Peter KURNEV
//		<pkv@irinox>

//=======================================================================
//function : BOPTools_CArray1::BOPTools_CArray1
//purpose  : 
//=======================================================================
  BOPTColStd_CArray1::BOPTColStd_CArray1
       (const Standard_Integer aLength,
	const Standard_Integer aBlockLength)
  :  
  myStart(NULL), 
  myLength(0), 
  myFactLength(0), 
  myBlockLength(aBlockLength),
  myIsAllocated(Standard_False)
{
  Resize(aLength);
}
//=======================================================================
//function : Resize
//purpose  : 
//=======================================================================
  void BOPTColStd_CArray1::Resize(const Standard_Integer aNL) 
{
  Array1Item* p = NULL;
  if (aNL>0) {
    Destroy();
    
    p=new Array1Item[aNL];
    
    if (!p) {
      Standard_OutOfMemory::Raise
	("IntBOPTools_CArray1 : Allocation failed.");
    }

    else {
      myIsAllocated=Standard_True;
      myLength=aNL;
      myFactLength=myLength;
      myStart = (void*) p;
    }
  }
}
//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================
  void BOPTColStd_CArray1::Remove(const Standard_Integer anInd)
{
  if (myIsAllocated)  {
    if (IsInvalidIndex(anInd)) {
      Standard_OutOfMemory::Raise
	("IntBOPTools_CArray1 : Attempt to remove inexisting Item.");
    }
    
    const Standard_Integer aNFL=myFactLength-1;
  
    Array1Item *p=NULL;
    p = new Array1Item[aNFL];
  
    if (!p) {
      Standard_OutOfMemory::Raise
	("IntBOPTools_CArray1::Append: Allocation failed.");
    }

    Standard_Integer i, j, anIndx, iLength;

    iLength=myLength;

    anIndx=anInd-1;
    for (i=0, j=0; i<myLength; ++i) {
      if (i!=anIndx) {
	p[j]= ((Array1Item *)myStart)[i];
	j++;
      }
    }

    Destroy();

    myFactLength=aNFL;
    myLength=iLength-1;
    myIsAllocated=Standard_True;
    myStart = (void*) p;
  }
}
//=======================================================================
//function : Append
//purpose  : 
//=======================================================================
  Standard_Integer BOPTColStd_CArray1::Append(const Array1Item& aV) 
{
  Standard_Integer i, aNL;

  aNL=myLength+1;
  if (aNL > myFactLength) {

    const Standard_Integer iLengthToAllocate=myLength+myBlockLength;
    
    Array1Item *p=NULL;
    p = new Array1Item[iLengthToAllocate];
  
    if (!p) {
      Standard_OutOfMemory::Raise
	("IntBOPTools_CArray1::Append: Allocation failed.");
    }
    
    for (i=0; i<myLength; i++) {
      p[i]=((Array1Item *)myStart)[i];
    }
    p[myLength]=aV;
    
    Destroy();

    myFactLength=iLengthToAllocate;
    myIsAllocated=Standard_True;
    myStart = (void*) p;
  }
  
  else {
    ((Array1Item *)myStart)[myLength]=aV; 
  }

  myLength=aNL;
  return myLength;
}
//=======================================================================
//function : IsInvalidIndex
//purpose  : 
//=======================================================================
  Standard_Boolean BOPTColStd_CArray1::IsInvalidIndex
        (const Standard_Integer anInd)const
{
  Standard_Boolean aFlag;
  Standard_Integer anIndx=anInd-1;
  aFlag=!(anIndx > -1 && anIndx < myLength);
  return aFlag;
}
//=======================================================================
//function : Destroy
//purpose  : 
//=======================================================================
  void BOPTColStd_CArray1::Destroy() 
{
  if (myIsAllocated) {
    delete [] (Array1Item *)myStart;
    myIsAllocated=Standard_False;
    myFactLength=0;
    myLength=0;
    myStart=NULL;
  }
  //myStart=NULL;
}
//=======================================================================
//function : Length
//purpose  : 
//=======================================================================
  Standard_Integer BOPTColStd_CArray1::Length() const
{
  return myLength;
}
//=======================================================================
//function : Extent
//purpose  : 
//=======================================================================
  Standard_Integer BOPTColStd_CArray1::Extent() const
{
  return myLength;
}
//=======================================================================
//function : FactLength
//purpose  : 
//=======================================================================
  Standard_Integer BOPTColStd_CArray1::FactLength() const
{
  return myFactLength;
}
//=======================================================================
//function : BlockLength
//purpose  : 
//=======================================================================
  Standard_Integer BOPTColStd_CArray1::BlockLength() const
{
  return myBlockLength;
}
//=======================================================================
//function : SetBlockLength
//purpose  : 
//=======================================================================
  void BOPTColStd_CArray1::SetBlockLength(const Standard_Integer aBL) 
{
  if (aBL > 0)
    myBlockLength=aBL;
}
//=======================================================================
//function : Value
//purpose  : 
//=======================================================================
  const Array1Item& BOPTColStd_CArray1::Value
      (const Standard_Integer Index) const
{
  if (IsInvalidIndex(Index)) {
    Standard_OutOfRange::Raise("BOPTools_CArray1::Value");
  }
  return ((Array1Item *)myStart)[Index-1];
}
//=======================================================================
//function : ChangeValue
//purpose  : 
//=======================================================================
  Array1Item& BOPTColStd_CArray1::ChangeValue
      (const Standard_Integer Index) 
{
  if (IsInvalidIndex(Index)) {
    Standard_OutOfRange::Raise("BOPTools_CArray1::ChangeValue");
  }
  return ((Array1Item *)myStart)[Index-1];
}