-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathdeque.h
203 lines (176 loc) · 6.55 KB
/
deque.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* MIT License
*
* Copyright (c) 2025 Tyge Løvset
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Deque - double ended queue. Implemented as a ring buffer, extension of queue.
#include "priv/linkage.h"
#include "types.h"
#ifndef STC_DEQUE_H_INCLUDED
#define STC_DEQUE_H_INCLUDED
#include "common.h"
#include <stdlib.h>
#endif // STC_DEQUE_H_INCLUDED
#ifndef _i_prefix
#define _i_prefix deque_
#endif
#define _pop _pop_front
#define _pull _pull_front
#include "priv/template.h"
#include "priv/queue_prv.h"
#undef _pop
#undef _pull
STC_API _m_value* _c_MEMB(_push_front)(Self* self, _m_value value);
STC_API _m_iter _c_MEMB(_insert_n)(Self* self, isize idx, const _m_value* arr, isize n);
STC_API _m_iter _c_MEMB(_insert_uninit)(Self* self, isize idx, isize n);
STC_API void _c_MEMB(_erase_n)(Self* self, isize idx, isize n);
STC_INLINE const _m_value*
_c_MEMB(_at)(const Self* self, isize idx)
{ return self->cbuf + _cbuf_topos(self, idx); }
STC_INLINE _m_value*
_c_MEMB(_at_mut)(Self* self, isize idx)
{ return self->cbuf + _cbuf_topos(self, idx); }
STC_INLINE _m_value*
_c_MEMB(_push_back)(Self* self, _m_value val)
{ return _c_MEMB(_push)(self, val); }
STC_INLINE void
_c_MEMB(_pop_back)(Self* self) {
c_assert(!_c_MEMB(_is_empty)(self));
self->end = (self->end - 1) & self->capmask;
i_keydrop((self->cbuf + self->end));
}
STC_INLINE _m_value _c_MEMB(_pull_back)(Self* self) { // move back out of deque
c_assert(!_c_MEMB(_is_empty)(self));
self->end = (self->end - 1) & self->capmask;
return self->cbuf[self->end];
}
STC_INLINE _m_iter
_c_MEMB(_insert_at)(Self* self, _m_iter it, const _m_value val) {
isize idx = _cbuf_toidx(self, it.pos);
return _c_MEMB(_insert_n)(self, idx, &val, 1);
}
STC_INLINE _m_iter
_c_MEMB(_erase_at)(Self* self, _m_iter it) {
_c_MEMB(_erase_n)(self, _cbuf_toidx(self, it.pos), 1);
if (it.pos == self->end) it.ref = NULL;
return it;
}
STC_INLINE _m_iter
_c_MEMB(_erase_range)(Self* self, _m_iter it1, _m_iter it2) {
isize idx1 = _cbuf_toidx(self, it1.pos);
isize idx2 = _cbuf_toidx(self, it2.pos);
_c_MEMB(_erase_n)(self, idx1, idx2 - idx1);
if (it1.pos == self->end) it1.ref = NULL;
return it1;
}
#if !defined i_no_emplace
STC_API _m_iter
_c_MEMB(_emplace_n)(Self* self, isize idx, const _m_raw* raw, isize n);
STC_INLINE _m_value*
_c_MEMB(_emplace_front)(Self* self, const _m_raw raw)
{ return _c_MEMB(_push_front)(self, i_keyfrom(raw)); }
STC_INLINE _m_value*
_c_MEMB(_emplace_back)(Self* self, const _m_raw raw)
{ return _c_MEMB(_push)(self, i_keyfrom(raw)); }
STC_INLINE _m_iter
_c_MEMB(_emplace_at)(Self* self, _m_iter it, const _m_raw raw)
{ return _c_MEMB(_insert_at)(self, it, i_keyfrom(raw)); }
#endif
#if defined _i_has_eq
STC_API _m_iter _c_MEMB(_find_in)(const Self* self, _m_iter p1, _m_iter p2, _m_raw raw);
STC_INLINE _m_iter
_c_MEMB(_find)(const Self* self, _m_raw raw) {
return _c_MEMB(_find_in)(self, _c_MEMB(_begin)(self), _c_MEMB(_end)(self), raw);
}
#endif // _i_has_eq
#if defined _i_has_cmp
#include "priv/sort_prv.h"
#endif // _i_has_cmp
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined i_implement
STC_DEF _m_value*
_c_MEMB(_push_front)(Self* self, _m_value value) {
isize start = (self->start - 1) & self->capmask;
if (start == self->end) { // full
_c_MEMB(_reserve)(self, self->capmask + 3); // => 2x expand
start = (self->start - 1) & self->capmask;
}
_m_value *v = self->cbuf + start;
self->start = start;
*v = value;
return v;
}
STC_DEF void
_c_MEMB(_erase_n)(Self* self, const isize idx, const isize n) {
const isize len = _c_MEMB(_size)(self);
c_assert(idx + n <= len);
for (isize i = idx + n - 1; i >= idx; --i)
i_keydrop(_c_MEMB(_at_mut)(self, i));
for (isize i = idx, j = i + n; j < len; ++i, ++j)
*_c_MEMB(_at_mut)(self, i) = *_c_MEMB(_at)(self, j);
self->end = (self->end - n) & self->capmask;
}
STC_DEF _m_iter
_c_MEMB(_insert_uninit)(Self* self, const isize idx, const isize n) {
const isize len = _c_MEMB(_size)(self);
_m_iter it = {._s=self};
if (len + n > self->capmask)
if (!_c_MEMB(_reserve)(self, len + n + 3)) // minimum 2x expand
return it;
it.pos = _cbuf_topos(self, idx);
it.ref = self->cbuf + it.pos;
self->end = (self->end + n) & self->capmask;
if (it.pos < self->end) // common case because of reserve policy
c_memmove(it.ref + n, it.ref, (len - idx)*c_sizeof *it.ref);
else for (isize i = len - 1, j = i + n; i >= idx; --i, --j)
*_c_MEMB(_at_mut)(self, j) = *_c_MEMB(_at)(self, i);
return it;
}
STC_DEF _m_iter
_c_MEMB(_insert_n)(Self* self, const isize idx, const _m_value* arr, const isize n) {
_m_iter it = _c_MEMB(_insert_uninit)(self, idx, n);
for (isize i = idx, j = 0; j < n; ++i, ++j)
*_c_MEMB(_at_mut)(self, i) = arr[j];
return it;
}
#if !defined i_no_emplace
STC_DEF _m_iter
_c_MEMB(_emplace_n)(Self* self, const isize idx, const _m_raw* raw, const isize n) {
_m_iter it = _c_MEMB(_insert_uninit)(self, idx, n);
for (isize i = idx, j = 0; j < n; ++i, ++j)
*_c_MEMB(_at_mut)(self, i) = i_keyfrom(raw[j]);
return it;
}
#endif
#if defined _i_has_eq
STC_DEF _m_iter
_c_MEMB(_find_in)(const Self* self, _m_iter i1, _m_iter i2, _m_raw raw) {
(void)self;
for (; i1.pos != i2.pos; _c_MEMB(_next)(&i1)) {
const _m_raw r = i_keytoraw(i1.ref);
if (i_eq((&raw), (&r)))
break;
}
return i1;
}
#endif
#endif // IMPLEMENTATION
#include "priv/linkage2.h"
#include "priv/template2.h"