LWQ
2025-08-07 c049c2fdf2f5722e460d0dc5c7597bf819f2e2e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <cstdlib>
 
namespace Euresys {
namespace MultiCam {
namespace Internal {
 
template < class CItem, int localsize = 512 >
class AssociativeArray : public Container < CItem >
{
        AssociativeArray<CItem, localsize> *next;
        CItem items[localsize];
        int keys[localsize];
        int used;
    public:
        /** Construct an associative array.
        **/
        AssociativeArray() : next(0), used(0) {
            int i;
            for(i = 0; i < localsize; i++) {
                items[i] = NULL;
                keys[i] = -1;
            }
        }
 
        ~AssociativeArray() {
            if (next)
                delete next;
        }
 
        void Add(int key, CItem item) {
            int i;
            i = Find(key);
            if (isValidIndex(i) || (!isValidIndex(i) && used < localsize)) {
                if (!isValidIndex(i)) {
                    i = used;
                    used++;
                }
                items[i] = item;
                keys[i] = key;
            } else {
                if (!next) {
                    next = new AssociativeArray<CItem, localsize>();
                }
                next->Add(key, item);
            }
        }
 
        // for compatibility with the container interface
        virtual int Add(CItem &) {
            // NOT IMPLEMENTED !!
            return 0;
        }
 
        // for compatibility with the container interface
        virtual void Assign(int idx, CItem item) {
            Add(idx, item);
        }
 
        virtual int GetCount() const {
            int size;
 
            size = used;
            if (next) size  += next->GetCount();
            return size;
        }
 
        int Find(int key) const {
            int i;
            for (i = 0; i < used && i < localsize; i++) {
                if (keys[i] == key)
                    return i;
            }
            return -1;
        }
 
        bool isValidIndex(int i) const {
            return i >= 0 && i < used && i < localsize;
        }
 
        void Remove(int key) {
            int i = Find(key);
            if (isValidIndex(i)) {
                items[i] = NULL;            
            }
        }
 
        CItem Get(int key) const {
            int i;
            i = Find(key);
            if (isValidIndex(i))
                return items[i];
            else if (next) 
                return next->Get(key);
            
            return NULL;
        }
 
        // for compatibility with the container interface
        CItem At(int key) const {
            return Get(key);
        }
 
        void DeleteAll() {
            int i;
            for (i=0; i < used; i++) {
                delete items[i];
                items[i] = NULL;
            }
            if (next) {
                next->DeleteAll();
                delete next;
                next = NULL;
            }
        }
};
 
      
      
      // ********************************************************************************************
      // List: simple list class
      // -----------------------
      
      template <class T, int N> class List : public Container<T>
      {
        protected:
            int count, allocCount;
            T *list;
            
        public:
            List();
            virtual ~List();
            
            T operator[](int Index) const { return At(Index); }
            T At(int Index) const { return list[Index]; }
            int GetCount() const { return count; }
            int Add(T &item);
      };
      
      template <class T, int N>
          List<T, N>::List()
      {
          count = 0;
          allocCount = 0;
          list = NULL;
      }
      
      template <class T, int N>
          List<T, N>::~List()
      {
          if (list != NULL)
              free(list);
      }
      
      template <class T, int N>
          int List<T, N>::Add(T &item)
      {
          int element = count++;
          
          if (count >= allocCount)
          {
              allocCount += N;
              list = (T *)realloc(list, sizeof(T) * allocCount);
          }
          
          list[element] = item;
          
          return element;
      }
    
    
}
}
}