source: branches/profile/NALIGNER/ali_tarray.hxx

Last change on this file was 8894, checked in by westram, 12 years ago
  • fixed cppcheck warnings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ali_tarray.hxx                                    //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#ifndef ALI_TARRAY_HXX
12#define ALI_TARRAY_HXX
13
14#ifndef ALI_TLIST_HXX
15#include "ali_tlist.hxx"
16#endif
17
18template<class T>
19class ALI_TARRAY : virtual Noncopyable {
20    T **array;
21    unsigned long size_of_array;
22
23public:
24    ALI_TARRAY(unsigned long Size) {
25        size_of_array = Size;
26        array = (T **) calloc((unsigned int) Size, sizeof(T));
27        if (array == 0)
28            ali_fatal_error("Out of memory");
29    }
30    ALI_TARRAY(ALI_TLIST<T>* list) {
31        size_of_array = list->cardinality();
32        array = (T (*) []) calloc((unsigned int) size_of_array, sizeof(T));
33        if (array == 0)
34            ali_fatal_error("Out of memory");
35        if (!list->is_empty()) {
36            unsigned long l = 0;
37            (*array)[l++] = list->first();
38            while (list->is_next() && l < size_of_array)
39                (*array)[l++] = list->next();
40            if (list->is_next())
41                ali_fatal_error("List inconsitent", "ALI_TARRAY::ALI_TARRAY()");
42        }
43    }
44    ~ALI_TARRAY() {
45        if (array)
46            free((char *) array);
47    }
48    unsigned long size() {
49        return size_of_array;
50    }
51    void set(unsigned long position, T value) {
52        if (position >= size_of_array)
53            ali_fatal_error("Access out of array", "ALI_TARRAY::set()");
54        (*array)[position] = value;
55    }
56    T get(unsigned long position) {
57        if (position >= size_of_array)
58            ali_fatal_error("Access out of array", "ALI_TARRAY::get()");
59        return (*array)[position];
60    }
61};
62
63#else
64#error ali_tarray.hxx included twice
65#endif // ALI_TARRAY_HXX
Note: See TracBrowser for help on using the repository browser.