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