/
parent_group.c
131 lines (116 loc) · 3.66 KB
/
parent_group.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//================
// parent_group.c
//================
// Copyright 2024, Sven Bieg (svenbieg@web.de)
// http://github.com/svenbieg/Heap
//=======
// Using
//=======
#include "parent_group.h"
//========
// Access
//========
uint16_t parent_group_get_group(parent_group_t* group, size_t* pos)
{
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
for(uint16_t u=0; u<child_count; u++)
{
size_t item_count=cluster_group_get_item_count(group->children[u]);
if(*pos<item_count)
return u;
*pos-=item_count;
}
return CLUSTER_GROUP_SIZE;
}
int16_t parent_group_get_nearest_space(parent_group_t* group, int16_t pos)
{
int16_t child_count=(int16_t)cluster_group_get_child_count((cluster_group_t*)group);
int16_t before=pos-1;
int16_t after=pos+1;
while(before>=0||after<child_count)
{
if(before>=0)
{
uint16_t count=cluster_group_get_child_count(group->children[before]);
if(count<CLUSTER_GROUP_SIZE)
return before;
before--;
}
if(after<child_count)
{
uint16_t count=cluster_group_get_child_count(group->children[after]);
if(count<CLUSTER_GROUP_SIZE)
return after;
after++;
}
}
return -1;
}
//==============
// Modification
//==============
void parent_group_append_groups(parent_group_t* group, cluster_group_t* const* append, uint16_t count)
{
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
assert(child_count+count<=CLUSTER_GROUP_SIZE);
for(uint16_t u=0; u<count; u++)
{
group->children[child_count+u]=append[u];
group->item_count+=cluster_group_get_item_count(append[u]);
}
cluster_group_set_child_count((cluster_group_t*)group, child_count+count);
}
void parent_group_cleanup(heap_handle_t heap, parent_group_t* group)
{
if(!cluster_group_is_dirty((cluster_group_t*)group))
return;
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
for(uint16_t pos=0; pos<child_count; )
{
uint16_t count=cluster_group_get_child_count((cluster_group_t*)group->children[pos]);
if(count==0)
{
parent_group_remove_group(heap, (parent_group_t*)group, pos);
child_count--;
continue;
}
pos++;
}
cluster_group_set_dirty((cluster_group_t*)group, false);
}
void parent_group_insert_groups(parent_group_t* group, uint16_t at, cluster_group_t* const* insert, uint16_t count)
{
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
assert(at<=child_count);
assert(child_count+count<=CLUSTER_GROUP_SIZE);
for(uint16_t u=child_count+count-1; u>=at+count; u--)
group->children[u]=group->children[u-count];
for(uint16_t u=0; u<count; u++)
{
group->children[at+u]=insert[u];
group->item_count+=cluster_group_get_item_count(insert[u]);
}
cluster_group_set_child_count((cluster_group_t*)group, child_count+count);
}
void parent_group_remove_group(heap_handle_t heap, parent_group_t* group, uint16_t at)
{
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
assert(at<child_count);
cluster_group_t* child=group->children[at];
assert(cluster_group_get_child_count(child)==0);
assert(cluster_group_get_item_count(child)==0);
for(uint16_t u=at; u+1<child_count; u++)
group->children[u]=group->children[u+1];
cluster_group_set_child_count((cluster_group_t*)group, child_count-1);
heap_free_to_cache(heap, child);
}
void parent_group_remove_groups(parent_group_t* group, uint16_t at, uint16_t count)
{
uint16_t child_count=cluster_group_get_child_count((cluster_group_t*)group);
assert(at+count<=child_count);
for(uint16_t u=0; u<count; u++)
group->item_count-=cluster_group_get_item_count(group->children[at+u]);
for(uint16_t u=at; u+count<child_count; u++)
group->children[u]=group->children[u+count];
cluster_group_set_child_count((cluster_group_t*)group, child_count-count);
}