1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: ftmpl_array.cc,v 1.4 1997-04-30 13:08:23 schmidt Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | $Log: not supported by cvs2svn $ |
---|
6 | Revision 1.3 1997/04/15 09:56:36 schmidt |
---|
7 | #include <config.h> added |
---|
8 | the headers config.h and assert.h will be included |
---|
9 | be makeheader now |
---|
10 | |
---|
11 | Revision 1.2 1997/03/27 10:28:51 schmidt |
---|
12 | error check changed to ASSERT |
---|
13 | |
---|
14 | Revision 1.1 1996/07/16 12:24:56 stobbe |
---|
15 | "operators += and *= deleted sincethey are not needed in an general |
---|
16 | array class als well as the functions sum, prod, crossprod. |
---|
17 | " |
---|
18 | |
---|
19 | Revision 1.0 1996/05/17 11:06:32 stobbe |
---|
20 | Initial revision |
---|
21 | |
---|
22 | */ |
---|
23 | |
---|
24 | #include <factoryconf.h> |
---|
25 | |
---|
26 | #include <templates/array.h> |
---|
27 | |
---|
28 | template <class T> |
---|
29 | Array<T>::Array() : data(0), _min(0), _max(-1), _size(0) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | template <class T> |
---|
34 | Array<T>::Array( const Array<T> & a ) |
---|
35 | { |
---|
36 | if ( a._size > 0 ) { |
---|
37 | _min = a._min; |
---|
38 | _max = a._max; |
---|
39 | _size = a._size; |
---|
40 | data = new T[_size]; |
---|
41 | for ( int i = 0; i < _size; i++ ) |
---|
42 | data[i] = a.data[i]; |
---|
43 | } |
---|
44 | else { |
---|
45 | data = 0; |
---|
46 | _min = _size = 0; |
---|
47 | _max = -1; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | template <class T> |
---|
52 | Array<T>::Array( int i ) |
---|
53 | { |
---|
54 | _min = 0; |
---|
55 | _max = i-1; |
---|
56 | _size = i; |
---|
57 | if ( i == 0 ) |
---|
58 | data = 0; |
---|
59 | else |
---|
60 | data = new T[_size]; |
---|
61 | } |
---|
62 | |
---|
63 | template <class T> |
---|
64 | Array<T>::Array( int min, int max ) |
---|
65 | { |
---|
66 | if ( max < min ) { |
---|
67 | _min = _size = 0; |
---|
68 | _max = -1; |
---|
69 | data = 0; |
---|
70 | } |
---|
71 | else { |
---|
72 | _min = min; |
---|
73 | _max = max; |
---|
74 | _size = _max - _min + 1; |
---|
75 | data = new T[_size]; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | template <class T> |
---|
80 | Array<T>::~Array() |
---|
81 | { |
---|
82 | delete [] data; |
---|
83 | } |
---|
84 | |
---|
85 | template <class T> |
---|
86 | Array<T>& Array<T>::operator= ( const Array<T> & a ) |
---|
87 | { |
---|
88 | if ( this != &a ) { |
---|
89 | delete [] data; |
---|
90 | _min = a._min; |
---|
91 | _max = a._max; |
---|
92 | _size = a._size; |
---|
93 | if ( a._size > 0 ) { |
---|
94 | _size = a._size; |
---|
95 | data = new T[_size]; |
---|
96 | for ( int i = 0; i < _size; i++ ) |
---|
97 | data[i] = a.data[i]; |
---|
98 | } |
---|
99 | else { |
---|
100 | data = 0; |
---|
101 | _size = 0; |
---|
102 | } |
---|
103 | } |
---|
104 | return *this; |
---|
105 | } |
---|
106 | |
---|
107 | template <class T> |
---|
108 | T& Array<T>::operator[] ( int i ) const |
---|
109 | { |
---|
110 | ASSERT( i >= _min && i <= _max, "warning: array size mismatch." ); |
---|
111 | return data[i-_min]; |
---|
112 | } |
---|
113 | |
---|
114 | template <class T> |
---|
115 | int Array<T>::size() const |
---|
116 | { |
---|
117 | return _size; |
---|
118 | } |
---|
119 | |
---|
120 | template <class T> |
---|
121 | int Array<T>::min() const |
---|
122 | { |
---|
123 | return _min; |
---|
124 | } |
---|
125 | |
---|
126 | template <class T> |
---|
127 | int Array<T>::max() const |
---|
128 | { |
---|
129 | return _max; |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | template <class T> |
---|
134 | Array<T>& Array<T>::operator*= ( const T & t ) |
---|
135 | { |
---|
136 | for ( int i = 0; i < _size; i++ ) |
---|
137 | data[i] *= t; |
---|
138 | return *this; |
---|
139 | } |
---|
140 | |
---|
141 | template <class T> |
---|
142 | Array<T>& Array<T>::operator+= ( const T & t ) |
---|
143 | { |
---|
144 | for ( int i = 0; i < _size; i++ ) |
---|
145 | data[i] += t; |
---|
146 | return *this; |
---|
147 | } |
---|
148 | |
---|
149 | template <class T> |
---|
150 | Array<T>& Array<T>::operator+= ( const Array<T> & a ) |
---|
151 | { |
---|
152 | ASSERT ( _size == a._size, "warning: array size mismatch." ); |
---|
153 | for ( int i = 0; i < _size; i++ ) |
---|
154 | data[i] += a.data[i]; |
---|
155 | return *this; |
---|
156 | } |
---|
157 | */ |
---|
158 | |
---|
159 | #ifndef NOSTREAMIO |
---|
160 | template <class T> |
---|
161 | void Array<T>::print ( ostream & os ) const |
---|
162 | { |
---|
163 | if ( _size == 0 ) |
---|
164 | os << "( )"; |
---|
165 | else { |
---|
166 | os << "( " << data[0]; |
---|
167 | for ( int i = 1; i < _size; i++ ) |
---|
168 | os << ", " << data[i]; |
---|
169 | os << " )"; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | template <class T> |
---|
174 | ostream& operator<< ( ostream & os, const Array<T> & a ) |
---|
175 | { |
---|
176 | a.print( os ); |
---|
177 | return os; |
---|
178 | } |
---|
179 | #endif /* NOSTREAMIO */ |
---|