made more consistent
This commit is contained in:
+15
-3
@@ -14,10 +14,20 @@ struct Array_List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
~Array_List() {
|
~Array_List() {
|
||||||
if (data) {
|
free(data);
|
||||||
free(data);
|
data = 0;
|
||||||
data = nullptr;
|
}
|
||||||
|
|
||||||
|
Array_List<type> clone() {
|
||||||
|
Array_List<type> ret;
|
||||||
|
ret.length = length;
|
||||||
|
ret.next_index = next_index;
|
||||||
|
|
||||||
|
ret.data = (type*)malloc(length * sizeof(type));
|
||||||
|
for (int i = 0; i < next_index; ++i) {
|
||||||
|
ret.data[i] = data[i];
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
type* begin() {
|
type* begin() {
|
||||||
@@ -79,6 +89,8 @@ struct Array_List {
|
|||||||
|
|
||||||
void sort(int left=-1, int right=-1) {
|
void sort(int left=-1, int right=-1) {
|
||||||
if (left == -1) {
|
if (left == -1) {
|
||||||
|
if (next_index == 0)
|
||||||
|
return;
|
||||||
sort(0, next_index - 1);
|
sort(0, next_index - 1);
|
||||||
return;
|
return;
|
||||||
} else if (left == right) {
|
} else if (left == right) {
|
||||||
|
|||||||
+27
-20
@@ -1,21 +1,19 @@
|
|||||||
#include "arraylist.hpp"
|
#include "arraylist.hpp"
|
||||||
|
|
||||||
template <typename type, unsigned int bucket_size>
|
template <typename type>
|
||||||
class Bucket_Allocator {
|
class Bucket_Allocator {
|
||||||
int latest_bucket;
|
|
||||||
int next_index_in_latest_bucket;
|
int next_index_in_latest_bucket;
|
||||||
int bucket_count;
|
|
||||||
int next_bucket_index;
|
int next_bucket_index;
|
||||||
|
int bucket_count;
|
||||||
|
unsigned int bucket_size;
|
||||||
|
|
||||||
Array_List<type*> free_list;
|
Array_List<type*> free_list;
|
||||||
type** buckets;
|
type** buckets;
|
||||||
|
|
||||||
void expand() {
|
void expand() {
|
||||||
|
// printf("realloc time\n");
|
||||||
// realloc time
|
// realloc time
|
||||||
buckets = (type**)realloc(buckets, bucket_count * 2 * sizeof(type*));
|
buckets = (type**)realloc(buckets, bucket_count * 2 * sizeof(type*));
|
||||||
for (int i = bucket_count; i < bucket_count * 2; ++i) {
|
|
||||||
buckets[i] = (type*)malloc(bucket_size * sizeof(type));
|
|
||||||
}
|
|
||||||
bucket_count *= 2;
|
bucket_count *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +23,7 @@ class Bucket_Allocator {
|
|||||||
if (next_bucket_index >= bucket_count) {
|
if (next_bucket_index >= bucket_count) {
|
||||||
expand();
|
expand();
|
||||||
}
|
}
|
||||||
|
buckets[next_bucket_index] = (type*)malloc(bucket_size * sizeof(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment_pointers(int amount = 1) {
|
void increment_pointers(int amount = 1) {
|
||||||
@@ -35,31 +34,40 @@ class Bucket_Allocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Bucket_Allocator(unsigned int initial_bucket_count = 1) {
|
Bucket_Allocator(unsigned int bucket_size, unsigned int initial_bucket_count) {
|
||||||
latest_bucket = 0;
|
this->bucket_size = bucket_size;
|
||||||
next_index_in_latest_bucket = 0;
|
next_index_in_latest_bucket = 0;
|
||||||
next_bucket_index = 0;
|
next_bucket_index = 0;
|
||||||
bucket_count = initial_bucket_count;
|
bucket_count = initial_bucket_count;
|
||||||
|
|
||||||
buckets = (type**)malloc(bucket_count * sizeof(type*));
|
buckets = (type**)malloc(bucket_count * sizeof(type*));
|
||||||
for (int i = 0; i < bucket_count; ++i) {
|
buckets[0] = (type*)malloc(bucket_size * sizeof(type));
|
||||||
buckets[i] = (type*)malloc(bucket_size * sizeof(type));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~Bucket_Allocator() {
|
~Bucket_Allocator() {
|
||||||
for (int i = 0; i < latest_bucket-1; ++i) {
|
for (int i = 0; i <= next_bucket_index; ++i) {
|
||||||
|
free(buckets[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buckets);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename proc>
|
||||||
|
void for_each(proc p) {
|
||||||
|
free_list.sort();
|
||||||
|
type* val;
|
||||||
|
for (int i = 0; i < next_bucket_index; ++i) {
|
||||||
for (int j = 0; j < bucket_size; ++j) {
|
for (int j = 0; j < bucket_size; ++j) {
|
||||||
::delete (buckets[i]+j);
|
val = buckets[i]+j;
|
||||||
|
if (free_list.sorted_find(val) == -1)
|
||||||
|
p(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (int j = 0; j < next_index_in_latest_bucket; ++j) {
|
||||||
for (int i = 0; i < next_index_in_latest_bucket; ++i) {
|
val = buckets[next_bucket_index]+j;
|
||||||
::delete (buckets[latest_bucket]+i);
|
if (free_list.sorted_find(val) == -1)
|
||||||
|
p(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
::delete[] buckets;
|
|
||||||
free_list.~Array_List();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type* allocate(unsigned int amount = 1) {
|
type* allocate(unsigned int amount = 1) {
|
||||||
@@ -93,7 +101,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void free_object(type* obj) {
|
void free_object(type* obj) {
|
||||||
delete obj;
|
|
||||||
free_list.append(obj);
|
free_list.append(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pushd $SCRIPTPATH > /dev/null
|
|||||||
time clang++ -D_DEBUG -D_PROFILING -fpermissive test.cpp -g -o ./ftb --std=c++17 || exit 1
|
time clang++ -D_DEBUG -D_PROFILING -fpermissive test.cpp -g -o ./ftb --std=c++17 || exit 1
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
time ./ftb
|
time valgrind --leak-check=full ./ftb
|
||||||
|
|
||||||
popd > /dev/null
|
popd > /dev/null
|
||||||
unset TIMEFORMAT
|
unset TIMEFORMAT
|
||||||
|
|||||||
@@ -4,33 +4,30 @@
|
|||||||
#include "bucket_allocator.hpp"
|
#include "bucket_allocator.hpp"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
Bucket_Allocator<int, 3> ba;
|
Bucket_Allocator<int> ba(2, 1);
|
||||||
|
|
||||||
int* a = ba.allocate();
|
int* a = ba.allocate();
|
||||||
*a = 1;
|
*a = 1;
|
||||||
printf("%d\n", *a);
|
|
||||||
|
|
||||||
ba.free_object(a);
|
ba.free_object(a);
|
||||||
|
|
||||||
int* b = ba.allocate();
|
int* b = ba.allocate();
|
||||||
*b = 2;
|
*b = 2;
|
||||||
printf("%d\n", *b);
|
|
||||||
|
|
||||||
int* c = ba.allocate();
|
int* c = ba.allocate();
|
||||||
*c = 3;
|
*c = 3;
|
||||||
printf("%d\n", *c);
|
|
||||||
|
|
||||||
int* d = ba.allocate();
|
int* d = ba.allocate();
|
||||||
*d = 4;
|
*d = 4;
|
||||||
printf("%d\n", *d);
|
|
||||||
|
|
||||||
int* e = ba.allocate();
|
int* e = ba.allocate();
|
||||||
*e = 5;
|
*e = 5;
|
||||||
printf("%d\n", *e);
|
|
||||||
|
|
||||||
int* f = ba.allocate();
|
int* f = ba.allocate();
|
||||||
*f = 6;
|
*f = 6;
|
||||||
printf("%d\n", *f);
|
|
||||||
|
ba.for_each([](int* i){
|
||||||
|
printf("%d\n", *i);
|
||||||
|
});
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user