Imported Upstream version 0.12.7
[bowtie.git] / auto_array.h
1 /*
2  * auto_array.h
3  *
4  *  Created on: Oct 12, 2009
5  *      Author: Ben Langmead
6  */
7
8 #include <cstring>
9
10 #ifndef AUTO_ARRAY_H_
11 #define AUTO_ARRAY_H_
12
13 /**
14  * A simple fixed-length array of type T, automatically freed in the
15  * destructor.
16  */
17 template<typename T>
18 class AutoArray {
19 public:
20         AutoArray(size_t sz) {
21                 t_ = NULL;
22                 t_ = new T[sz];
23                 memset(t_, 0, sz*sizeof(T));
24                 sz_ = sz;
25         }
26         ~AutoArray() { if(t_ != NULL) delete[] t_; }
27         T& operator[](size_t sz) {
28                 return t_[sz];
29         }
30         const T& operator[](size_t sz) const {
31                 return t_[sz];
32         }
33 private:
34         T *t_;
35         size_t sz_;
36 };
37
38 #endif /* AUTO_ARRAY_H_ */