cd4bdd1699284f8f313aee06e23b74eb030afc82
[occt.git] / src / math / math_SingleTab.gxx
1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19
20 #include <math_Memory.hxx>
21 #include <Standard_OutOfRange.hxx>
22 #include <Standard_Failure.hxx>
23
24 // macro to get size of C array
25 #define CARRAY_LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
26
27 math_SingleTab::math_SingleTab(const Standard_Integer LowerIndex,
28                                const Standard_Integer UpperIndex) :
29                    Addr(Buf),
30                    isAllocated(UpperIndex - LowerIndex + 1 > CARRAY_LENGTH(Buf)),
31                                First(LowerIndex), Last(UpperIndex)
32 {
33   Item* TheAddr = !isAllocated? Buf :
34     (Item*) Standard::Allocate((Last-First+1) * sizeof(Item));
35   Addr = (Standard_Address) (TheAddr - First);
36 }
37
38 math_SingleTab::math_SingleTab(const Item& Tab,
39                                const Standard_Integer LowerIndex,
40                                const Standard_Integer UpperIndex) :
41                                Addr((void*)(&Tab - LowerIndex)), 
42                                isAllocated(Standard_False),
43                                First(LowerIndex), Last(UpperIndex)
44                                
45 {
46 }
47
48 void math_SingleTab::Init(const Item& InitValue)
49 {
50   for (Standard_Integer i=First; i<= Last; i++) {
51     ((Item*)Addr)[i] = InitValue;
52   }
53 }
54
55
56 math_SingleTab::math_SingleTab(const math_SingleTab& Other) :
57
58   isAllocated(Other.Last - Other.First + 1 > CARRAY_LENGTH(Buf)),
59   First(Other.First),
60   Last(Other.Last)
61
62 {
63   Item* TheAddr = !isAllocated? Buf :
64     (Item*) Standard::Allocate((Last-First+1) * sizeof(Item));
65   Addr = (Standard_Address) (TheAddr - First);
66   Item* TheOtherAddr = (Item*) Other.Addr;
67   memmove((void*) TheAddr, (const void*) (TheOtherAddr + First),
68           (size_t)(Last - First + 1) * sizeof(Item));
69 }
70
71
72 void math_SingleTab::Free()
73 {
74   if(isAllocated) {
75     Standard_Address it = (Standard_Address)&((Item*)Addr)[First];
76     Standard::Free(it);
77     Addr = 0;
78   }
79 }
80
81
82 void math_SingleTab::SetLower(const Standard_Integer LowerIndex)
83 {
84   Item* TheAddr = (Item*) Addr;
85   Addr = (Standard_Address) (TheAddr + First - LowerIndex);
86   Last = Last - First + LowerIndex;
87   First = LowerIndex;
88 }
89
90
91
92