Subversion Repositories Kolibri OS

Rev

Rev 7930 | Rev 7973 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5959 leency 1
#ifndef INCLUDE_COLLECTION_H
2
#define INCLUDE_COLLECTION_H
3
#print "[include ]\n"
4
 
7972 leency 5
#include "array.h"
6
 
6285 leency 7
/*========================================================
8
=                                                        =
7972 leency 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
}
82
 
83
/*========================================================
84
=                                                        =
6285 leency 85
=                       String                           =
86
=                                                        =
87
========================================================*/
88
 
5959 leency 89
struct collection
90
{
5965 leency 91
	int realloc_size, count;
92
	dword data_start;
5959 leency 93
	dword data_size;
7972 leency 94
	collection_int offset;
5965 leency 95
	int add();
7049 leency 96
	int addn();
7738 leency 97
	dword get(); //get_name_by_pos
98
	dword get_pos_by_name();
5959 leency 99
	void drop();
5965 leency 100
	void increase_data_size();
7878 leency 101
	dword get_last();
7885 leency 102
	bool pop();
5959 leency 103
};
104
 
7286 leency 105
:void collection::increase_data_size() {
5965 leency 106
	int filled_size;
107
	if (realloc_size<4096) realloc_size = 4096;
108
	if (!data_size) {
109
		data_size = realloc_size;
110
		data_start = malloc(realloc_size);
111
	}
112
	else {
113
		data_size = data_size + realloc_size;
114
		data_start = realloc(data_start, data_size);
115
	}
5959 leency 116
}
117
 
7286 leency 118
:int collection::add(dword in) {
7049 leency 119
	return addn(in, strlen(in));
120
}
121
 
7286 leency 122
:int collection::addn(dword in, len) {
7972 leency 123
	if (offset.get(count)+len+2 > data_size) {
5965 leency 124
		increase_data_size();
7049 leency 125
		addn(in, len);
126
		return 1;
5965 leency 127
	}
7972 leency 128
	strncpy(data_start+offset.get(count), in, len);
5959 leency 129
	count++;
7972 leency 130
	offset.set(count, offset.get(count-1) + len + 1);
5965 leency 131
	return 1;
5959 leency 132
}
133
 
7286 leency 134
:dword collection::get(dword pos) {
5974 leency 135
	if (pos<0) || (pos>=count) return 0;
7972 leency 136
	return data_start + offset.get(pos);
5959 leency 137
}
138
 
7878 leency 139
:dword collection::get_last() {
140
	return get(count-1);
141
}
142
 
7738 leency 143
:dword collection::get_pos_by_name(dword name) {
144
	dword i;
145
	for (i=0; i
7972 leency 146
		if (strcmp(data_start + offset.get(i), name)==0) return i;
7738 leency 147
	}
148
	return -1;
149
}
150
 
7286 leency 151
:void collection::drop() {
5965 leency 152
	if (data_start) free(data_start);
7771 leency 153
	data_size = 0;
154
	data_start = 0;
7972 leency 155
	offset.drop();
7771 leency 156
	count = 0;
5959 leency 157
}
158
 
7885 leency 159
:bool collection::pop() {
160
	if (count>0) count--;
7878 leency 161
}
6285 leency 162
 
5959 leency 163
#endif