source: tags/ms_r18q1/NALIGNER/ali_tarray.hxx

Last change on this file was 16766, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 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        ali_out_of_memory_if(!array);
28    }
29    ALI_TARRAY(ALI_TLIST<T>* list) {
30        size_of_array = list->cardinality();
31        array = (T (*) []) calloc((unsigned int) size_of_array, sizeof(T));
32        ali_out_of_memory_if(!array);
33        if (!list->is_empty()) {
34            unsigned long l = 0;
35            (*array)[l++] = list->first();
36            while (list->has_next() && l < size_of_array)
37                (*array)[l++] = list->next();
38            if (list->has_next())
39                ali_fatal_error("List inconsitent", "ALI_TARRAY::ALI_TARRAY()");
40        }
41    }
42    ~ALI_TARRAY() {
43        if (array)
44            free((char *) array);
45    }
46    unsigned long size() {
47        return size_of_array;
48    }
49    void set(unsigned long position, T value) {
50        if (position >= size_of_array)
51            ali_fatal_error("Access out of array", "ALI_TARRAY::set()");
52        (*array)[position] = value;
53    }
54    T get(unsigned long position) {
55        if (position >= size_of_array)
56            ali_fatal_error("Access out of array", "ALI_TARRAY::get()");
57        return (*array)[position];
58    }
59};
60
61#else
62#error ali_tarray.hxx included twice
63#endif // ALI_TARRAY_HXX
Note: See TracBrowser for help on using the repository browser.