-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.hpp
More file actions
71 lines (71 loc) · 2.57 KB
/
array.hpp
File metadata and controls
71 lines (71 loc) · 2.57 KB
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
#pragma once
#include<cstdint>
#include<utility>
#include<ranges>
#include<span>
namespace cppp{
constexpr inline struct value_init_tag_t{} value_init_tag;
template<typename T>
class fixed_array{
std::span<T> buf;
public:
template<std::ranges::sized_range R>
fixed_array(R&& ran) : fixed_array(std::ranges::size(ran)){
std::ranges::copy(ran,begin());
}
// special initializer_list ctor to prevent {element} from matching the size_t ctor
fixed_array(std::initializer_list<T> il) : fixed_array(std::ranges::size(il)){
std::ranges::copy(il,begin());
}
// don't take a std::span or we may conflict with the above
fixed_array(T* m,std::size_t n) : buf(m,n){}
fixed_array(std::size_t sz) : buf(new T[sz],sz){}
fixed_array(std::size_t sz,value_init_tag_t) : buf(new T[sz](),sz){}
template<typename ...C>
fixed_array(std::size_t sz,std::in_place_t,C&& ...t) : buf(new T[sz](std::forward<T>(t)...),sz){}
fixed_array(const fixed_array&) = delete;
fixed_array(fixed_array&& other) noexcept : buf(std::exchange(other.buf,std::span<T>())){}
fixed_array& operator=(const fixed_array&) = delete;
fixed_array& operator=(fixed_array&& other) noexcept{
buf = std::exchange(other.buf,std::span<T>());
return *this;
}
T& operator[](std::size_t i) noexcept{
return buf[i];
}
const T& operator[](std::size_t i) const noexcept{
return buf[i];
}
T* data() noexcept{
return buf.data();
}
const T* data() const noexcept{
return buf.data();
}
std::size_t size() const noexcept{
return buf.size();
}
T* release() noexcept{
T* m = data();
buf = {};
return m;
}
using iterator = T*;
using const_iterator = const T*;
T* begin() noexcept{
return buf.data();
}
const T* begin() const noexcept{
return buf.data();
}
T* end() noexcept{
return buf.data()+buf.size();
}
const T* end() const noexcept{
return buf.data()+buf.size();
}
~fixed_array(){
delete[] buf.data();
}
};
}