-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_vector.d
145 lines (122 loc) · 4.81 KB
/
std_vector.d
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import std_allocator;
///////////////////////////////////////////////////////////////////////////////
// std::vector declaration.
//
// Current caveats :
// - manual name mangling (=> only vector!int is functionnal)
// See https://issues.dlang.org/show_bug.cgi?id=14086.
// - won't work with custom allocators.
// - missing noexcept
// - iterators are implemented as pointers
// - no reverse_iterator nor rbegin/rend
///////////////////////////////////////////////////////////////////////////////
extern(C++, std) {
final class vector(T, ALLOC = allocator!T) {
alias value_type = T;
alias allocator_type = ALLOC;
alias reference = ref T;
alias const_reference = ref const(T);
alias pointer = T*;
alias const_pointer = const(T*);
alias iterator = pointer;
alias const_iterator = const_pointer;
// alias reverse_iterator
// alias const_reverse_iterator
alias difference_type = ptrdiff_t;
alias size_type = size_t;
final {
// Ctor/dtor
pragma(mangle, "_ZNSt6vectorIiSaIiEEC1Ev") this();
pragma(mangle, "_ZNSt6vectorIiSaIiEEC2EmRKS0_") this(size_type n, ref const allocator_type _ = defaultAlloc);
pragma(mangle, "_ZNSt6vectorIiSaIiEEC2EmRKiRKS0_") this(size_type n, ref const value_type val, ref const allocator_type _ = defaultAlloc);
pragma(mangle, "_ZNSt6vectorIiSaIiEEC2ERKS1_") this(ref const vector x);
pragma(mangle, "_ZNSt6vectorIiSaIiEED1Ev") ~this();
// pragma(mangle, "_ZNSt6vectorIiSaIiEEaSERKS1_") ref vector opAssign(ref const vector s);
// Iterator
pragma(mangle, "_ZNSt6vectorIiSaIiEE5beginEv") iterator begin();
pragma(mangle, "_ZNKSt6vectorIiSaIiEE5beginEv") const_iterator begin() const;
pragma(mangle, "_ZNKSt6vectorIiSaIiEE6cbeginEv") const_iterator cbegin() const;
pragma(mangle, "_ZNSt6vectorIiSaIiEE3endEv") iterator end();
pragma(mangle, "_ZNKSt6vectorIiSaIiEE3endEv") const_iterator end() const;
pragma(mangle, "_ZNKSt6vectorIiSaIiEE4cendEv") const_iterator cend() const;
// no reverse iterator for now.
// Capacity
pragma(mangle, "_ZNKSt6vectorIiSaIiEE4sizeEv") size_t size() const;
pragma(mangle, "_ZNKSt6vectorIiSaIiEE8max_sizeEv") size_t max_size() const;
pragma(mangle, "_ZNKSt6vectorIiSaIiEE8capacityEv") size_t capacity() const;
pragma(mangle, "_ZNKSt6vectorIiSaIiEE5emptyEv") bool empty() const;
pragma(mangle, "_ZNSt6vectorIiSaIiEE5clearEv") void clear();
pragma(mangle, "_ZNSt6vectorIiSaIiEE6resizeEm") void resize(size_t n);
pragma(mangle, "_ZNSt6vectorIiSaIiEE6resizeEmRKi") void resize(size_t n, T c);
pragma(mangle, "_ZNSt6vectorIiSaIiEE7reserveEm") void reserve(size_t n = 0);
pragma(mangle, "_ZNSt6vectorIiSaIiEE13shrink_to_fitEv") void shrink_to_fit();
// Element access
pragma(mangle, "_ZNSt6vectorIiSaIiEEixEm") ref T opIndex(size_t i);
pragma(mangle, "_ZNKSt6vectorIiSaIiEEixEm") ref const(T) opIndex(size_t i) const;
pragma(mangle, "_ZNSt6vectorIiSaIiEE2atEm") ref T at(size_t i);
pragma(mangle, "_ZNKSt6vectorIiSaIiEE2atEm") ref const(T) at(size_t i) const;
pragma(mangle, "_ZNSt6vectorIiSaIiEE4backEv") ref T back();
pragma(mangle, "_ZNKSt6vectorIiSaIiEE4backEv") ref const(T) back() const;
pragma(mangle, "_ZNSt6vectorIiSaIiEE5frontEv") ref T front();
pragma(mangle, "_ZNKSt6vectorIiSaIiEE5frontEv") ref const(T) front() const;
// Modifier
pragma(mangle, "_ZNSt6vectorIiSaIiEE9push_backEOi") void push_back(ref const T _);
void push_back(const T _) { push_back(_);} // forwards to ref version
pragma(mangle, "_ZNSt6vectorIiSaIiEE8pop_backEv") void pop_back();
}
private:
void[24] _ = void; // to match sizeof(std::vector) and pad the object correctly.
__gshared static immutable allocator!T defaultAlloc;
}
}
///////////////////////////////////////////////////////////////////////////////
// Tests
///////////////////////////////////////////////////////////////////////////////
// unittest {
// auto s = new vector!int(0);
//
// assert(s.begin == s.end);
// assert(s.cbegin == s.cend);
// }
//
// unittest {
// const s = new vector!int(0);
//
// assert(s.begin == s.end);
// assert(s.cbegin == s.cend);
// }
//
// unittest {
// auto s = new vector!int(0);
//
// assert(s.empty);
// s.push_back(10);
// assert(!s.empty);
// assert(s.begin[0] == 10);
// assert(s[0] == 10);
// assert(s.at(0) == 10);
//
// s[0] = 1;
// assert(s[0] == 1);
//
// s.at(0) = 2;
// assert(s[0] == 2);
//
// assert(s.front == 2);
// assert(s.back == 2);
//
// s.pop_back();
// assert(s.empty);
// assert(s.cbegin == s.cend);
// }
//
//
// unittest {
// auto s = new vector!int(0);
//
// s.resize(5);
// assert(s.capacity >= 5);
// assert(s.size == 5);
// assert(!s.empty);
// assert(s.begin[0] == 0);
// }