PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Lib/std/_std_deque.i

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