Przeglądaj źródła

arrayst list alloc from made not static

master
Felix Brendel 5 lat temu
rodzic
commit
bfeb3aa0c6
Nie znaleziono w bazie danych klucza dla tego podpisu ID klucza GPG: CF3E4920F479BC6E
3 zmienionych plików z 23 dodań i 11 usunięć
  1. +1
    -1
      .gitignore
  2. +19
    -10
      arraylist.hpp
  3. +3
    -0
      macros.hpp

+ 1
- 1
.gitignore Wyświetl plik

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

+ 19
- 10
arraylist.hpp Wyświetl plik

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

template <typename type>
struct Array_List {
@@ -15,6 +16,16 @@ struct Array_List {
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() {
free(data);
data = nullptr;
@@ -68,15 +79,6 @@ struct Array_List {
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) {
u32 start2 = mid + 1;
@@ -151,7 +153,14 @@ struct Array_List {

template <typename 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) {
this->alloc(l.size());


+ 3
- 0
macros.hpp Wyświetl plik

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

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


/**
* Defer *


Ładowanie…
Anuluj
Zapisz