~vladh/peony

peony/src/array.hpp -rw-r--r-- 2.0 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#pragma once

#include "memory.hpp"

template <typename T>
class Array {
public:
  MemoryPool *memory_pool = nullptr;
  const char *debug_name = nullptr;
  uint32 length = 0;
  uint32 capacity = 0;
  bool32 is_sparse = false;
  uint32 starting_idx = 0;
  T *items = nullptr;

  void alloc() {
    this->items = (T*)memory::push(
      this->memory_pool,
      sizeof(T) * this->capacity,
      this->debug_name
    );
  }

  T* push() {
    if (!this->items) {
      alloc();
    }
    assert(this->length < this->capacity);
    uint32 new_idx = this->length;
    this->length++;
    T* new_slot = &this->items[new_idx];
    return new_slot;
  }

  T* push(T new_item) {
    T* new_slot = push();
    *new_slot = new_item;
    return new_slot;
  }

  T* get(uint32 idx) {
    if (!this->items) {
      alloc();
    }
    assert(idx >= this->starting_idx && idx < this->capacity);
    if (idx >= this->length) {
      assert(this->is_sparse);
      this->length = idx + 1;
    }
    return &this->items[idx];
  }

  T* operator[](uint32 idx) {
    return get(idx);
  }

  template <typename F>
  T* find(F match) {
    for (auto item = begin(); item < end(); item++) {
      if (match(item)) {
        return item;
      }
    }
    return nullptr;
  }

  T* begin() {
    return &this->items[this->starting_idx];
  }

  T* end() {
    return &this->items[this->length];
  }

  void clear() {
    memset(this->items, 0, sizeof(T) * this->capacity);
    this->length = 0;
  }

  void delete_elements_after_index(uint32 idx) {
    memset(&this->items[idx], 0, sizeof(T) * (this->length - idx));
    this->length = idx;
  }

  Array(
    MemoryPool *memory_pool,
    uint32 capacity,
    const char *debug_name,
    bool32 is_sparse = false,
    uint32 starting_idx = 0
  ) :
    memory_pool(memory_pool),
    debug_name(debug_name),
    capacity(capacity),
    is_sparse(is_sparse),
    starting_idx(starting_idx)
  {
  }
};