ARB
ali_tarray.hxx
Go to the documentation of this file.
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 
18 template<class T>
19 class ALI_TARRAY : virtual Noncopyable {
20  T **array;
21  unsigned long size_of_array;
22 
23 public:
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  }
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  }
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
void ali_out_of_memory_if(bool cond)
Definition: ali_misc.hxx:52
ALI_TARRAY(unsigned long Size)
Definition: ali_tarray.hxx:24
int has_next()
Definition: ali_tlist.hxx:168
int cardinality()
Definition: ali_tlist.hxx:162
void set(unsigned long position, T value)
Definition: ali_tarray.hxx:49
ALI_TARRAY(ALI_TLIST< T > *list)
Definition: ali_tarray.hxx:29
int is_empty()
Definition: ali_tlist.hxx:165
void ali_fatal_error(const char *message, const char *func)
Definition: ali_main.cxx:85
Definition: trnsprob.h:20
unsigned long size()
Definition: ali_tarray.hxx:46