| 1 | /* This file contains a generic definition of std::deque along with |
| 2 | * some helper functions. Specific language modules should include |
| 3 | * this file to generate wrappers. |
| 4 | */ |
| 5 | |
| 6 | %include <std_common.i> |
| 7 | |
| 8 | %{ |
| 9 | #include <deque> |
| 10 | #include <stdexcept> |
| 11 | %} |
| 12 | |
| 13 | |
| 14 | /* This macro defines all of the standard methods for a deque. This |
| 15 | is defined as a macro to simplify the task of specialization. For |
| 16 | example, |
| 17 | |
| 18 | template<> class deque<int> { |
| 19 | public: |
| 20 | %std_deque_methods(int); |
| 21 | }; |
| 22 | */ |
| 23 | |
| 24 | %define %std_deque_methods(T) |
| 25 | typedef T &reference; |
| 26 | typedef const T& const_reference; |
| 27 | |
| 28 | deque(); |
| 29 | deque(unsigned int size, const T& value=T()); |
| 30 | deque(const deque<T> &); |
| 31 | ~deque(); |
| 32 | |
| 33 | void assign(unsigned int n, const T& value); |
| 34 | void swap(deque<T> &x); |
| 35 | unsigned int size() const; |
| 36 | unsigned int max_size() const; |
| 37 | void resize(unsigned int n, T c = T()); |
| 38 | bool empty() const; |
| 39 | const_reference front(); |
| 40 | const_reference back(); |
| 41 | void push_front(const T& x); |
| 42 | void push_back(const T& x); |
| 43 | void pop_front(); |
| 44 | void pop_back(); |
| 45 | void clear(); |
| 46 | |
| 47 | /* Some useful extensions */ |
| 48 | %extend { |
| 49 | const_reference getitem(int i) throw (std::out_of_range) { |
| 50 | int size = int(self->size()); |
| 51 | if (i<0) i += size; |
| 52 | if (i>=0 && i<size) |
| 53 | return (*self)[i]; |
| 54 | else |
| 55 | throw std::out_of_range("deque index out of range"); |
| 56 | } |
| 57 | void setitem(int i, const T& x) throw (std::out_of_range) { |
| 58 | int size = int(self->size()); |
| 59 | if (i<0) i+= size; |
| 60 | if (i>=0 && i<size) |
| 61 | (*self)[i] = x; |
| 62 | else |
| 63 | throw std::out_of_range("deque index out of range"); |
| 64 | } |
| 65 | void delitem(int i) throw (std::out_of_range) { |
| 66 | int size = int(self->size()); |
| 67 | if (i<0) i+= size; |
| 68 | if (i>=0 && i<size) { |
| 69 | self->erase(self->begin()+i); |
| 70 | } else { |
| 71 | throw std::out_of_range("deque index out of range"); |
| 72 | } |
| 73 | } |
| 74 | std::deque<T> getslice(int i, int j) { |
| 75 | int size = int(self->size()); |
| 76 | if (i<0) i = size+i; |
| 77 | if (j<0) j = size+j; |
| 78 | if (i<0) i = 0; |
| 79 | if (j>size) j = size; |
| 80 | std::deque<T > tmp(j-i); |
| 81 | std::copy(self->begin()+i,self->begin()+j,tmp.begin()); |
| 82 | return tmp; |
| 83 | } |
| 84 | void setslice(int i, int j, const std::deque<T>& v) { |
| 85 | int size = int(self->size()); |
| 86 | if (i<0) i = size+i; |
| 87 | if (j<0) j = size+j; |
| 88 | if (i<0) i = 0; |
| 89 | if (j>size) j = size; |
| 90 | if (int(v.size()) == j-i) { |
| 91 | std::copy(v.begin(),v.end(),self->begin()+i); |
| 92 | } else { |
| 93 | self->erase(self->begin()+i,self->begin()+j); |
| 94 | if (i+1 <= size) |
| 95 | self->insert(self->begin()+i+1,v.begin(),v.end()); |
| 96 | else |
| 97 | self->insert(self->end(),v.begin(),v.end()); |
| 98 | } |
| 99 | } |
| 100 | void delslice(int i, int j) { |
| 101 | int size = int(self->size()); |
| 102 | if (i<0) i = size+i; |
| 103 | if (j<0) j = size+j; |
| 104 | if (i<0) i = 0; |
| 105 | if (j>size) j = size; |
| 106 | self->erase(self->begin()+i,self->begin()+j); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | %enddef |
| 111 | |
| 112 | namespace std { |
| 113 | template<class T> class deque { |
| 114 | public: |
| 115 | %std_deque_methods(T); |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |