Rev 7771 | Rev 7885 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7771 | Rev 7878 | ||
---|---|---|---|
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 | 4 | ||
5 | /*======================================================== |
5 | /*======================================================== |
6 | = = |
6 | = = |
7 | = String = |
7 | = String = |
8 | = = |
8 | = = |
9 | ========================================================*/ |
9 | ========================================================*/ |
10 | 10 | ||
11 | struct collection |
11 | struct collection |
12 | { |
12 | { |
13 | int realloc_size, count; |
13 | int realloc_size, count; |
14 | dword data_start; |
14 | dword data_start; |
15 | dword data_size; |
15 | dword data_size; |
16 | dword element_offset[4000]; |
16 | dword element_offset[4000]; |
17 | int add(); |
17 | int add(); |
18 | int addn(); |
18 | int addn(); |
19 | dword get(); //get_name_by_pos |
19 | dword get(); //get_name_by_pos |
20 | dword get_pos_by_name(); |
20 | dword get_pos_by_name(); |
21 | void drop(); |
21 | void drop(); |
22 | void increase_data_size(); |
22 | void increase_data_size(); |
- | 23 | dword get_last(); |
|
- | 24 | bool delete_last(); |
|
23 | }; |
25 | }; |
24 | 26 | ||
25 | :void collection::increase_data_size() { |
27 | :void collection::increase_data_size() { |
26 | int filled_size; |
28 | int filled_size; |
27 | if (realloc_size<4096) realloc_size = 4096; |
29 | if (realloc_size<4096) realloc_size = 4096; |
28 | if (!data_size) { |
30 | if (!data_size) { |
29 | data_size = realloc_size; |
31 | data_size = realloc_size; |
30 | data_start = malloc(realloc_size); |
32 | data_start = malloc(realloc_size); |
31 | } |
33 | } |
32 | else { |
34 | else { |
33 | data_size = data_size + realloc_size; |
35 | data_size = data_size + realloc_size; |
34 | data_start = realloc(data_start, data_size); |
36 | data_start = realloc(data_start, data_size); |
35 | } |
37 | } |
36 | } |
38 | } |
37 | 39 | ||
38 | :int collection::add(dword in) { |
40 | :int collection::add(dword in) { |
39 | return addn(in, strlen(in)); |
41 | return addn(in, strlen(in)); |
40 | } |
42 | } |
41 | 43 | ||
42 | :int collection::addn(dword in, len) { |
44 | :int collection::addn(dword in, len) { |
43 | if (count >= 4000) return 0; |
45 | if (count >= 4000) { |
- | 46 | debugln("collection: more than 4000 elements!"); |
|
- | 47 | return 0; |
|
- | 48 | } |
|
44 | if (element_offset[count]+len+2 > data_size) { |
49 | if (element_offset[count]+len+2 > data_size) { |
45 | increase_data_size(); |
50 | increase_data_size(); |
46 | addn(in, len); |
51 | addn(in, len); |
47 | return 1; |
52 | return 1; |
48 | } |
53 | } |
49 | strncpy(data_start+element_offset[count], in, len); |
54 | strncpy(data_start+element_offset[count], in, len); |
50 | count++; |
55 | count++; |
51 | element_offset[count] = element_offset[count-1] + len + 1; |
56 | element_offset[count] = element_offset[count-1] + len + 1; |
52 | return 1; |
57 | return 1; |
53 | } |
58 | } |
54 | 59 | ||
55 | :dword collection::get(dword pos) { |
60 | :dword collection::get(dword pos) { |
56 | if (pos<0) || (pos>=count) return 0; |
61 | if (pos<0) || (pos>=count) return 0; |
57 | return data_start + element_offset[pos]; |
62 | return data_start + element_offset[pos]; |
58 | } |
63 | } |
- | 64 | ||
- | 65 | :dword collection::get_last() { |
|
- | 66 | return get(count-1); |
|
- | 67 | } |
|
59 | 68 | ||
60 | :dword collection::get_pos_by_name(dword name) { |
69 | :dword collection::get_pos_by_name(dword name) { |
61 | dword i; |
70 | dword i; |
62 | for (i=0; i |
71 | for (i=0; i |
63 | if (strcmp(data_start + element_offset[i], name)==0) return i; |
72 | if (strcmp(data_start + element_offset[i], name)==0) return i; |
64 | } |
73 | } |
65 | return -1; |
74 | return -1; |
66 | } |
75 | } |
67 | 76 | ||
68 | :void collection::drop() { |
77 | :void collection::drop() { |
69 | if (data_start) free(data_start); |
78 | if (data_start) free(data_start); |
70 | data_size = 0; |
79 | data_size = 0; |
71 | data_start = 0; |
80 | data_start = 0; |
72 | element_offset[count] = 0; |
81 | element_offset[count] = 0; |
73 | count = 0; |
82 | count = 0; |
74 | } |
83 | } |
- | 84 | ||
- | 85 | :bool collection::delete_last() { |
|
- | 86 | count--; |
|
75 | 87 | } |
|
76 | 88 | ||
77 | /*======================================================== |
89 | /*======================================================== |
78 | = = |
90 | = = |
79 | = Integer = |
91 | = Integer = |
80 | = = |
92 | = = |
81 | ========================================================*/ |
93 | ========================================================*/ |
82 | 94 | ||
83 | struct collection_int |
95 | struct collection_int |
84 | { |
96 | { |
85 | int count; |
97 | int count; |
86 | dword element[4096*3]; |
98 | dword element[4096*3]; |
87 | int add(); |
99 | int add(); |
88 | dword get(); |
100 | dword get(); |
89 | dword get_last(); |
101 | dword get_last(); |
90 | void pop(); |
102 | void pop(); |
91 | void drop(); |
103 | void drop(); |
92 | }; |
104 | }; |
93 | 105 | ||
94 | :int collection_int::add(dword in) { |
106 | :int collection_int::add(dword in) { |
95 | if (count >= 4096*3) return 0; |
107 | if (count >= 4096*3) return 0; |
96 | element[count] = in; |
108 | element[count] = in; |
97 | count++; |
109 | count++; |
98 | return 1; |
110 | return 1; |
99 | } |
111 | } |
100 | 112 | ||
101 | :dword collection_int::get(dword pos) { |
113 | :dword collection_int::get(dword pos) { |
102 | if (pos<0) || (pos>=count) return 0; |
114 | if (pos<0) || (pos>=count) return 0; |
103 | return element[pos]; |
115 | return element[pos]; |
104 | } |
116 | } |
105 | 117 | ||
106 | :dword collection_int::get_last() { |
118 | :dword collection_int::get_last() { |
107 | return element[count]; |
119 | return element[count]; |
108 | } |
120 | } |
109 | 121 | ||
110 | :void collection_int::pop() { |
122 | :void collection_int::pop() { |
111 | if (count>0) count--; |
123 | if (count>0) count--; |
112 | } |
124 | } |
113 | 125 | ||
114 | :void collection_int::drop() { |
126 | :void collection_int::drop() { |
115 | element[0] = |
127 | element[0] = |
116 | count = 0; |
128 | count = 0; |
117 | } |
129 | } |
118 | 130 | ||
119 | #endif0)>0)>4096)> |
131 | #endif0)>0)>4096)> |