spielwiese
Release-4-3-2p1
Last change
on this file was
fb1675,
checked in by Hans Schoenemann <hannes@…>, 6 years ago
|
use include ".." for singular related .h, p8
|
-
Property mode set to
100644
|
File size:
2.7 KB
|
Line | |
---|
1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | #include "factory/templates/ftmpl_array.h" |
---|
4 | |
---|
5 | template <class T> |
---|
6 | Array<T>::Array() : data(0), _min(0), _max(-1), _size(0) |
---|
7 | { |
---|
8 | } |
---|
9 | |
---|
10 | template <class T> |
---|
11 | Array<T>::Array( const Array<T> & a ) |
---|
12 | { |
---|
13 | if ( a._size > 0 ) { |
---|
14 | _min = a._min; |
---|
15 | _max = a._max; |
---|
16 | _size = a._size; |
---|
17 | data = new T[_size]; |
---|
18 | for ( int i = 0; i < _size; i++ ) |
---|
19 | data[i] = a.data[i]; |
---|
20 | } |
---|
21 | else { |
---|
22 | data = 0; |
---|
23 | _min = _size = 0; |
---|
24 | _max = -1; |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | template <class T> |
---|
29 | Array<T>::Array( int i ) |
---|
30 | { |
---|
31 | _min = 0; |
---|
32 | _max = i-1; |
---|
33 | _size = i; |
---|
34 | if ( i == 0 ) |
---|
35 | data = 0; |
---|
36 | else |
---|
37 | data = new T[_size]; |
---|
38 | } |
---|
39 | |
---|
40 | template <class T> |
---|
41 | Array<T>::Array( int min, int max ) |
---|
42 | { |
---|
43 | if ( max < min ) { |
---|
44 | _min = _size = 0; |
---|
45 | _max = -1; |
---|
46 | data = 0; |
---|
47 | } |
---|
48 | else { |
---|
49 | _min = min; |
---|
50 | _max = max; |
---|
51 | _size = _max - _min + 1; |
---|
52 | data = new T[_size]; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | template <class T> |
---|
57 | Array<T>::~Array() |
---|
58 | { |
---|
59 | delete [] data; |
---|
60 | } |
---|
61 | |
---|
62 | template <class T> |
---|
63 | Array<T>& Array<T>::operator= ( const Array<T> & a ) |
---|
64 | { |
---|
65 | if ( this != &a ) { |
---|
66 | delete [] data; |
---|
67 | _min = a._min; |
---|
68 | _max = a._max; |
---|
69 | _size = a._size; |
---|
70 | if ( a._size > 0 ) { |
---|
71 | _size = a._size; |
---|
72 | data = new T[_size]; |
---|
73 | for ( int i = 0; i < _size; i++ ) |
---|
74 | data[i] = a.data[i]; |
---|
75 | } |
---|
76 | else { |
---|
77 | data = 0; |
---|
78 | _size = 0; |
---|
79 | } |
---|
80 | } |
---|
81 | return *this; |
---|
82 | } |
---|
83 | |
---|
84 | template <class T> |
---|
85 | T& Array<T>::operator[] ( int i ) const |
---|
86 | { |
---|
87 | ASSERT( i >= _min && i <= _max, "warning: array size mismatch." ); |
---|
88 | return data[i-_min]; |
---|
89 | } |
---|
90 | |
---|
91 | template <class T> |
---|
92 | int Array<T>::size() const |
---|
93 | { |
---|
94 | return _size; |
---|
95 | } |
---|
96 | |
---|
97 | template <class T> |
---|
98 | int Array<T>::min() const |
---|
99 | { |
---|
100 | return _min; |
---|
101 | } |
---|
102 | |
---|
103 | template <class T> |
---|
104 | int Array<T>::max() const |
---|
105 | { |
---|
106 | return _max; |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | template <class T> |
---|
111 | Array<T>& Array<T>::operator*= ( const T & t ) |
---|
112 | { |
---|
113 | for ( int i = 0; i < _size; i++ ) |
---|
114 | data[i] *= t; |
---|
115 | return *this; |
---|
116 | } |
---|
117 | |
---|
118 | template <class T> |
---|
119 | Array<T>& Array<T>::operator+= ( const T & t ) |
---|
120 | { |
---|
121 | for ( int i = 0; i < _size; i++ ) |
---|
122 | data[i] += t; |
---|
123 | return *this; |
---|
124 | } |
---|
125 | |
---|
126 | template <class T> |
---|
127 | Array<T>& Array<T>::operator+= ( const Array<T> & a ) |
---|
128 | { |
---|
129 | ASSERT ( _size == a._size, "warning: array size mismatch." ); |
---|
130 | for ( int i = 0; i < _size; i++ ) |
---|
131 | data[i] += a.data[i]; |
---|
132 | return *this; |
---|
133 | } |
---|
134 | */ |
---|
135 | |
---|
136 | #ifndef NOSTREAMIO |
---|
137 | template <class T> |
---|
138 | void Array<T>::print ( OSTREAM & os ) const |
---|
139 | { |
---|
140 | if ( _size == 0 ) |
---|
141 | os << "( )"; |
---|
142 | else { |
---|
143 | os << "( " << data[0]; |
---|
144 | for ( int i = 1; i < _size; i++ ) |
---|
145 | os << ", " << data[i]; |
---|
146 | os << " )"; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | template <class T> |
---|
151 | OSTREAM& operator<< ( OSTREAM & os, const Array<T> & a ) |
---|
152 | { |
---|
153 | a.print( os ); |
---|
154 | return os; |
---|
155 | } |
---|
156 | #endif /* NOSTREAMIO */ |
---|
Note: See
TracBrowser
for help on using the repository browser.