Integration of OCCT 6.5.0 from SVN
[occt.git] / src / TCollection / TCollection_Array2.gxx
1 #include <Standard_DimensionMismatch.hxx>
2 #include <Standard_RangeError.hxx>
3 #include <Standard_OutOfMemory.hxx>
4 #include <Standard.hxx>
5
6 //=======================================================================
7 //function : Allocate
8 //purpose  : Allocate memory for the array, set up indirection table
9 //=======================================================================
10
11 void TCollection_Array2::Allocate ()
12 {
13   Standard_Integer RowSize    = myUpperColumn-myLowerColumn+1;
14   Standard_Integer ColumnSize = myUpperRow-myLowerRow+1;
15
16   if (myDeletable) {
17     // allocation of the data in the array
18      
19     Standard_Integer Size = RowSize * ColumnSize;
20     
21 //  Modified by Sergey KHROMOV - Mon Feb 10 11:46:14 2003 Begin
22 //     Standard_RangeError_Raise_if(( RowSize < 0  || ColumnSize < 0 ),
23 //                               "TCollection_Array2::Create");
24     Standard_RangeError_Raise_if(( RowSize <= 0  || ColumnSize <= 0 ),
25                                  "TCollection_Array2::Create");
26 //  Modified by Sergey KHROMOV - Mon Feb 10 11:46:15 2003 End
27 #ifdef __OPTIM_ARRAY
28     myData = Standard::Allocate(Size*sizeof (Array2Item));
29     //    myData = (Array2Item *) new char [Size * sizeof (Array2Item)];
30 #else 
31     myData = new Array2Item [Size];
32 #endif
33
34     if (!myData) Standard_OutOfMemory::Raise("Array2 : Allocation failed");
35   }
36   
37   // allocation of the indirection table (pointers on rows)
38   Array2Item*  p = (Array2Item*) myData;
39   Array2Item** q = (Array2Item**)Standard::Allocate(ColumnSize * sizeof(Array2Item*));
40
41   for (Standard_Integer i = 0; i < ColumnSize; i++) {
42     q[i] = p - myLowerColumn;
43     p += RowSize;
44   }
45   
46   myData = (void*) (q - myLowerRow);
47 }
48
49 //=======================================================================
50 //function : TCollection_Array2
51 //purpose  : 
52 //=======================================================================
53
54 TCollection_Array2::TCollection_Array2 (const Standard_Integer R1, 
55                                         const Standard_Integer R2,
56                                         const Standard_Integer C1, 
57                                         const Standard_Integer C2) :
58        myLowerRow(R1),
59        myLowerColumn(C1),
60        myUpperRow(R2),
61        myUpperColumn(C2),
62        myDeletable(Standard_True)
63 {
64   Allocate ();
65 }
66
67 //=======================================================================
68 //function : TCollection_Array2
69 //purpose  : User allocated data
70 //=======================================================================
71
72 TCollection_Array2::TCollection_Array2 (const Array2Item& Item,
73                                         const Standard_Integer R1, 
74                                         const Standard_Integer R2,
75                                         const Standard_Integer C1, 
76                                         const Standard_Integer C2) :
77        myLowerRow(R1),
78        myLowerColumn(C1),
79        myUpperRow(R2),
80        myUpperColumn(C2),
81        myDeletable(Standard_False),
82        myData((void*)&Item)
83 {
84   Allocate ();
85 }
86
87
88 //=======================================================================
89 //function : Init
90 //purpose  : 
91 //=======================================================================
92
93 void TCollection_Array2::Init (const Array2Item& V) 
94 {
95   Standard_Integer Size = RowLength() * ColLength();
96   Array2Item* p = &(ChangeValue(myLowerRow,myLowerColumn));
97   for (Standard_Integer I = 0; I < Size ; I++) p[I] = V;
98 }
99
100 //=======================================================================
101 //function : Destroy
102 //purpose  : 
103 //=======================================================================
104
105 void TCollection_Array2::Destroy () 
106 {
107   Array2Item** anItemPtr = ((Array2Item**)myData + myLowerRow);
108   
109   // delete the data
110   //
111   if (myDeletable) 
112 #ifdef __OPTIM_ARRAY
113     {
114     Standard_Integer RowSize    = myUpperColumn-myLowerColumn+1;
115     Standard_Integer ColumnSize = myUpperRow-myLowerRow+1;
116     Standard_Integer Size = RowSize * ColumnSize;
117     Standard_Address it = (Standard_Address)&((Array2Item **)myData)[myLowerRow][myLowerColumn];
118     Standard::Free(it);  
119   }
120 #else
121   delete [] &ChangeValue(myLowerRow,myLowerColumn);
122 #endif
123
124   // delete the indirection table
125   Standard::Free((void*&)anItemPtr);
126 }
127
128 //=======================================================================
129 //function : Assign
130 //purpose  : 
131 //=======================================================================
132
133 const TCollection_Array2& TCollection_Array2::Assign 
134   (const TCollection_Array2& Right)
135 {
136   Standard_Integer MaxColumn = RowLength() ;
137   Standard_Integer MaxRow    = ColLength() ;
138   Standard_Integer MaxSize   = MaxColumn * MaxRow;
139
140   Standard_DimensionMismatch_Raise_if(MaxRow != Right.ColLength() ||
141                                       MaxColumn != Right.RowLength(),
142                                       "Array2::Operator=");
143
144   Array2Item* p = &ChangeValue(myLowerRow,myLowerColumn);
145   const Array2Item* q = &Right.Value(Right.LowerRow(),Right.LowerCol());
146   for (Standard_Integer i=0; i<MaxSize; i++) {
147     p[i] = q[i];
148   }
149   return *this;
150 }
151
152