1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: ftmpl_array.cc,v 1.9 2006-05-15 09:03:07 Singular Exp $ */ |
---|
3 | |
---|
4 | #include <factoryconf.h> |
---|
5 | |
---|
6 | #ifdef macintosh |
---|
7 | #include <:templates:ftmpl_array.h> |
---|
8 | #else |
---|
9 | #include <templates/ftmpl_array.h> |
---|
10 | #endif |
---|
11 | |
---|
12 | template <class T> |
---|
13 | Array<T>::Array() : data(0), _min(0), _max(-1), _size(0) |
---|
14 | { |
---|
15 | } |
---|
16 | |
---|
17 | template <class T> |
---|
18 | Array<T>::Array( const Array<T> & a ) |
---|
19 | { |
---|
20 | if ( a._size > 0 ) { |
---|
21 | _min = a._min; |
---|
22 | _max = a._max; |
---|
23 | _size = a._size; |
---|
24 | data = new T[_size]; |
---|
25 | for ( int i = 0; i < _size; i++ ) |
---|
26 | data[i] = a.data[i]; |
---|
27 | } |
---|
28 | else { |
---|
29 | data = 0; |
---|
30 | _min = _size = 0; |
---|
31 | _max = -1; |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | template <class T> |
---|
36 | Array<T>::Array( int i ) |
---|
37 | { |
---|
38 | _min = 0; |
---|
39 | _max = i-1; |
---|
40 | _size = i; |
---|
41 | if ( i == 0 ) |
---|
42 | data = 0; |
---|
43 | else |
---|
44 | data = new T[_size]; |
---|
45 | } |
---|
46 | |
---|
47 | template <class T> |
---|
48 | Array<T>::Array( int min, int max ) |
---|
49 | { |
---|
50 | if ( max < min ) { |
---|
51 | _min = _size = 0; |
---|
52 | _max = -1; |
---|
53 | data = 0; |
---|
54 | } |
---|
55 | else { |
---|
56 | _min = min; |
---|
57 | _max = max; |
---|
58 | _size = _max - _min + 1; |
---|
59 | data = new T[_size]; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | template <class T> |
---|
64 | Array<T>::~Array() |
---|
65 | { |
---|
66 | delete [] data; |
---|
67 | } |
---|
68 | |
---|
69 | template <class T> |
---|
70 | Array<T>& Array<T>::operator= ( const Array<T> & a ) |
---|
71 | { |
---|
72 | if ( this != &a ) { |
---|
73 | delete [] data; |
---|
74 | _min = a._min; |
---|
75 | _max = a._max; |
---|
76 | _size = a._size; |
---|
77 | if ( a._size > 0 ) { |
---|
78 | _size = a._size; |
---|
79 | data = new T[_size]; |
---|
80 | for ( int i = 0; i < _size; i++ ) |
---|
81 | data[i] = a.data[i]; |
---|
82 | } |
---|
83 | else { |
---|
84 | data = 0; |
---|
85 | _size = 0; |
---|
86 | } |
---|
87 | } |
---|
88 | return *this; |
---|
89 | } |
---|
90 | |
---|
91 | template <class T> |
---|
92 | T& Array<T>::operator[] ( int i ) const |
---|
93 | { |
---|
94 | ASSERT( i >= _min && i <= _max, "warning: array size mismatch." ); |
---|
95 | return data[i-_min]; |
---|
96 | } |
---|
97 | |
---|
98 | template <class T> |
---|
99 | int Array<T>::size() const |
---|
100 | { |
---|
101 | return _size; |
---|
102 | } |
---|
103 | |
---|
104 | template <class T> |
---|
105 | int Array<T>::min() const |
---|
106 | { |
---|
107 | return _min; |
---|
108 | } |
---|
109 | |
---|
110 | template <class T> |
---|
111 | int Array<T>::max() const |
---|
112 | { |
---|
113 | return _max; |
---|
114 | } |
---|
115 | |
---|
116 | /* |
---|
117 | template <class T> |
---|
118 | Array<T>& Array<T>::operator*= ( const T & t ) |
---|
119 | { |
---|
120 | for ( int i = 0; i < _size; i++ ) |
---|
121 | data[i] *= t; |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | template <class T> |
---|
126 | Array<T>& Array<T>::operator+= ( const T & t ) |
---|
127 | { |
---|
128 | for ( int i = 0; i < _size; i++ ) |
---|
129 | data[i] += t; |
---|
130 | return *this; |
---|
131 | } |
---|
132 | |
---|
133 | template <class T> |
---|
134 | Array<T>& Array<T>::operator+= ( const Array<T> & a ) |
---|
135 | { |
---|
136 | ASSERT ( _size == a._size, "warning: array size mismatch." ); |
---|
137 | for ( int i = 0; i < _size; i++ ) |
---|
138 | data[i] += a.data[i]; |
---|
139 | return *this; |
---|
140 | } |
---|
141 | */ |
---|
142 | |
---|
143 | #ifndef NOSTREAMIO |
---|
144 | template <class T> |
---|
145 | void Array<T>::print ( OSTREAM & os ) const |
---|
146 | { |
---|
147 | if ( _size == 0 ) |
---|
148 | os << "( )"; |
---|
149 | else { |
---|
150 | os << "( " << data[0]; |
---|
151 | for ( int i = 1; i < _size; i++ ) |
---|
152 | os << ", " << data[i]; |
---|
153 | os << " )"; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | template <class T> |
---|
158 | OSTREAM& operator<< ( OSTREAM & os, const Array<T> & a ) |
---|
159 | { |
---|
160 | a.print( os ); |
---|
161 | return os; |
---|
162 | } |
---|
163 | #endif /* NOSTREAMIO */ |
---|