-
Notifications
You must be signed in to change notification settings - Fork 19
/
sparse_array.h
1566 lines (1557 loc) · 52.9 KB
/
sparse_array.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <cstdint>
#include <algorithm>
#include <cstring>
#include <cassert>
struct sparse_array_default_config
{
typedef void *handle_t;
enum
{
memory_size = 512, //内存是按块分配,这里定义每个内存块大小(类型不同,最小值也不同,过小编译报错)
atomic_length = 4, //最小连续数据长度(类型不同,最小值也不同,过小编译报错)
invalid_handle = 0, //非法内存句柄(可以认为是nil)
};
//分配一块memory_size长度的内存
handle_t alloc()
{
return ::malloc(memory_size);
}
//回收一块已分配的内存
void dealloc(handle_t handle)
{
::free(handle);
}
//从内存句柄得到内存地址
void *get(handle_t handle)
{
return handle;
}
};
template<class value_t, class config_t = sparse_array_default_config>
class sparse_array
{
public:
typedef sparse_array<value_t, config_t> self_t;
typedef typename config_t::handle_t handle_t;
enum
{
memory_size = config_t::memory_size,
atomic_length = config_t::atomic_length,
invalid_handle = config_t::invalid_handle,
};
//内存片段
struct sparse_range
{
uint32_t index;
uint16_t length;
uint16_t offset;
handle_t handle;
};
//红黑树节点
struct sparse_range_set_base
{
handle_t parent_handle;
handle_t left_handle;
handle_t right_handle;
uint32_t black : 1;
uint32_t nil : 1;
uint32_t length : 30;
};
//数据
struct dump_data
{
handle_t parent_handle;
handle_t left_handle;
handle_t right_handle;
handle_t list_handle;
handle_t free_handle;
uint32_t free_length;
uint16_t free_offset;
uint16_t free_cross;
};
//红黑树根节点+内存适配器
class sparse_range_tree
{
public:
sparse_range_set_base const *root() const
{
return &root_;
}
sparse_range_set_base *root()
{
return &root_;
}
handle_t alloc()
{
return root_.alloc();
}
config_t const &allocator() const
{
return root_;
}
config_t &allocator()
{
return root_;
}
void dealloc(handle_t handle)
{
root_.dealloc(handle);
}
void *deref(handle_t handle) const
{
return root_.get(handle);
}
private:
struct sparse_range_root : public sparse_range_set_base, public config_t
{
};
mutable sparse_range_root root_;
};
//红黑树数据节点,内存片段集合(数据是有序的)
struct sparse_range_set : public sparse_range_set_base
{
uint32_t end;
sparse_range begin[1];
};
//空闲内存链表
struct memory_blcok_free
{
handle_t handle;
uint16_t offset;
uint16_t cross;
};
//数据块元素(数据/空闲链表节点)
struct memory_block_data
{
union
{
value_t data[atomic_length];
memory_blcok_free free;
};
};
//数据块
struct memory_block
{
handle_t next_handle;
handle_t prev_handle;
memory_block_data data[1];
};
struct sparse_range_op
{
bool operator()(sparse_range const &left, uint32_t const &right) const
{
return left.index < right;
}
bool operator()(uint32_t const &left, sparse_range const &right) const
{
return left < right.index;
}
};
enum
{
//每个片段集合有多少个片段
range_length = (memory_size - sizeof(sparse_range_set)) / sizeof(sparse_range) + 1,
//每个数据块有多少个元素
block_length = (memory_size - sizeof(memory_block)) / sizeof(memory_block_data) + 1,
};
//operator[]代理
class index_proxy
{
public:
operator value_t() const
{
return self_.get(index_);
}
index_proxy const &operator = (value_t const &value) const
{
self_.set(index_, value);
return *this;
}
index_proxy const &operator = (index_proxy const &other) const
{
self_.set(index_, other.self_.get(other.index_));
return *this;
}
index_proxy(uint32_t index, self_t &self) : index_(index), self_(self)
{
}
private:
uint32_t index_;
self_t &self_;
};
public:
sparse_array()
{
static_assert(memory_size >= sizeof(memory_block), "low memory_size");
static_assert(memory_size >= sizeof(sparse_range_set), "low memory_size");
static_assert(range_length >= 3, "low memory_size");
static_assert(sizeof(value_t) * atomic_length >= sizeof(memory_blcok_free), "low atomic_length");
block_handle_ = handle_t(invalid_handle);
free_.handle = handle_t(invalid_handle);
//free_.offset = 0;
//free_.cross = 0;
index_tree_.root()->length = 0;
rb_set_nil_(rb_nil_(), true);
rb_set_root_(rb_nil_());
rb_set_most_left_(rb_nil_());
rb_set_most_right_(rb_nil_());
}
~sparse_array()
{
for(handle_t rb_it = rb_get_most_left_(); rb_it != rb_nil_(); )
{
//非POD对象,没必要析构
if(!std::is_pod<value_t>::value)
{
sparse_range_set *range_set = deref_<sparse_range_set>(rb_it);
for(sparse_range *range = range_set->begin, *range_end = range + range_set->end; range != range_end; ++range)
{
memory_block_data *data = deref_<memory_block>(range->handle)->data + range->offset;
for(value_t *it = data->data, *end = it + atomic_length; it != end; ++it)
{
it->~value_t();
}
}
}
handle_t next_it = rb_move_<true>(rb_it);
rb_erase_(rb_it);
index_tree_.dealloc(rb_it);
rb_it = next_it;
}
while(block_handle_ != handle_t(invalid_handle))
{
handle_t next_handle = deref_<memory_block>(block_handle_)->next_handle;
index_tree_.dealloc(block_handle_);
block_handle_ = next_handle;
}
}
//内存适配器
config_t const &allocator() const
{
return index_tree_.allocator();
}
//内存适配器
config_t &allocator()
{
return index_tree_.allocator();
}
//dump(配合allocator,请自己保存allocator状态)
dump_data dump() const
{
dump_data d;
d.parent_handle = index_tree_.root()->parent_handle;
d.left_handle = index_tree_.root()->left_handle;
d.right_handle = index_tree_.root()->right_handle;
d.list_handle = block_handle_;
d.free_handle = free_.handle;
d.free_length = index_tree_.root()->length;
d.free_offset = free_.offset;
d.free_cross = free_.cross;
return d;
}
//load_dump(配合allocator,load后恢复allocator状态)
void load_dump(dump_data const &d)
{
clear();
index_tree_.root()->parent_handle = d.parent_handle;
index_tree_.root()->left_handle = d.left_handle;
index_tree_.root()->right_handle = d.right_handle;
block_handle_ = d.list_handle;
free_.handle = d.free_handle;
index_tree_.root()->length = d.free_length;
free_.offset = d.free_offset;
free_.cross = d.free_cross;
}
//元素数量(这里不是真正数量,实际没有什么上限,这里只是对齐后的最大下标
uint32_t size() const
{
if(rb_is_nil_(rb_get_root_()))
{
return 0;
}
else
{
return deref_<sparse_range_set>(rb_get_most_right_())->max_index;
}
}
//获取元素
value_t get(uint32_t index) const
{
sparse_range *range = find_index_(index, NULL);
if(range == NULL)
{
return value_t();
}
return deref_<memory_block>(range->handle)->data[range->offset].data[index - range->index];
}
//添加元素
void set(uint32_t index, value_t const &value)
{
handle_t find;
sparse_range *range = find_index_(index, &find);
if(range != NULL)
{
deref_<memory_block>(range->handle)->data[range->offset].data[index - range->index] = value;
}
else if(value != value_t())
{
create_new_range_(find, index, &value, 1);
}
}
//批量获取元素
void get_multi(uint32_t index, value_t *out_arr, uint32_t length) const
{
assert(length == 0 || out_arr != NULL);
assert(length >= 0);
if(length == 0)
{
return;
}
while(true)
{
uint32_t copy_length;
sparse_range *range = find_index_(index, NULL);
if(range != NULL)
{
copy_length = std::min<uint32_t>(length, range->index + range->length - index);
value_t *ptr = deref_<memory_block>(range->handle)->data[range->offset].data + (index - range->index);
std::copy(ptr, ptr + copy_length, out_arr);
}
else
{
copy_length = std::min<uint32_t>(length, atomic_length - index % atomic_length);
std::fill(out_arr, out_arr + copy_length, value_t());
}
length -= copy_length;
if(length == 0)
{
return;
}
index += copy_length;
out_arr += copy_length;
}
}
//批量设置元素
void set_multi(uint32_t index, value_t const *in_arr, uint32_t length)
{
assert(length == 0 || in_arr != NULL);
assert(length >= 0);
if(length == 0)
{
return;
}
value_t default_value = value_t();
uint32_t offset = 0, i = 1;
struct check
{
static bool is_default(self_t *self, uint32_t index, value_t const *in_arr, uint32_t length, uint32_t &i, uint32_t &offset, value_t const &default_value)
{
while(i < length && in_arr[i] == default_value)
{
++i;
}
self->erase_index_range_(index + offset, i - offset);
if(i == length)
{
return true;
}
offset = i;
return false;
}
static bool is_value(self_t *self, uint32_t index, value_t const *in_arr, uint32_t length, uint32_t &i, uint32_t &offset, value_t const &default_value)
{
while(i < length && in_arr[i] != default_value)
{
++i;
}
self->set_multi_without_check_(index + offset, in_arr + offset, i - offset);
if(i == length)
{
return true;
}
offset = i;
return false;
}
};
if(in_arr[0] == default_value)
{
while(true)
{
if(check::is_default(this, index, in_arr, length, i, offset, default_value))
{
break;
}
if(check::is_value(this, index, in_arr, length, i, offset, default_value))
{
break;
}
}
}
else
{
while(true)
{
if(check::is_value(this, index, in_arr, length, i, offset, default_value))
{
break;
}
if(check::is_default(this, index, in_arr, length, i, offset, default_value))
{
break;
}
}
}
}
//清空
void clear()
{
self_t::~sparse_array();
::new(this) self_t();
}
//擦除部分区间
void clear(uint32_t index, uint32_t length)
{
erase_index_range_(index, length);
}
//下标方式访问
value_t operator[](uint32_t index) const
{
return get(index);
}
//下标方式访问
index_proxy operator[](uint32_t index)
{
return index_proxy(index, *this);
}
private:
//从片段集合树中找到片段
sparse_range *find_index_(uint32_t index, handle_t *out_handle) const
{
handle_t find = rb_special_bound_(index);
if(out_handle != NULL)
{
*out_handle = find;
}
if(find == rb_nil_())
{
return NULL;
}
sparse_range_set *range_set = deref_<sparse_range_set>(find);
sparse_range *range = &range_set->begin[range_set->end - 1];
if(index >= range->index + range->length)
{
return NULL;
}
sparse_range *const end = range_set->begin + range_set->end;
range = special_bound_(range_set->begin, end, index);
if(range == end || index >= range->index + range->length)
{
return NULL;
}
return range;
}
//分配一个数据块
handle_t alloc_block_(memory_block **out_block)
{
handle_t new_handle = index_tree_.alloc();
memory_block *block = deref_<memory_block>(new_handle);
block->next_handle = block_handle_;
block->prev_handle = handle_t(invalid_handle);
if(block_handle_ != handle_t(invalid_handle))
{
deref_<memory_block>(block_handle_)->prev_handle = new_handle;
}
block_handle_ = new_handle;
if(out_block != NULL)
{
*out_block = block;
}
return block_handle_;
}
//回收一个数据块
void dealloc_block_(handle_t handle)
{
memory_block *block = deref_<memory_block>(handle);
if(block->next_handle != handle_t(invalid_handle))
{
deref_<memory_block>(block->next_handle)->prev_handle = block->prev_handle;
}
if(block->prev_handle != handle_t(invalid_handle))
{
deref_<memory_block>(block->prev_handle)->next_handle = block->next_handle;
}
if(handle == block_handle_)
{
block_handle_ = block->next_handle;
}
index_tree_.dealloc(handle);
}
//分配一块内存(优先匹配合适大小,找不到就拆分大内存,再找不到就开辟新内存块)
void alloc_(uint32_t cross, handle_t &out_handle, uint16_t &out_offset)
{
assert(cross >= 1);
assert(cross <= block_length);
memory_blcok_free *result = NULL, *parent = &free_;
for(memory_blcok_free *it = &free_; it->handle != handle_t(invalid_handle);)
{
memory_block_data *next_data = deref_<memory_block>(it->handle)->data + it->offset;
if(it->cross == cross)
{
result = &next_data->free;
parent = it;
break;
}
if(it->cross > cross && (result == NULL || it->cross < parent->cross))
{
result = &next_data->free;
parent = it;
}
it = &next_data->free;
}
if(result == NULL)
{
memory_block *block;
handle_t new_handle = alloc_block_(&block);
out_handle = new_handle;
out_offset = 0;
if(cross < block_length)
{
block->data[cross].free = free_;
free_.handle = new_handle;
free_.offset = cross;
free_.cross = block_length - cross;
index_tree_.root()->length += free_.cross;
}
}
else
{
out_handle = parent->handle;
out_offset = parent->offset;
if(parent->cross == cross)
{
*parent = *result;
}
else
{
parent->offset += cross;
parent->cross -= cross;
memory_block_data *next_data = deref_<memory_block>(parent->handle)->data + parent->offset;
next_data->free = *result;
}
index_tree_.root()->length -= cross;
}
}
//回收一块内存(优先合并到相邻内存)
void dealloc_(handle_t handle, uint16_t offset, uint32_t cross)
{
assert(cross >= 1);
assert(cross <= block_length);
if(cross == block_length)
{
dealloc_block_(handle);
return;
}
for(memory_blcok_free *it = &free_; it->handle != handle_t(invalid_handle);)
{
memory_block_data *next_data = deref_<memory_block>(it->handle)->data + it->offset;
if(it->handle == handle)
{
if(it->offset + it->cross == offset)
{
index_tree_.root()->length -= it->cross;
offset = it->offset;
cross += it->cross;
*it = deref_<memory_block>(handle)->data[it->offset].free;
dealloc_(handle, offset, cross);
return;
}
if(it->offset == offset + cross)
{
index_tree_.root()->length -= it->cross;
cross += it->cross;
*it = deref_<memory_block>(handle)->data[it->offset].free;
dealloc_(handle, offset, cross);
return;
}
}
it = &next_data->free;
}
index_tree_.root()->length += cross;
deref_<memory_block>(handle)->data[offset].free = free_;
free_.handle = handle;
free_.offset = offset;
free_.cross = cross;
}
//真正的批量设置元素(前面的set_multi会过滤掉为默认值的元素)
void set_multi_without_check_(uint32_t index, value_t const *in_arr, uint32_t length)
{
handle_t find;
while(true)
{
uint32_t copy_length;
sparse_range *range = find_index_(index, &find);
if(range != NULL)
{
copy_length = std::min<uint32_t>(length, range->index + range->length - index);
value_t *ptr = deref_<memory_block>(range->handle)->data[range->offset].data + (index - range->index);
std::copy(in_arr, in_arr + copy_length, ptr);
}
else
{
copy_length = std::min<uint32_t>(length, atomic_length - index % atomic_length);
create_new_range_(find, index, in_arr, copy_length);
}
length -= copy_length;
if(length == 0)
{
return;
}
index += copy_length;
in_arr += copy_length;
}
}
//擦除一段数据(恢复到默认值)
void erase_index_range_(uint32_t index, uint32_t length)
{
handle_t find;
while(true)
{
uint32_t erase_length;
sparse_range *range = find_index_(index, &find);
if(range != NULL)
{
erase_length = std::min<uint32_t>(length, range->index + range->length - index);
if(erase_length == range->length && index == range->index)
{
remove_range_(find, range);
}
else
{
value_t *ptr = deref_<memory_block>(range->handle)->data[range->offset].data + (index - range->index);
std::fill(ptr, ptr + erase_length, value_t());
}
}
else
{
erase_length = std::min<uint32_t>(length, atomic_length - index % atomic_length);
}
length -= erase_length;
if(length == 0)
{
return;
}
index += erase_length;
}
}
template<class iterator_t> static iterator_t special_bound_(iterator_t bound_begin, iterator_t bound_end, uint32_t index)
{
uint32_t count = std::distance(bound_begin, bound_end);
iterator_t where = bound_end;
while(0 < count)
{
uint32_t half_count = count / 2;
iterator_t mid = std::next(bound_begin, half_count);
if(!sparse_range_op()(index, *mid))
{
where = mid;
bound_begin = ++mid;
count -= half_count + 1;
}
else
{
count = half_count;
}
}
return where;
}
//创建一个新的片段
void create_new_range_(handle_t where, uint32_t index, value_t const *arr, uint32_t length)
{
assert(index % atomic_length + length <= atomic_length);
if(merge_range_(index, arr, length))
{
return;
}
sparse_range range_insert =
{
index - index % atomic_length, atomic_length
};
alloc_(1, range_insert.handle, range_insert.offset);
memory_block_data *data = deref_<memory_block>(range_insert.handle)->data + range_insert.offset;
init_range_data_(data->data + (index - range_insert.index), data->data, data->data + atomic_length, arr, length);
insert_range_(where, &range_insert);
}
//初始化片段数据
void init_range_data_(value_t *ptr, value_t *begin, value_t *end, value_t const *arr, uint32_t length)
{
for(value_t *it = begin; it != end; )
{
if(it == ptr)
{
while(length-- > 0)
{
::new(it++) value_t(*arr++);
}
ptr = NULL;
}
else
{
::new(it++) value_t();
}
}
}
//调整片段头部大小
void adjust_range_head_(handle_t handle, sparse_range *range, int32_t change)
{
assert(change != 0);
assert(range->length / atomic_length + change >= 1);
assert(range->length / atomic_length + change <= block_length);
sparse_range adjust = *range;
alloc_(range->length / atomic_length + change, adjust.handle, adjust.offset);
if(change > 0)
{
value_t *from = deref_<memory_block>(range->handle)->data[range->offset].data;
value_t *to = deref_<memory_block>(adjust.handle)->data[adjust.offset + change].data;
for(value_t *end = from + range->length; from != end; ++from, ++to)
{
::new(to) value_t(std::move(*from));
from->~value_t();
}
}
else
{
value_t *from = deref_<memory_block>(range->handle)->data[range->offset].data;
value_t *to = deref_<memory_block>(adjust.handle)->data[adjust.offset].data;
uint32_t cut_length = uint32_t(-change) * atomic_length;
for(value_t *end = from + cut_length; from != end; ++from)
{
from->~value_t();
}
for(value_t *end = from + (range->length - cut_length); from != end; ++from, ++to)
{
::new(to) value_t(std::move(*from));
from->~value_t();
}
}
dealloc_(range->handle, range->offset, range->length / atomic_length);
adjust.index -= change * atomic_length;
adjust.length += change * atomic_length;
*range = adjust;
}
//调整片段尾部大小
void adjust_range_tail_(handle_t handle, sparse_range *range, int32_t change)
{
assert(change != 0);
assert(range->length / atomic_length + change >= 1);
assert(range->length / atomic_length + change <= block_length);
sparse_range adjust = *range;
alloc_(range->length / atomic_length + change, adjust.handle, adjust.offset);
if(change > 0)
{
value_t *from = deref_<memory_block>(range->handle)->data[range->offset].data;
value_t *to = deref_<memory_block>(adjust.handle)->data[adjust.offset].data;
for(value_t *end = from + range->length; from != end; ++from, ++to)
{
::new(to) value_t(std::move(*from));
from->~value_t();
}
}
else
{
value_t *from = deref_<memory_block>(range->handle)->data[range->offset].data;
value_t *to = deref_<memory_block>(adjust.handle)->data[adjust.offset + change].data;
uint32_t cut_length = uint32_t(-change) * atomic_length;
for(value_t *end = from + (range->length - cut_length); from != end; ++from, ++to)
{
::new(to) value_t(std::move(*from));
from->~value_t();
}
for(value_t *end = from + cut_length; from != end; ++from)
{
from->~value_t();
}
}
dealloc_(range->handle, range->offset, range->length / atomic_length);
adjust.length += change * atomic_length;
*range = adjust;
}
//尝试合并数据到相邻的片段
bool merge_range_(uint32_t index, value_t const *arr, uint32_t length)
{
uint32_t fix_index = index - index % atomic_length;
handle_t find_before, find_after;
sparse_range *range_before, *range_after;
if(fix_index > 0)
{
range_before = find_index_(fix_index - atomic_length, &find_before);
}
else
{
range_before = NULL;
find_before = handle_t(invalid_handle);
}
range_after = find_index_(fix_index + atomic_length, &find_after);
if(range_before != NULL && range_after != NULL && range_before->length + atomic_length + range_after->length <= block_length * atomic_length)
{
sparse_range before = *range_before;
adjust_range_tail_(find_before, range_before, (atomic_length + range_after->length) / atomic_length);
value_t *to = deref_<memory_block>(range_before->handle)->data[range_before->offset + before.length / atomic_length].data;
value_t *from = deref_<memory_block>(range_after->handle)->data[range_after->offset].data;
init_range_data_(to + (index - fix_index), to, to + atomic_length, arr, length);
to += atomic_length;
for(value_t *end = from + range_after->length; from != end; ++from, ++to)
{
::new(to) value_t(std::move(*from));
from->~value_t();
}
remove_range_(find_after, range_after);
return true;
}
if(range_before != NULL && range_before->length + atomic_length <= block_length * atomic_length)
{
sparse_range before = *range_before;
adjust_range_tail_(find_before, range_before, 1);
value_t *to = deref_<memory_block>(range_before->handle)->data[range_before->offset + before.length / atomic_length].data;
init_range_data_(to + (index - fix_index), to, to + atomic_length, arr, length);
return true;
}
if(range_after != NULL && range_after->length + atomic_length <= block_length * atomic_length)
{
adjust_range_head_(find_after, range_after, 1);
value_t *to = deref_<memory_block>(range_after->handle)->data[range_after->offset].data;
init_range_data_(to + (index - fix_index), to, to + atomic_length, arr, length);
return true;
}
return false;
}
//拆分片段集合,2/3
void split_range_set_(handle_t where_left, handle_t where_right)
{
sparse_range_set *range_set_left = deref_<sparse_range_set>(where_left);
sparse_range_set *range_set_right = deref_<sparse_range_set>(where_right);
handle_t handle = index_tree_.alloc();
sparse_range_set *range_set = deref_<sparse_range_set>(handle);
enum
{
move_size = range_length / 3
};
std::memmove(range_set->begin, range_set_left->begin + range_set_left->end - move_size, move_size * sizeof(sparse_range));
range_set_left->end -= move_size;
std::memmove(range_set->begin + move_size, range_set_right->begin, move_size * sizeof(sparse_range));
range_set->end = move_size * 2;
range_set_right->end -= move_size;
std::memmove(range_set_right->begin, range_set_right->begin + move_size, range_set_right->end * sizeof(sparse_range));
rb_insert_(handle);
}
//拆分片段集合,1/2
void split_range_set_(handle_t where)
{
sparse_range_set *range_set_left = deref_<sparse_range_set>(where);
handle_t handle = index_tree_.alloc();
sparse_range_set *range_set = deref_<sparse_range_set>(handle);
enum
{
move_size = range_length / 2
};
std::memmove(range_set->begin, range_set_left->begin + range_set_left->end - move_size, move_size * sizeof(sparse_range));
range_set_left->end -= move_size;
range_set->end = move_size;
rb_insert_(handle);
}
//插入一块片段
void insert_range_(handle_t where, sparse_range *range)
{
if(rb_is_nil_(rb_get_root_()))
{
handle_t handle = index_tree_.alloc();
sparse_range_set *range_set = deref_<sparse_range_set>(handle);
range_set->end = 1;
*range_set->begin = *range;
rb_insert_(handle);
return;
}
if(where == rb_nil_())
{
where = rb_get_most_left_();
}
sparse_range_set *range_set = deref_<sparse_range_set>(where);
if(range_set->end == range_length)
{
sparse_range_set *range_set_left = nullptr;
sparse_range_set *range_set_right = nullptr;
handle_t left = rb_move_<false>(where);
if(left != rb_nil_())
{
range_set_left = deref_<sparse_range_set>(left);
if(range_set_left->end != range_length)
{
if(range_set->begin->index < range->index)
{
range_set_left->begin[range_set_left->end++] = *range_set->begin;
set_remove_range_(range_set, range_set->begin);
set_insert_range_(range_set, range);
}
else
{
range_set_left->begin[range_set_left->end++] = *range;
}
return;
}
}
handle_t right = rb_move_<true>(where);
if(right != rb_nil_())
{
range_set_right = deref_<sparse_range_set>(right);
if(range_set_right->end != range_length)
{
sparse_range *back_range = range_set->begin + range_set->end - 1;
if(range->index < back_range->index)
{
set_insert_range_(range_set_right, back_range);
--range_set->end;
set_insert_range_(range_set, range);
}
else
{
set_insert_range_(range_set_right, range);
}
return;
}
}
if(range_set_right != nullptr)
{
split_range_set_(where, right);
}
else if(range_set_left != nullptr)
{
split_range_set_(left, where);
}
else
{
split_range_set_(where);
}
if((where = rb_special_bound_(range->index)) == rb_nil_())
{
where = rb_get_most_left_();
}
range_set = deref_<sparse_range_set>(where);
}
set_insert_range_(range_set, range);
}
//强制插入片段
void set_insert_range_(sparse_range_set *range_set, sparse_range *range)
{
sparse_range *const end = range_set->begin + range_set->end;
sparse_range *where = std::lower_bound(range_set->begin, end, range->index, sparse_range_op());
if(where != end)
{
std::memmove(where + 1, where, (end - where) * sizeof(sparse_range));
}
*where = *range;
++range_set->end;
}
//合并片段集合
void merge_range_set_(handle_t left, handle_t right)
{
sparse_range_set *range_set_left = deref_<sparse_range_set>(left);
sparse_range_set *range_set_right = deref_<sparse_range_set>(right);
std::memmove(range_set_left->begin + range_set_left->end, range_set_right->begin, range_set_right->end * sizeof(sparse_range));
range_set_left->end += range_set_right->end;
rb_erase_(right);
index_tree_.dealloc(right);
}
//移除一个片段
void remove_range_(handle_t where, sparse_range *range)
{
dealloc_(range->handle, range->offset, range->length / atomic_length);
sparse_range_set *range_set = deref_<sparse_range_set>(where);
if(range_set->end == 1)
{
rb_erase_(where);
index_tree_.dealloc(where);
return;
}
set_remove_range_(range_set, range);
enum
{
move_size = range_length / 3
};
if(range_set->end < range_length / 2)
{
handle_t left = rb_move_<false>(where);
sparse_range_set *range_set_left = nullptr;
if(left != rb_nil_())
{
range_set_left = deref_<sparse_range_set>(left);
if(range_set_left->end >range_length / 2)
{
sparse_range *back_range = range_set_left->begin + range_set_left->end - 1;
set_insert_range_(range_set, back_range);
--range_set_left->end;
return;