Bladeren bron

arrayst list alloc from made not static

master
Felix Brendel 5 jaren geleden
bovenliggende
commit
bfeb3aa0c6
Geen bekende sleutel gevonden voor deze handtekening in de database GPG sleutel-ID: CF3E4920F479BC6E
3 gewijzigde bestanden met toevoegingen van 23 en 11 verwijderingen
  1. +1
    -1
      .gitignore
  2. +19
    -10
      arraylist.hpp
  3. +3
    -0
      macros.hpp

+ 1
- 1
.gitignore Bestand weergeven

@@ -1,4 +1,4 @@
/bin/* /bin/*
*.report *.report
/ftb /ftb
vgcore.*
vgcore.*

+ 19
- 10
arraylist.hpp Bestand weergeven

@@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <initializer_list> #include <initializer_list>
#include "types.hpp" #include "types.hpp"
#include "macros.hpp"


template <typename type> template <typename type>
struct Array_List { struct Array_List {
@@ -15,6 +16,16 @@ struct Array_List {
length = initial_capacity; length = initial_capacity;
} }


void alloc_from(std::initializer_list<type> l) {
length = max(l.size(), 1); // alloc at least one

data = (type*)malloc(length * sizeof(type));
count = 0;
for (type t : l) {
data[count++] = t;
}
}

void dealloc() { void dealloc() {
free(data); free(data);
data = nullptr; data = nullptr;
@@ -68,15 +79,6 @@ struct Array_List {
return data[index]; return data[index];
} }


static Array_List<type> alloc_from(std::initializer_list<type> l) {
Array_List<type> ret;
ret.alloc(l.size());
for (auto e : l) {
ret.append(e);
}
ret.auto_free = true;
return ret;
}


void _merge(u32 start, u32 mid, u32 end) { void _merge(u32 start, u32 mid, u32 end) {
u32 start2 = mid + 1; u32 start2 = mid + 1;
@@ -151,7 +153,14 @@ struct Array_List {


template <typename type> template <typename type>
struct Auto_Array_List : public Array_List<type> { struct Auto_Array_List : public Array_List<type> {
Auto_Array_List() = default;

Auto_Array_List(u32 length) {
this->alloc(length);
}

Auto_Array_List() {
this->alloc(16);
}


Auto_Array_List(std::initializer_list<type> l) { Auto_Array_List(std::initializer_list<type> l) {
this->alloc(l.size()); this->alloc(l.size());


+ 3
- 0
macros.hpp Bestand weergeven

@@ -6,6 +6,9 @@
#define label(x, y) concat(x, y) #define label(x, y) concat(x, y)
#define line_label(x) label(x, __LINE__) #define line_label(x) label(x, __LINE__)


#define min(a, b) ((a) < (b)) ? (a) : (b)
#define max(a, b) ((a) > (b)) ? (a) : (b)



/** /**
* Defer * * Defer *


Laden…
Annuleren
Opslaan