2 // Copyright 2012 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef _CORE_COMPAT_HPP_
24 #define _CORE_COMPAT_HPP_
35 // XXX - For cases where we can't rely on STL... I.e. the
36 // interface between code compiled as C++98 and C++11
37 // source. Get rid of this as soon as everything can be
44 alloc(int n, const T *q, int m) {
45 T *p = reinterpret_cast<T *>(std::malloc(n * sizeof(T)));
47 for (int i = 0; i < m; ++i)
55 for (int i = 0; i < n; ++i)
62 vector() : p(NULL), n(0) {
65 vector(const vector &v) : p(alloc(v.n, v.p, v.n)), n(v.n) {
68 vector(T *p, size_t n) : p(alloc(n, p, n)), n(n) {
73 p(alloc(v.size(), &*v.begin(), v.size())), n(v.size()) {
81 operator=(const vector &v) {
84 p = alloc(v.n, v.p, v.n);
93 T *q = alloc(m, p, n);
102 resize(size_t m, T x = T()) {
107 for (size_t i = n; i < m; ++i)
112 push_back(const T &x) {
149 operator[](int i) const {
161 vector_ref(T *p, size_t n) : p(p), n(n) {
165 vector_ref(C &v) : p(&*v.begin()), n(v.size()) {
199 operator[](int i) const {
210 typedef vector_ref<const unsigned char> buffer_t;
217 istream(const buffer_t &buf) : buf(buf), offset(0) {}
220 read(char *p, size_t n) {
221 if (offset + n > buf.size())
224 std::memcpy(p, buf.begin() + offset, n);
235 typedef vector<unsigned char> buffer_t;
237 ostream(buffer_t &buf) : buf(buf), offset(buf.size()) {}
240 write(const char *p, size_t n) {
241 buf.resize(offset + n);
242 std::memcpy(buf.begin() + offset, p, n);
251 class string : public vector_ref<const char> {
253 string(const char *p) : vector_ref(p, std::strlen(p)) {
257 string(const C &v) : vector_ref(v) {
260 operator std::string() const {
261 return std::string(begin(), end());
265 find(const string &s) const {
266 for (size_t i = 0; i + s.size() < size(); ++i) {
267 if (!std::memcmp(begin() + i, s.begin(), s.size()))
277 operator==(const vector_ref<T> &a, const vector_ref<T> &b) {
278 if (a.size() != b.size())
281 for (size_t i = 0; i < a.size(); ++i)