Debian Codegraph @

cuneiform_src/Kern/lns32/src/fararray.h

raw
1 /*
2 Copyright (c) 1993-2008, Cognitive Technologies
3 All rights reserved.
4
5 Разрешается повторное распространение и использование как в виде исходного кода,
6 так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:
7
8 * При повторном распространении исходного кода должны оставаться указанное
9 выше уведомление об авторском праве, этот список условий и последующий
10 отказ от гарантий.
11 * При повторном распространении двоичного кода в документации и/или в
12 других материалах, поставляемых при распространении, должны сохраняться
13 указанная выше информация об авторском праве, этот список условий и
14 последующий отказ от гарантий.
15 * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
16 быть использованы в качестве средства поддержки и/или продвижения
17 продуктов, основанных на этом ПО, без предварительного письменного
18 разрешения.
19
20 ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
21 ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
22 ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
23 ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
24 МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
25 НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
26 ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
27 НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
28 СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
29 ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
30 НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
31 ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.
32
33 Redistribution and use in source and binary forms, with or without modification,
34 are permitted provided that the following conditions are met:
35
36 * Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright notice,
39 this list of conditions and the following disclaimer in the documentation
40 and/or other materials provided with the distribution.
41 * Neither the name of the Cognitive Technologies nor the names of its
42 contributors may be used to endorse or promote products derived from this
43 software without specific prior written permission.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
46 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
49 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 #ifndef __FARARRAY_H
58 #define __FARARRAY_H
59
60 #ifndef __LNSLANG_H
61 # include "lnslang.h"
62 #endif
63 template<class Type> class TArray //: public TFarHeap
64 {
65 protected:
66 Type * data;
67 long int last; // == len - 1
68 public:
69 Type & operator[](int32_t i) {
70 assert( i <= last );
71 return (*(data + i));
72 }
73
74 Type * ptr(int32_t num) {
75 assert( num <= last );
76 return (data + num);
77 }
78
79 TArray(void) {
80 data = NULL;
81 }
82
83 TArray(int32_t of_count) {
84 create(of_count);
85 }
86
87 ~TArray(void) {
88 flash();
89 }
90
91 Type * lastPtr(void) {
92 assert(!isEmpty());
93 return (data + last);
94 }
95
96 Type * firstPtr(void) {
97 //?assert(!isEmpty());
98 return (data);
99 }
100
101 Bool create(int32_t of_count) {
102 if (of_count != 0)
103 data = (Type *) malloc(of_count * sizeof(Type));
104 else
105 data = (Type *) malloc(sizeof(Type));
106 if (data != NULL)
107 last = (of_count - 1);
108 return (data != NULL);
109 }
110
111 Bool Grow(int32_t of_count = -1) {
112 if (of_count == -1) {
113 of_count = (last + 1) * 2;
114 }
115
116 if (of_count <= 0)
117 of_count = 1;
118
119 Type * new_data = (Type *) (malloc((int32_t) (of_count
120 * (int32_t) sizeof(Type))));
121 if (new_data != NULL) {
122 if (data != NULL) {
123 memcpy(new_data, data, (last + 1) * sizeof(Type));
124 free(data);
125 data = new_data;
126 new_data = NULL;
127 last = (of_count - 1);
128 }
129 }
130 return (data != NULL);
131 }
132
133 void flash(void) {
134 if (data != NULL)
135 free((void*) data);
136 data = NULL;
137 }
138
139 void flush(void) {
140 flash();
141 }
142
143 int volume(void) {
144 assert(data!=NULL);
145 return (last + 1);
146 }
147
148 int lastHandle(void) {
149 assert(data!=NULL);
150 return (last);
151 }
152
153 Bool isEmpty(void) {
154 return (data == NULL);
155 }
156
157 Bool isOk(void) {
158 return (data != NULL);
159 }
160
161 void fill(Type * value, int32_t from = 0);
162
163 Bool shrink(int32_t el_count) {
164 return TRUE;
165 /*
166 assert(el_count < volume() );
167 if (el_count > 0)
168 { void* new_data = data; TFarHeap::shrinkMemBlock( data, sizeof(Type)*(int32_t)el_count );
169 if (new_data == NULL)
170 return WRONG(); // can't shrink ??
171 else {
172 data = (Type *)new_data;
173 last = el_count -1;
174 };
175 return TRUE;
176 }
177 else
178 { void* new_data = TFarHeap::shrinkMemBlock( data, (int32_t)sizeof(Type) );
179 if (new_data == NULL)
180 return WRONG(); // can't shrink ??
181 else {
182 data = (Type *)new_data;
183 last = -1;
184 };
185 return TRUE;
186 };
187 */
188 }
189
190 void copy(TArray<Type> * arr) {
191 assert( arr != NULL );
192 assert( arr->isOk() );
193 assert( isOk() );
194 assert( arr->volume() <= volume() );
195 MEMMOVE(data, arr->data, (arr->volume()) * (int32_t) (sizeof(Type)));
196 }
197
198 void copy(TArray<Type> * arr, int32_t count) {
199 assert( arr != NULL );
200 assert( arr->isOk() );
201 assert( isOk() );
202 assert( count <= arr->volume() );
203 assert( count <= volume() );
204 MEMMOVE(data, arr->data, count * (int32_t) (sizeof(Type)));
205 }
206 };
207
208 template<class Type>
209 void TArray<Type>::fill(Type * value, int32_t from) {
210 if(from == 0 && last < 0) // Cleaning function calls this even for a zero-sized array.
211 data[0] = *value;
212 else {
213 assert(from <= last);
214 for (from = from; from <= last; from++)
215 data[from] = (*value);
216 }
217 }
218
219 #endif // __FARARRAY_H