1 | |
---|
2 | |
---|
3 | #ifndef _ALI_TARRAY_INC_ |
---|
4 | #define _ALI_TARRAY_INC_ |
---|
5 | |
---|
6 | // #include <malloc.h> |
---|
7 | |
---|
8 | #include "ali_misc.hxx" |
---|
9 | #include "ali_tlist.hxx" |
---|
10 | |
---|
11 | template<class T> |
---|
12 | class ALI_TARRAY { |
---|
13 | T **array; |
---|
14 | unsigned long size_of_array; |
---|
15 | |
---|
16 | public: |
---|
17 | ALI_TARRAY(unsigned long Size) { |
---|
18 | size_of_array = Size; |
---|
19 | array = (T **) calloc((unsigned int) Size, sizeof(T)); |
---|
20 | //array = (T (*) [1]) calloc((unsigned int) Size, sizeof(T)); |
---|
21 | if (array == 0) |
---|
22 | ali_fatal_error("Out of memory"); |
---|
23 | } |
---|
24 | ALI_TARRAY(ALI_TLIST<T>* list) { |
---|
25 | unsigned long l = 0; |
---|
26 | size_of_array = list->cardinality(); |
---|
27 | array = (T (*) []) calloc((unsigned int) size_of_array,sizeof(T)); |
---|
28 | if (array == 0) |
---|
29 | ali_fatal_error("Out of memory"); |
---|
30 | if (!list->is_empty()) { |
---|
31 | (*array)[l++] = list->first(); |
---|
32 | while (list->is_next() && l < size_of_array) |
---|
33 | (*array)[l++] = list->next(); |
---|
34 | if (list->is_next()) |
---|
35 | ali_fatal_error("List inconsitent","ALI_TARRAY::ALI_TARRAY()"); |
---|
36 | } |
---|
37 | } |
---|
38 | /* |
---|
39 | ALI_TARRAY(ALI_TARRAY<T> *ar) { |
---|
40 | unsigned long l = 0; |
---|
41 | size_of_array = ar->size_of_array; |
---|
42 | array = (T (*) []) calloc((unsigned int) size_of_array,sizeof(T)); |
---|
43 | if (array == 0) |
---|
44 | ali_fatal_error("Out of memory"); |
---|
45 | for (l = 0; l < size_of_array; l++) |
---|
46 | (*array)[l] = (*ar->array)[l]; |
---|
47 | } |
---|
48 | */ |
---|
49 | ~ALI_TARRAY(void) { |
---|
50 | if (array) |
---|
51 | free((char *) array); |
---|
52 | } |
---|
53 | unsigned long size(void) { |
---|
54 | return size_of_array; |
---|
55 | } |
---|
56 | void set(unsigned long position, T value) { |
---|
57 | if (position >= size_of_array) |
---|
58 | ali_fatal_error("Access out of array","ALI_TARRAY::set()"); |
---|
59 | (*array)[position] = value; |
---|
60 | } |
---|
61 | T get(unsigned long position) { |
---|
62 | if (position >= size_of_array) |
---|
63 | ali_fatal_error("Access out of array","ALI_TARRAY::get()"); |
---|
64 | return (*array)[position]; |
---|
65 | } |
---|
66 | }; |
---|
67 | |
---|
68 | #endif |
---|
69 | |
---|