2022-10-30 14:24:06 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <algorithm>
|
2022-10-30 14:24:06 -04:00
|
|
|
#include <map>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <string>
|
2023-12-21 09:13:39 -05:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <set>
|
2022-10-30 14:24:06 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace geode::base {
|
2022-10-30 14:59:20 -04:00
|
|
|
uintptr_t get();
|
2022-10-30 14:24:06 -04:00
|
|
|
}
|
|
|
|
|
2024-01-19 18:33:17 -05:00
|
|
|
#if defined(GEODE_IS_ANDROID)
|
2023-08-27 12:34:33 -04:00
|
|
|
|
|
|
|
#include "gnustl-map.hpp"
|
2024-09-16 08:52:54 -04:00
|
|
|
#include "gnustl/unordered_map.hpp"
|
|
|
|
#include "gnustl/unordered_set.hpp"
|
|
|
|
#include "gnustl/hash_specialization.hpp"
|
|
|
|
#undef _GLIBCXX_RELEASE
|
|
|
|
#undef __GLIBCXX__
|
|
|
|
#undef _GLIBCXX_USE_DUAL_ABI
|
2023-08-27 12:34:33 -04:00
|
|
|
|
2022-10-30 14:24:06 -04:00
|
|
|
namespace gd {
|
2023-08-27 12:34:33 -04:00
|
|
|
using namespace geode::stl;
|
|
|
|
|
2024-01-13 08:43:53 -05:00
|
|
|
void* operatorNew(size_t size);
|
|
|
|
void operatorDelete(void* ptr);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class allocator : public std::allocator<T> {
|
|
|
|
public:
|
|
|
|
typedef size_t size_type;
|
|
|
|
typedef T* pointer;
|
|
|
|
typedef const T* const_pointer;
|
|
|
|
|
|
|
|
template<typename _Tp1>
|
|
|
|
struct rebind {
|
|
|
|
typedef allocator<_Tp1> other;
|
|
|
|
};
|
|
|
|
|
|
|
|
pointer allocate(size_type n, const void *hint=0) {
|
|
|
|
return (pointer)operatorNew(n * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(pointer p, size_type n) {
|
|
|
|
return operatorDelete(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator() throw(): std::allocator<T>() { }
|
|
|
|
allocator(const allocator &a) throw(): std::allocator<T>(a) { }
|
2024-03-15 12:09:21 -04:00
|
|
|
template <class U>
|
2024-01-13 08:43:53 -05:00
|
|
|
allocator(const allocator<U> &a) throw(): std::allocator<T>(a) { }
|
|
|
|
~allocator() throw() { }
|
|
|
|
};
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename K, typename V>
|
|
|
|
class GEODE_DLL map {
|
|
|
|
protected:
|
|
|
|
std::less<K> compare;
|
|
|
|
_rb_tree_base m_header;
|
|
|
|
size_t m_nodecount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef _rb_tree_node<std::pair<K, V>>* _tree_node;
|
2023-08-27 12:34:33 -04:00
|
|
|
typedef _rb_tree_iterator<std::pair<K, V>> iterator;
|
|
|
|
|
|
|
|
std::map<K, V> std() {
|
|
|
|
return (std::map<K, V>)(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::map<K, V>() {
|
|
|
|
auto iter_node = static_cast<_tree_node>(m_header.m_left);
|
|
|
|
auto end_node = static_cast<_tree_node>(&m_header);
|
|
|
|
std::map<K, V> out;
|
|
|
|
for (; iter_node != end_node;
|
2024-01-13 08:43:53 -05:00
|
|
|
iter_node = static_cast<_tree_node>(_rb_increment(iter_node))) {
|
2023-08-27 12:34:33 -04:00
|
|
|
out[iter_node->m_value.first] = iter_node->m_value.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::map<K, V>() const {
|
|
|
|
auto iter_node = static_cast<_tree_node>(m_header.m_left);
|
|
|
|
auto end_node = (_tree_node)(&m_header);
|
|
|
|
std::map<K, V> out;
|
|
|
|
for (; iter_node != end_node;
|
2024-01-13 08:43:53 -05:00
|
|
|
iter_node = static_cast<_tree_node>(_rb_increment(iter_node))) {
|
2023-08-27 12:34:33 -04:00
|
|
|
out[iter_node->m_value.first] = iter_node->m_value.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(_tree_node x, _tree_node p, std::pair<K, V> const& val) {
|
|
|
|
bool insert_left =
|
|
|
|
(x != 0 || p == static_cast<_tree_node>(&m_header) || val.first < p->m_value.first);
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
_tree_node z = new _rb_tree_node<std::pair<K, V>>();
|
|
|
|
z->m_value = val;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
_rb_insert_rebalance(insert_left, z, p, m_header);
|
|
|
|
++m_nodecount;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:53:43 -04:00
|
|
|
void insert(std::pair<K, V> const& val) {
|
|
|
|
insert_pair(val);
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
void insert_pair(std::pair<K, V> const& val) {
|
|
|
|
_tree_node x = static_cast<_tree_node>(m_header.m_parent);
|
|
|
|
_tree_node y = static_cast<_tree_node>(&m_header);
|
|
|
|
bool comp = true;
|
|
|
|
while (x != 0) {
|
|
|
|
y = x;
|
|
|
|
comp = val.first < x->m_value.first;
|
|
|
|
x = comp ? static_cast<_tree_node>(x->m_left) : static_cast<_tree_node>(x->m_right);
|
|
|
|
}
|
|
|
|
auto iter = y;
|
|
|
|
|
|
|
|
if (comp) {
|
|
|
|
if (iter == static_cast<_tree_node>(m_header.m_left)) {
|
|
|
|
insert(x, y, val);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iter = static_cast<_tree_node>(_rb_decrement(iter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iter->m_value.first < val.first) {
|
|
|
|
insert(x, y, val);
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
map(std::map<K, V> input) {
|
|
|
|
m_header.m_isblack = false;
|
|
|
|
m_header.m_parent = 0;
|
|
|
|
m_header.m_left = &m_header;
|
|
|
|
m_header.m_right = &m_header;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
for (auto i : input) {
|
|
|
|
insert_pair(i);
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
void erase(_tree_node x) {
|
|
|
|
while (x != 0) {
|
|
|
|
erase(static_cast<_tree_node>(x->m_right));
|
|
|
|
auto y = static_cast<_tree_node>(x->m_left);
|
|
|
|
delete y;
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:44:29 -04:00
|
|
|
std::pair<iterator, iterator> equal_range(K const& __k) {
|
2023-08-27 12:34:33 -04:00
|
|
|
return std::pair<iterator, iterator>(lower_bound(__k), upper_bound(__k));
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
size_t erase(K const& __x) {
|
|
|
|
std::pair<iterator, iterator> __p = equal_range(__x);
|
2023-08-27 12:44:29 -04:00
|
|
|
size_t __old = size();
|
2023-08-27 12:34:33 -04:00
|
|
|
erase(__p.first, __p.second);
|
2023-08-27 12:44:29 -04:00
|
|
|
return __old - size();
|
2023-08-27 12:34:33 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
void clear() {
|
|
|
|
erase(static_cast<_tree_node>(m_header.m_parent));
|
|
|
|
m_header.m_parent = 0;
|
|
|
|
m_header.m_left = &m_header;
|
|
|
|
m_header.m_right = &m_header;
|
|
|
|
m_nodecount = 0;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
void erase(iterator __first, iterator __last) {
|
|
|
|
if (__first == begin() && __last == end()) {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (__first != __last) {
|
|
|
|
erase(__first++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void erase(iterator __pos) {
|
|
|
|
_tree_node __y = static_cast<_tree_node>(_rb_rebalance_for_erase(
|
2023-08-27 12:44:29 -04:00
|
|
|
__pos.m_node, m_header
|
|
|
|
));
|
2023-08-27 12:34:33 -04:00
|
|
|
delete __y;
|
|
|
|
--m_nodecount;
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:40:49 -04:00
|
|
|
V& operator[](K const& __k) {
|
|
|
|
iterator __i = lower_bound(__k);
|
|
|
|
if (__i == end() || compare(__k, (*__i).first)) {
|
2023-08-27 13:05:36 -04:00
|
|
|
insert_pair(std::pair<K, V>(__k, V()));
|
|
|
|
__i = lower_bound(__k);
|
2023-08-27 12:40:49 -04:00
|
|
|
}
|
|
|
|
return (*__i).second;
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:12:56 -04:00
|
|
|
V& at(K const& __k) {
|
|
|
|
iterator __i = lower_bound(__k);
|
|
|
|
if (__i == end() || compare(__k, (*__i).first)) {
|
|
|
|
throw std::out_of_range("map::at");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*__i).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const V& at(K const& __k) const {
|
|
|
|
iterator __i = lower_bound(__k);
|
|
|
|
if (__i == end() || compare(__k, (*__i).first)) {
|
|
|
|
throw std::out_of_range("map::at");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*__i).second;
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
iterator begin() noexcept {
|
|
|
|
return iterator(m_header.m_left);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() noexcept {
|
|
|
|
return iterator(&m_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const noexcept {
|
|
|
|
return m_nodecount == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const noexcept {
|
|
|
|
return m_nodecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator lower_bound(K const& __x) {
|
2024-03-15 12:09:21 -04:00
|
|
|
_tree_node __j = static_cast<_tree_node>(m_header.m_parent);
|
2023-08-27 12:34:33 -04:00
|
|
|
_tree_node __k = static_cast<_tree_node>(&m_header);
|
|
|
|
while (__j != nullptr) {
|
|
|
|
if (!compare(__j->m_value.first, __x)) {
|
|
|
|
__k = __j;
|
|
|
|
__j = static_cast<_tree_node>(__j->m_left);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
__j = static_cast<_tree_node>(__j->m_right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iterator(__k);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator upper_bound(K const& __x) {
|
2024-03-15 12:09:21 -04:00
|
|
|
_tree_node __j = static_cast<_tree_node>(m_header.m_parent);
|
2023-08-27 12:34:33 -04:00
|
|
|
_tree_node __k = static_cast<_tree_node>(&m_header);
|
|
|
|
while (__j != nullptr) {
|
|
|
|
if (compare(__x, __j->m_value.first)) {
|
|
|
|
__k = __j;
|
|
|
|
__j = static_cast<_tree_node>(__j->m_left);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
__j = static_cast<_tree_node>(__j->m_right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iterator(__k);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator find(K const& __x) {
|
|
|
|
iterator __j = lower_bound(__x);
|
|
|
|
return (__j == end() || compare(__x, (*__j).first)) ? end() : __j;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count(K const& __x) {
|
|
|
|
return find(__x) != end() ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:12:56 -04:00
|
|
|
bool contains(K const& __x) {
|
2024-03-16 15:09:58 -04:00
|
|
|
return count(__x) > 0;
|
2024-03-15 16:12:56 -04:00
|
|
|
}
|
|
|
|
|
2023-08-27 12:34:33 -04:00
|
|
|
map(map const& lol) : map(std::map<K, V>(lol)) {}
|
|
|
|
|
|
|
|
map() : map(std::map<K, V>()) {}
|
|
|
|
|
|
|
|
~map() {
|
|
|
|
erase(static_cast<_tree_node>(m_header.m_parent));
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
};
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
// template <class Type>
|
|
|
|
// using vector = std::vector<Type>;
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename T>
|
|
|
|
class GEODE_DLL vector {
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
2024-06-16 10:45:47 -04:00
|
|
|
using iterator = T*;
|
|
|
|
using const_iterator = const T*;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
auto allocator() const {
|
2024-01-13 08:43:53 -05:00
|
|
|
return gd::allocator<T>();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
operator std::vector<T>() const {
|
|
|
|
return std::vector<T>(m_start, m_finish);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector() {
|
|
|
|
m_start = nullptr;
|
|
|
|
m_finish = nullptr;
|
|
|
|
m_reserveEnd = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-06-16 10:45:47 -04:00
|
|
|
std::size_t nextCapacity(std::size_t x) {
|
2024-02-10 06:42:45 -05:00
|
|
|
// minimum 16, powers of 2, don't use builtins
|
|
|
|
if (x < 16) return 16;
|
2024-06-16 10:45:47 -04:00
|
|
|
std::size_t out = 16;
|
2024-02-10 06:42:45 -05:00
|
|
|
while (out < x) {
|
|
|
|
out *= 2;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector(std::vector<T> const& input) : vector() {
|
|
|
|
if (input.size()) {
|
2024-02-10 06:42:45 -05:00
|
|
|
auto capacity = nextCapacity(input.size());
|
|
|
|
m_start = this->allocator().allocate(capacity);
|
2022-12-03 06:49:52 -05:00
|
|
|
m_finish = m_start + input.size();
|
2024-02-10 06:42:45 -05:00
|
|
|
m_reserveEnd = m_start + capacity;
|
2024-01-13 09:01:50 -05:00
|
|
|
|
|
|
|
std::uninitialized_default_construct(m_start, m_finish);
|
|
|
|
std::copy(input.begin(), input.end(), m_start);
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector(gd::vector<T> const& input) : vector() {
|
|
|
|
if (input.size()) {
|
2024-02-10 06:42:45 -05:00
|
|
|
auto capacity = nextCapacity(input.size());
|
|
|
|
m_start = this->allocator().allocate(capacity);
|
2022-12-03 06:49:52 -05:00
|
|
|
m_finish = m_start + input.size();
|
2024-02-10 06:42:45 -05:00
|
|
|
m_reserveEnd = m_start + capacity;
|
2024-01-13 09:01:50 -05:00
|
|
|
|
|
|
|
std::uninitialized_default_construct(m_start, m_finish);
|
|
|
|
std::copy(input.begin(), input.end(), m_start);
|
2022-12-03 06:49:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector(gd::vector<T>&& input) : vector() {
|
|
|
|
m_start = input.m_start;
|
|
|
|
m_finish = input.m_finish;
|
|
|
|
m_reserveEnd = input.m_reserveEnd;
|
|
|
|
|
|
|
|
input.m_start = nullptr;
|
|
|
|
input.m_finish = nullptr;
|
|
|
|
input.m_reserveEnd = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector& operator=(gd::vector<T> const& input) {
|
|
|
|
this->clear();
|
2022-12-02 10:40:51 -05:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
if (input.size()) {
|
2024-02-10 06:42:45 -05:00
|
|
|
auto capacity = nextCapacity(input.size());
|
|
|
|
m_start = this->allocator().allocate(capacity);
|
2022-12-03 06:49:52 -05:00
|
|
|
m_finish = m_start + input.size();
|
2024-02-10 06:42:45 -05:00
|
|
|
m_reserveEnd = m_start + capacity;
|
2022-12-03 06:49:52 -05:00
|
|
|
|
2024-02-10 06:42:45 -05:00
|
|
|
std::uninitialized_default_construct(m_start, m_finish);
|
2022-12-03 06:49:52 -05:00
|
|
|
std::copy(input.begin(), input.end(), m_start);
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2022-12-03 06:49:52 -05:00
|
|
|
|
|
|
|
return *this;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector& operator=(gd::vector<T>&& input) {
|
|
|
|
m_start = input.m_start;
|
|
|
|
m_finish = input.m_finish;
|
|
|
|
m_reserveEnd = input.m_reserveEnd;
|
2022-12-02 10:40:51 -05:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
input.m_start = nullptr;
|
|
|
|
input.m_finish = nullptr;
|
|
|
|
input.m_reserveEnd = nullptr;
|
|
|
|
|
|
|
|
return *this;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2022-12-03 06:49:52 -05:00
|
|
|
|
2024-02-10 06:42:45 -05:00
|
|
|
void grow() {
|
|
|
|
if (m_finish == m_reserveEnd) {
|
|
|
|
auto newSize = this->capacity() * 2;
|
|
|
|
auto newStart = this->allocator().allocate(newSize);
|
|
|
|
auto newFinish = newStart + this->size();
|
|
|
|
|
|
|
|
std::uninitialized_default_construct(newStart, newFinish);
|
|
|
|
std::copy(m_start, m_finish, newStart);
|
|
|
|
|
|
|
|
std::destroy(m_start, m_finish);
|
|
|
|
this->allocator().deallocate(m_start, this->capacity());
|
|
|
|
|
|
|
|
m_start = newStart;
|
|
|
|
m_finish = newFinish;
|
|
|
|
m_reserveEnd = newStart + newSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shrink() {
|
|
|
|
if (m_finish < m_reserveEnd / 2 && this->capacity() > 16) {
|
|
|
|
auto newSize = this->capacity() / 2;
|
|
|
|
auto newStart = this->allocator().allocate(newSize);
|
|
|
|
auto newFinish = newStart + this->size();
|
|
|
|
|
|
|
|
std::uninitialized_default_construct(newStart, newFinish);
|
|
|
|
std::copy(m_start, m_finish, newStart);
|
|
|
|
|
|
|
|
std::destroy(m_start, m_finish);
|
|
|
|
this->allocator().deallocate(m_start, this->capacity());
|
|
|
|
|
|
|
|
m_start = newStart;
|
|
|
|
m_finish = newFinish;
|
|
|
|
m_reserveEnd = newStart + newSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 10:45:47 -04:00
|
|
|
iterator erase(iterator pos)
|
|
|
|
{
|
|
|
|
return erase(pos, pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator erase(const_iterator pos)
|
|
|
|
{
|
|
|
|
return erase(pos, pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator erase(iterator first, iterator last)
|
|
|
|
{
|
|
|
|
std::move(last, m_finish, first);
|
|
|
|
|
|
|
|
--m_finish;
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator erase(const_iterator first, const_iterator last)
|
|
|
|
{
|
|
|
|
std::move(last, m_finish, first);
|
|
|
|
|
|
|
|
--m_finish;
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2024-02-10 06:42:45 -05:00
|
|
|
void push_back(T const& input) {
|
|
|
|
this->grow();
|
|
|
|
|
|
|
|
new (m_finish) T(input);
|
|
|
|
++m_finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(T&& input) {
|
|
|
|
this->grow();
|
|
|
|
|
|
|
|
new (m_finish) T(std::move(input));
|
|
|
|
++m_finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
if (m_finish != m_start) {
|
|
|
|
--m_finish;
|
|
|
|
m_finish->~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->shrink();
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector(std::initializer_list<T> const& input) : vector() {
|
|
|
|
if (input.size()) {
|
2024-02-10 06:42:45 -05:00
|
|
|
auto capacity = nextCapacity(input.size());
|
|
|
|
m_start = this->allocator().allocate(capacity);
|
2022-12-03 06:49:52 -05:00
|
|
|
m_finish = m_start + input.size();
|
2024-02-10 06:42:45 -05:00
|
|
|
m_reserveEnd = m_start + capacity;
|
2022-12-03 06:49:52 -05:00
|
|
|
|
2024-01-13 09:01:50 -05:00
|
|
|
std::uninitialized_default_construct(m_start, m_finish);
|
|
|
|
std::copy(input.begin(), input.end(), m_start);
|
2022-12-03 06:49:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 17:30:17 -05:00
|
|
|
void clear() {
|
2022-12-03 06:49:52 -05:00
|
|
|
if (m_start) {
|
|
|
|
std::destroy(m_start, m_finish);
|
2022-12-02 10:40:51 -05:00
|
|
|
|
2024-02-10 06:42:45 -05:00
|
|
|
this->allocator().deallocate(m_start, this->capacity());
|
2022-12-03 06:49:52 -05:00
|
|
|
}
|
2022-12-02 10:40:51 -05:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
m_start = nullptr;
|
|
|
|
m_finish = nullptr;
|
|
|
|
m_reserveEnd = nullptr;
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](size_t index) {
|
|
|
|
return m_start[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
T const& operator[](size_t index) const {
|
|
|
|
return m_start[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
T& at(size_t index) {
|
|
|
|
if (index >= this->size()) {
|
|
|
|
throw std::out_of_range("gd::vector::at");
|
|
|
|
}
|
|
|
|
return m_start[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
T const& at(size_t index) const {
|
|
|
|
if (index >= this->size()) {
|
|
|
|
throw std::out_of_range("gd::vector::at");
|
|
|
|
}
|
|
|
|
return m_start[index];
|
2022-11-24 17:30:17 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
T& front() {
|
|
|
|
return *m_start;
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T* begin() {
|
2022-10-30 14:59:20 -04:00
|
|
|
return m_start;
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T* end() {
|
2022-10-30 14:59:20 -04:00
|
|
|
return m_finish;
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T const* begin() const {
|
|
|
|
return m_start;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T const* end() const {
|
|
|
|
return m_finish;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~vector() {
|
2023-10-11 14:23:13 -04:00
|
|
|
if (m_start) {
|
|
|
|
for (auto& x : *this) {
|
|
|
|
x.~T();
|
|
|
|
}
|
|
|
|
delete m_start;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
size_t size() const {
|
|
|
|
return m_finish - m_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t capacity() const {
|
|
|
|
return m_reserveEnd - m_start;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
protected:
|
|
|
|
T* m_start;
|
|
|
|
T* m_finish;
|
2022-12-02 10:40:51 -05:00
|
|
|
T* m_reserveEnd;
|
2022-10-30 14:59:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _bit_reference {
|
|
|
|
uintptr_t* m_bitptr;
|
|
|
|
uintptr_t m_mask;
|
|
|
|
|
|
|
|
_bit_reference(uintptr_t* x, uintptr_t y) : m_bitptr(x), m_mask(y) {}
|
|
|
|
|
|
|
|
_bit_reference() : m_bitptr(0), m_mask(0) {}
|
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
return !!(*m_bitptr & m_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
_bit_reference& operator=(bool x) {
|
|
|
|
if (x) *m_bitptr |= m_mask;
|
|
|
|
else *m_bitptr &= ~m_mask;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bit_reference& operator=(_bit_reference const& x) {
|
|
|
|
return *this = bool(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(_bit_reference const& x) const {
|
|
|
|
return bool(*this) == bool(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(_bit_reference const& x) const {
|
|
|
|
return !bool(*this) && bool(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flip() {
|
|
|
|
*m_bitptr ^= m_mask;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _bit_iterator {
|
|
|
|
uintptr_t* m_bitptr;
|
|
|
|
unsigned int m_offset;
|
|
|
|
|
|
|
|
_bit_iterator(uintptr_t* x) : m_bitptr(x), m_offset(0) {}
|
|
|
|
|
|
|
|
_bit_iterator(uintptr_t* x, unsigned o) : m_bitptr(x), m_offset(o) {}
|
|
|
|
|
|
|
|
_bit_reference operator*() const {
|
|
|
|
return _bit_reference(m_bitptr, 1UL << m_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
_bit_iterator& operator++() {
|
|
|
|
if (m_offset++ == sizeof(uintptr_t) - 1) {
|
|
|
|
m_offset = 0;
|
|
|
|
m_bitptr++;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(_bit_iterator const& b) {
|
|
|
|
return !(m_bitptr == b.m_bitptr && m_offset == b.m_offset);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class vector<bool> {
|
|
|
|
protected:
|
|
|
|
_bit_iterator m_start;
|
|
|
|
_bit_iterator m_end;
|
|
|
|
uintptr_t* m_capacity_end;
|
|
|
|
|
|
|
|
public:
|
2022-12-03 06:49:52 -05:00
|
|
|
auto allocator() const {
|
2024-01-13 08:43:53 -05:00
|
|
|
return gd::allocator<uintptr_t>();
|
2022-12-03 06:49:52 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
vector() : m_start(nullptr), m_end(nullptr), m_capacity_end(nullptr) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
// vector(std::vector<bool> input) : vector() {
|
|
|
|
// auto realsize = input.size() / int(sizeof(uintptr_t));
|
|
|
|
// auto start = this->allocator().allocate(realsize);
|
|
|
|
|
|
|
|
// m_start = _bit_iterator(start);
|
|
|
|
// m_end = _bit_iterator(start + realsize, input.size() % sizeof(uintptr_t));
|
|
|
|
// m_capacity_end = start + realsize;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
// auto itmp = m_start;
|
|
|
|
// for (auto i : input) {
|
|
|
|
// *itmp = i;
|
|
|
|
// ++itmp;
|
|
|
|
// }
|
|
|
|
// }
|
2022-11-27 13:16:11 -05:00
|
|
|
|
2022-12-03 06:49:52 -05:00
|
|
|
// vector(vector<bool> const& input) : vector() {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// vector() : vector(std::vector<bool>()) {}
|
2022-11-27 13:16:11 -05:00
|
|
|
|
|
|
|
~vector() {
|
|
|
|
delete[] m_start.m_bitptr;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
operator std::vector<bool>() const {
|
|
|
|
std::vector<bool> out;
|
|
|
|
for (auto i = m_start; i != m_end; ++i) {
|
|
|
|
out.push_back(*i);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-11-27 13:16:11 -05:00
|
|
|
_bit_reference operator[](size_t index) {
|
2022-12-03 06:49:52 -05:00
|
|
|
auto const real_index = index / sizeof(uintptr_t);
|
|
|
|
auto const offset = index % sizeof(uintptr_t);
|
2022-11-27 13:16:11 -05:00
|
|
|
return _bit_reference(&m_start.m_bitptr[real_index], 1UL << offset);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-27 13:16:11 -05:00
|
|
|
bool operator[](size_t index) const {
|
|
|
|
return const_cast<vector&>(*this)[index];
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2024-06-03 00:24:18 -04:00
|
|
|
|
|
|
|
_bit_reference at(size_t index) {
|
|
|
|
// TODO: bounds checking
|
|
|
|
return this->operator[](index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool at(size_t index) const {
|
|
|
|
// TODO: bounds checking
|
|
|
|
return this->operator[](index);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
};
|
2023-12-21 09:13:39 -05:00
|
|
|
|
|
|
|
// 2.2 TODO: Implement set, unordered_map and unordered_set
|
2024-01-17 09:49:16 -05:00
|
|
|
|
|
|
|
// the sizes of these are always the same, no matter the type
|
2023-12-21 09:13:39 -05:00
|
|
|
template <class V>
|
2024-01-17 09:49:16 -05:00
|
|
|
using set = void*[6];
|
2023-12-21 09:13:39 -05:00
|
|
|
|
2024-09-16 08:52:54 -04:00
|
|
|
template <class Key, class Tp, class Hash = geode::stl::hash<Key>, class Pred = geode::stl::equal_to<Key>, class Alloc = std::allocator<std::pair<const Key, Tp>>>
|
|
|
|
using unordered_map = geode::stl::unordered_map<Key, Tp, Hash, Pred, Alloc>;
|
2023-12-21 09:13:39 -05:00
|
|
|
|
2024-09-16 08:52:54 -04:00
|
|
|
template <class Value, class Hash = geode::stl::hash<Value>, class Pred = geode::stl::equal_to<Value>, class Alloc = std::allocator<Value>>
|
|
|
|
using unordered_set = geode::stl::unordered_set<Value, Hash, Pred, Alloc>;
|
2022-10-30 14:24:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#elif defined(GEODE_IS_IOS)
|
|
|
|
namespace gd {
|
2022-10-30 14:59:20 -04:00
|
|
|
class GEODE_DLL string {
|
|
|
|
public:
|
|
|
|
string() {}
|
|
|
|
|
|
|
|
string(char const* ok) : m_internal(ok) {}
|
|
|
|
|
|
|
|
string(std::string ok) : m_internal(ok) {}
|
|
|
|
|
|
|
|
operator std::string() {
|
|
|
|
return m_internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::string() const {
|
|
|
|
return m_internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
string(string const& ok) : m_internal(ok) {}
|
|
|
|
|
|
|
|
string& operator=(char const* ok) {
|
|
|
|
m_internal = ok;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
string& operator=(string const& ok) {
|
|
|
|
m_internal = ok;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~string() {}
|
|
|
|
|
|
|
|
char const* c_str() const {
|
|
|
|
return m_internal.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::string m_internal;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class GEODE_DLL vector {
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
operator std::vector<T>() {
|
|
|
|
return m_internal;
|
|
|
|
}
|
2022-12-03 06:49:52 -05:00
|
|
|
|
2022-11-24 16:57:11 -05:00
|
|
|
void clear() {
|
|
|
|
m_internal.clear();
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
operator std::vector<T>() const {
|
|
|
|
return m_internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector(std::vector<T> input) : m_internal(input) {}
|
|
|
|
|
|
|
|
T& front() {
|
|
|
|
return m_internal.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
vector(vector const& lol) : m_internal(lol) {}
|
|
|
|
|
|
|
|
vector() : m_internal() {}
|
|
|
|
|
|
|
|
~vector() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<T> m_internal;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
|
|
|
class GEODE_DLL map {
|
|
|
|
protected:
|
|
|
|
std::map<K, V> m_internal;
|
|
|
|
|
|
|
|
public:
|
|
|
|
operator std::map<K, V>() {
|
|
|
|
return m_internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::map<K, V>() const {
|
|
|
|
return m_internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
map(std::map<K, V> input) : m_internal(input) {}
|
|
|
|
|
|
|
|
map(map const& lol) : m_internal(lol) {}
|
|
|
|
|
|
|
|
map() {}
|
|
|
|
|
|
|
|
~map() {}
|
|
|
|
};
|
2022-10-30 14:24:06 -04:00
|
|
|
}
|
|
|
|
#endif
|