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 CLOVER_UTIL_COMPAT_HPP
24 #define CLOVER_UTIL_COMPAT_HPP
34 // XXX - For cases where we can't rely on STL... I.e. the
35 // interface between code compiled as C++98 and C++11
36 // source. Get rid of this as soon as everything can be
40 template<typename R, typename S>
42 ranges_equal(const R &a, const S &b) {
43 if (a.size() != b.size())
46 for (size_t i = 0; i < a.size(); ++i)
58 alloc(int n, const T *q, int m) {
59 T *p = reinterpret_cast<T *>(std::malloc(n * sizeof(T)));
61 for (int i = 0; i < m; ++i)
69 for (int i = 0; i < n; ++i)
77 typedef const T *const_iterator;
80 typedef const T &const_reference;
81 typedef std::ptrdiff_t difference_type;
82 typedef std::size_t size_type;
84 vector() : p(NULL), _size(0), _capacity(0) {
87 vector(const vector &v) :
88 p(alloc(v._size, v.p, v._size)),
89 _size(v._size), _capacity(v._size) {
92 vector(const_iterator p, size_type n) :
93 p(alloc(n, p, n)), _size(n), _capacity(n) {
98 p(alloc(v.size(), NULL, 0)), _size(0),
100 for (typename C::const_iterator it = v.begin(); it != v.end(); ++it)
101 new(&p[_size++]) T(*it);
109 operator=(const vector &v) {
112 p = alloc(v._size, v.p, v._size);
120 operator==(const vector &v) const {
121 return detail::ranges_equal(*this, v);
125 reserve(size_type n) {
127 T *q = alloc(n, p, _size);
136 resize(size_type n, T x = T()) {
138 for (size_type i = n; i < _size; ++i)
144 for (size_type i = _size; i < n; ++i)
152 push_back(const T &x) {
189 operator[](size_type i) {
194 operator[](size_type i) const {
208 typedef const T *const_iterator;
209 typedef T value_type;
210 typedef T &reference;
211 typedef const T &const_reference;
212 typedef std::ptrdiff_t difference_type;
213 typedef std::size_t size_type;
215 vector_ref(iterator p, size_type n) : p(p), n(n) {
219 vector_ref(C &v) : p(&*v.begin()), n(v.size()) {
223 operator==(const vector_ref &v) const {
224 return detail::ranges_equal(*this, v);
258 operator[](int i) const {
269 typedef vector_ref<const unsigned char> buffer_t;
276 istream(const buffer_t &buf) : buf(buf), offset(0) {}
279 read(char *p, size_t n) {
280 if (offset + n > buf.size())
283 std::memcpy(p, buf.begin() + offset, n);
294 typedef vector<unsigned char> buffer_t;
296 ostream(buffer_t &buf) : buf(buf), offset(buf.size()) {}
299 write(const char *p, size_t n) {
300 buf.resize(offset + n);
301 std::memcpy(buf.begin() + offset, p, n);