Rev 7930 | Rev 7973 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7930 | Rev 7972 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #ifndef INCLUDE_COLLECTION_H |
1 | #ifndef INCLUDE_COLLECTION_H |
2 | #define INCLUDE_COLLECTION_H |
2 | #define INCLUDE_COLLECTION_H |
3 | #print "[include |
3 | #print "[include |
Line -... | Line 4... | ||
- | 4 | ||
- | 5 | #include "array.h" |
|
- | 6 | ||
- | 7 | /*======================================================== |
|
- | 8 | = = |
|
- | 9 | = Integer = |
|
- | 10 | = = |
|
- | 11 | ========================================================*/ |
|
- | 12 | ||
- | 13 | struct collection_int |
|
- | 14 | { |
|
- | 15 | dword buf; |
|
- | 16 | dword buf_size; |
|
- | 17 | unsigned count; |
|
- | 18 | void alloc(); |
|
- | 19 | void add(); |
|
- | 20 | dword get(); |
|
- | 21 | void set(); |
|
- | 22 | void swap(); |
|
- | 23 | dword len(); |
|
- | 24 | dword get_last(); |
|
- | 25 | void pop(); |
|
- | 26 | void drop(); |
|
- | 27 | }; |
|
- | 28 | ||
- | 29 | :void collection_int::alloc() { |
|
- | 30 | if (!buf) { |
|
- | 31 | buf_size = 4096; |
|
- | 32 | buf = malloc(4096); |
|
- | 33 | } else { |
|
- | 34 | buf_size += 4096; |
|
- | 35 | buf = realloc(buf, buf_size); |
|
- | 36 | } |
|
- | 37 | } |
|
- | 38 | ||
- | 39 | :void collection_int::add(dword _in) { |
|
- | 40 | if (!buf) || (count * sizeof(dword) >= buf_size) alloc(); |
|
- | 41 | EAX = count * sizeof(dword) + buf; |
|
- | 42 | ESDWORD[EAX] = _in; |
|
- | 43 | count++; |
|
- | 44 | } |
|
- | 45 | ||
- | 46 | :dword collection_int::get(dword pos) { |
|
- | 47 | if (pos<0) || (pos>=count) return 0; |
|
- | 48 | return ESDWORD[pos * sizeof(dword) + buf]; |
|
- | 49 | } |
|
- | 50 | ||
- | 51 | ||
- | 52 | :void collection_int::set(dword pos, _in) { |
|
- | 53 | while (pos >= count) add(0); |
|
- | 54 | EAX = pos * sizeof(dword) + buf; |
|
- | 55 | ESDWORD[EAX] = _in; |
|
- | 56 | } |
|
- | 57 | ||
- | 58 | :void collection_int::swap(dword pos1, pos2) { |
|
- | 59 | while (pos1 >= count) add(0); |
|
- | 60 | while (pos2 >= count) add(0); |
|
- | 61 | EAX = pos1 * sizeof(dword) + buf; |
|
- | 62 | EBX = pos2 * sizeof(dword) + buf; |
|
- | 63 | ESDWORD[EAX] >< ESDWORD[EBX]; |
|
- | 64 | } |
|
- | 65 | ||
- | 66 | :dword collection_int::len(dword pos) { |
|
- | 67 | if (pos<0) || (pos+1>=count) return 0; |
|
- | 68 | return get(pos+1) - get(pos); |
|
- | 69 | } |
|
- | 70 | ||
- | 71 | :dword collection_int::get_last() { |
|
- | 72 | return get(count-1); |
|
- | 73 | } |
|
- | 74 | ||
- | 75 | :void collection_int::pop() { |
|
- | 76 | if (count>0) count--; |
|
- | 77 | } |
|
- | 78 | ||
- | 79 | :void collection_int::drop() { |
|
- | 80 | count = 0; |
|
- | 81 | } |
|
4 | 82 | ||
5 | /*======================================================== |
83 | /*======================================================== |
6 | = = |
84 | = = |
7 | = String = |
85 | = String = |
8 | = = |
86 | = = |
Line 11... | Line 89... | ||
11 | struct collection |
89 | struct collection |
12 | { |
90 | { |
13 | int realloc_size, count; |
91 | int realloc_size, count; |
14 | dword data_start; |
92 | dword data_start; |
15 | dword data_size; |
93 | dword data_size; |
16 | dword element_offset[4000]; |
94 | collection_int offset; |
17 | int add(); |
95 | int add(); |
18 | int addn(); |
96 | int addn(); |
19 | dword get(); //get_name_by_pos |
97 | dword get(); //get_name_by_pos |
20 | dword get_pos_by_name(); |
98 | dword get_pos_by_name(); |
21 | void drop(); |
99 | void drop(); |
Line 40... | Line 118... | ||
40 | :int collection::add(dword in) { |
118 | :int collection::add(dword in) { |
41 | return addn(in, strlen(in)); |
119 | return addn(in, strlen(in)); |
42 | } |
120 | } |
Line 43... | Line 121... | ||
43 | 121 | ||
44 | :int collection::addn(dword in, len) { |
- | |
45 | if (count >= 4000) { |
- | |
46 | debugln("collection: more than 4000 elements!"); |
- | |
47 | return 0; |
- | |
48 | } |
122 | :int collection::addn(dword in, len) { |
49 | if (element_offset[count]+len+2 > data_size) { |
123 | if (offset.get(count)+len+2 > data_size) { |
50 | increase_data_size(); |
124 | increase_data_size(); |
51 | addn(in, len); |
125 | addn(in, len); |
52 | return 1; |
126 | return 1; |
53 | } |
127 | } |
54 | strncpy(data_start+element_offset[count], in, len); |
128 | strncpy(data_start+offset.get(count), in, len); |
55 | count++; |
129 | count++; |
56 | element_offset[count] = element_offset[count-1] + len + 1; |
130 | offset.set(count, offset.get(count-1) + len + 1); |
57 | return 1; |
131 | return 1; |
Line 58... | Line 132... | ||
58 | } |
132 | } |
59 | 133 | ||
60 | :dword collection::get(dword pos) { |
134 | :dword collection::get(dword pos) { |
61 | if (pos<0) || (pos>=count) return 0; |
135 | if (pos<0) || (pos>=count) return 0; |
Line 62... | Line 136... | ||
62 | return data_start + element_offset[pos]; |
136 | return data_start + offset.get(pos); |
63 | } |
137 | } |
64 | 138 | ||
Line 65... | Line 139... | ||
65 | :dword collection::get_last() { |
139 | :dword collection::get_last() { |
66 | return get(count-1); |
140 | return get(count-1); |
67 | } |
141 | } |
68 | 142 | ||
69 | :dword collection::get_pos_by_name(dword name) { |
143 | :dword collection::get_pos_by_name(dword name) { |
70 | dword i; |
144 | dword i; |
71 | for (i=0; i |
145 | for (i=0; i |
Line 72... | Line 146... | ||
72 | if (strcmp(data_start + element_offset[i], name)==0) return i; |
146 | if (strcmp(data_start + offset.get(i), name)==0) return i; |
73 | } |
147 | } |
74 | return -1; |
148 | return -1; |
75 | } |
149 | } |
76 | 150 | ||
77 | :void collection::drop() { |
151 | :void collection::drop() { |
78 | if (data_start) free(data_start); |
152 | if (data_start) free(data_start); |
Line 79... | Line 153... | ||
79 | data_size = 0; |
153 | data_size = 0; |
80 | data_start = 0; |
154 | data_start = 0; |
81 | element_offset[count] = 0; |
155 | offset.drop(); |
Line 82... | Line -... | ||
82 | count = 0; |
- | |
83 | } |
- | |
84 | - | ||
85 | :bool collection::pop() { |
- | |
86 | if (count>0) count--; |
- | |
87 | } |
- | |
88 | - | ||
89 | /*======================================================== |
- | |
90 | = = |
- | |
91 | = Integer = |
- | |
92 | = = |
- | |
93 | ========================================================*/ |
- | |
94 | - | ||
95 | struct collection_int |
- | |
96 | { |
- | |
97 | dword buf; |
- | |
98 | dword buf_size; |
- | |
99 | unsigned count; |
- | |
100 | void alloc(); |
- | |
101 | void add(); |
- | |
102 | dword get(); |
- | |
103 | dword len(); |
- | |
104 | dword get_last(); |
- | |
105 | void pop(); |
- | |
106 | void drop(); |
- | |
107 | }; |
- | |
108 | - | ||
109 | :void collection_int::alloc() { |
- | |
110 | if (!buf) { |
- | |
111 | buf_size = 4096; |
- | |
112 | buf = malloc(4096); |
- | |
113 | } else { |
- | |
114 | buf_size += 4096; |
- | |
115 | buf = realloc(buf, buf_size); |
- | |
116 | } |
- | |
117 | } |
- | |
118 | - | ||
119 | :void collection_int::add(dword _in) { |
- | |
120 | if (!buf) || (count * sizeof(dword) >= buf_size) alloc(); |
- | |
121 | EAX = count * sizeof(dword) + buf; |
- | |
122 | ESDWORD[EAX] = _in; |
- | |
123 | count++; |
- | |
124 | } |
- | |
125 | - | ||
126 | :dword collection_int::get(dword pos) { |
- | |
127 | if (pos<0) || (pos>=count) return 0; |
- | |
128 | return ESDWORD[pos * sizeof(dword) + buf]; |
- | |
129 | } |
- | |
130 | - | ||
131 | :dword collection_int::len(dword pos) { |
- | |
132 | if (pos<0) || (pos+1>=count) return 0; |
- | |
133 | return get(pos+1) - get(pos); |
- | |
134 | } |
- | |
135 | - | ||
136 | :dword collection_int::get_last() { |
- | |
137 | return get(count-1); |
- | |
138 | } |
- | |
139 | - | ||
140 | :void collection_int::pop() { |
- | |
141 | if (count>0) count--; |
156 | count = 0; |
142 | } |
157 | } |