1// Copyright (c) 1993-1999 Matra Datavision
2// Copyright (c) 1999-2014 OPEN CASCADE SAS
3//
4// This file is part of Open CASCADE Technology software library.
5//
6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
11//
12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
14
15#include <Standard_DimensionMismatch.hxx>
16#include <Standard_RangeError.hxx>
17#include <Standard_OutOfMemory.hxx>
18#include <Standard.hxx>
19
20//=======================================================================
21//function : Allocate
22//purpose  : Allocate memory for the array, set up indirection table
23//=======================================================================
24
25void TCollection_Array2::Allocate ()
26{
27  Standard_Integer RowSize    = myUpperColumn-myLowerColumn+1;
28  Standard_Integer ColumnSize = myUpperRow-myLowerRow+1;
29
30  if (myDeletable) {
31    // allocation of the data in the array
32
33    Standard_Integer Size = RowSize * ColumnSize;
34
35//  Modified by Sergey KHROMOV - Mon Feb 10 11:46:14 2003 Begin
36//     Standard_RangeError_Raise_if(( RowSize < 0  || ColumnSize < 0 ),
37// 				 "TCollection_Array2::Create");
38    Standard_RangeError_Raise_if(( RowSize <= 0  || ColumnSize <= 0 ),
39				 "TCollection_Array2::Create");
40//  Modified by Sergey KHROMOV - Mon Feb 10 11:46:15 2003 End
41    myData = new Array2Item [Size];
42
43    if (!myData) throw Standard_OutOfMemory("Array2 : Allocation failed");
44  }
45
46  // allocation of the indirection table (pointers on rows)
47  Array2Item*  p = (Array2Item*) myData;
48  Array2Item** q = (Array2Item**)Standard::Allocate(ColumnSize * sizeof(Array2Item*));
49
50  for (Standard_Integer i = 0; i < ColumnSize; i++) {
51    q[i] = p - myLowerColumn;
52    p += RowSize;
53  }
54
55  myData = (void*) (q - myLowerRow);
56}
57
58//=======================================================================
59//function : TCollection_Array2
60//purpose  :
61//=======================================================================
62
63TCollection_Array2::TCollection_Array2 (const Standard_Integer R1,
64                                        const Standard_Integer R2,
65                                        const Standard_Integer C1,
66                                        const Standard_Integer C2) :
67       myLowerRow(R1),
68       myLowerColumn(C1),
69       myUpperRow(R2),
70       myUpperColumn(C2),
71       myDeletable(Standard_True)
72{
73  Allocate ();
74}
75
76//=======================================================================
77//function : TCollection_Array2
78//purpose  : User allocated data
79//=======================================================================
80
81TCollection_Array2::TCollection_Array2 (const Array2Item& Item,
82					const Standard_Integer R1,
83					const Standard_Integer R2,
84					const Standard_Integer C1,
85					const Standard_Integer C2) :
86       myLowerRow(R1),
87       myLowerColumn(C1),
88       myUpperRow(R2),
89       myUpperColumn(C2),
90       myDeletable(Standard_False),
91       myData((void*)&Item)
92{
93  Allocate ();
94}
95
96//=======================================================================
97//function : Init
98//purpose  :
99//=======================================================================
100
101void TCollection_Array2::Init (const Array2Item& V)
102{
103  Standard_Integer Size = RowLength() * ColLength();
104  Array2Item* p = &(ChangeValue(myLowerRow,myLowerColumn));
105  for (Standard_Integer I = 0; I < Size ; I++) p[I] = V;
106}
107
108//=======================================================================
109//function : Destroy
110//purpose  :
111//=======================================================================
112
113void TCollection_Array2::Destroy ()
114{
115  Array2Item** anItemPtr = ((Array2Item**)myData + myLowerRow);
116
117  // delete the data
118  //
119  if (myDeletable)
120    delete [] &ChangeValue(myLowerRow,myLowerColumn);
121
122  // delete the indirection table
123  Standard::Free (anItemPtr);
124}
125
126//=======================================================================
127//function : Assign
128//purpose  :
129//=======================================================================
130
131const TCollection_Array2& TCollection_Array2::Assign
132  (const TCollection_Array2& Right)
133{
134  Standard_Integer MaxColumn = RowLength() ;
135  Standard_Integer MaxRow    = ColLength() ;
136  Standard_Integer MaxSize   = MaxColumn * MaxRow;
137
138  Standard_DimensionMismatch_Raise_if(MaxRow != Right.ColLength() ||
139                                      MaxColumn != Right.RowLength(),
140				      "Array2::Operator=");
141
142  Array2Item* p = &ChangeValue(myLowerRow,myLowerColumn);
143  const Array2Item* q = &Right.Value(Right.LowerRow(),Right.LowerCol());
144  for (Standard_Integer i=0; i<MaxSize; i++) {
145    p[i] = q[i];
146  }
147  return *this;
148}
149
150
151