2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /** implements a generic array */
24 template < class T
> class Array
27 /** pointer to the data */
32 /** create empty array */
44 /** copy constructor */
45 Array (const Array
< T
> &a
)
52 for (unsigned i
= 0; i
< count
; i
++)
57 /** create array from old style array
58 * @param elem pointer to elements
59 * @param cnt element count
61 Array (const T elem
[], unsigned cnt
)
68 /** set content to them of a old style array
69 * @param elem pointer to elements
70 * @param cnt element count
72 void set (const T elem
[], unsigned cnt
)
76 for (i
= 0; i
< cnt
; i
++)
80 /** copy content of an array */
81 void set (const Array
& a
)
83 set (a
.array (), a ());
86 /** replace a part of the array and resize to fit
87 * @param elem pointer to new elements
88 * @param start start position
89 * @param cnt element count
91 void setpart (const T elem
[], unsigned start
, unsigned cnt
)
94 if (cnt
+ start
> count
)
96 for (i
= 0; i
< cnt
; i
++)
97 data
[i
+ start
] = elem
[i
];
100 /** replace a part of the array with the content of a and resize to fit
101 * @param start start index
102 * @param a new elements
104 void setpart (const Array
< T
> &a
, unsigned start
)
106 setpart (a
.array (), start
, a ());
110 * @param start start index
111 * @param cnt element count
113 void deletepart (unsigned start
, unsigned cnt
)
119 if (start
+ cnt
>= count
)
120 cnt
-= start
+ cnt
- count
;
121 for (unsigned i
= start
+ cnt
; i
< count
; i
++)
122 data
[i
- cnt
] = data
[i
];
123 resize (count
- cnt
);
126 /** assignement operator */
127 const Array
& operator = (const Array
& a
)
136 for (unsigned i
= 0; i
< count
; i
++)
142 /** compare array elementwise for equal */
143 bool operator== (const Array
& a
)
147 for (unsigned i
= 0; i
< count
; i
++)
153 /** compare array elementwise for not equal */
154 bool operator!= (const Array
& a
)
156 return !(*this == a
);
159 /** returns pointer to the elements */
160 const T
*array () const
165 /** returns pointer to the elements */
171 /** resize array to newcount elements */
172 void resize (unsigned newcount
)
182 T
*d1
= new T
[newcount
];
183 for (unsigned i
= 0; i
< (count
< newcount
? count
: newcount
); i
++)
191 /** insert elem at pos */
192 void insert (unsigned pos
, const T
& elem
)
200 T
*d1
= new T
[count
+ 1];
201 for (unsigned i
= 0; i
< pos
; i
++)
204 for (unsigned i
= pos
; i
< count
; i
++)
212 /** add element elem to the add */
213 void add (const T
& elem
)
216 operator[](count
- 1) = elem
;
218 /** access random element in standart C notation */
219 T
& operator[](unsigned elem
)
224 /** access random element in standart C notation */
225 const T
& operator[] (unsigned elem
) const
230 /** returns element count */
231 unsigned operator () () const
235 /** return element count */
236 unsigned len () const
243 for (int i
= 0; i
< count
; i
++)
244 for (int j
= i
+ 1; j
< count
; j
++)
245 if (data
[i
] > data
[j
])