Home | History | Annotate | Download | only in allegrocl
      1 /* -----------------------------------------------------------------------------
      2  * std_list.i
      3  *
      4  * SWIG typemaps for std::list types
      5  *
      6  * To use, add:
      7  *
      8  * %include "std_list.i"
      9  *
     10  * to your interface file. You will also need to include a template directive
     11  * for each instance of the list container you want to use in your application.
     12  * e.g.
     13  *
     14  * %template (intlist) std::list<int>;
     15  * %template (floatlist) std::list<float>;
     16  * ----------------------------------------------------------------------------- */
     17 
     18 %module std_list
     19 %warnfilter(468) std::list;
     20 
     21 %{
     22 #include <list>
     23 #include <stdexcept>
     24 %}
     25 
     26 
     27 namespace std{
     28     template<class T> class list
     29     {
     30     public:
     31 
     32 	typedef T &reference;
     33 	typedef const T& const_reference;
     34 	typedef T &iterator;
     35 	typedef const T& const_iterator;
     36 
     37 	list();
     38 	list(unsigned int size, const T& value = T());
     39 	list(const list<T> &);
     40 
     41 	~list();
     42 	void assign(unsigned int n, const T& value);
     43 	void swap(list<T> &x);
     44 
     45 	const_reference front();
     46 	const_reference back();
     47 	const_iterator begin();
     48 	const_iterator end();
     49 
     50 	void resize(unsigned int n, T c = T());
     51 	bool empty() const;
     52 
     53 	void push_front(const T& INPUT);
     54 	void push_back(const T& INPUT);
     55 
     56 
     57 	void pop_front();
     58 	void pop_back();
     59 	void clear();
     60 	unsigned int size() const;
     61 	unsigned int max_size() const;
     62 	void resize(unsigned int n, const T& INPUT);
     63 
     64 	void remove(const T& INPUT);
     65 	void unique();
     66 	void reverse();
     67 	void sort();
     68 
     69 	%extend
     70 	    {
     71 	        %typemap(lout) T &__getitem__ "(cl::setq ACL_ffresult (ff:fslot-value-typed '$*out_fftype :c $body))";
     72 		%typemap(lout) T *__getitem__ "(cl::setq ACL_ffresult (make-instance '$lclass :foreign-address $body))";
     73 
     74 		const_reference __getitem__(int i) throw (std::out_of_range)
     75 		    {
     76 			std::list<T>::iterator first = self->begin();
     77 			int size = int(self->size());
     78 			if (i<0) i += size;
     79 			if (i>=0 && i<size)
     80 			{
     81 			    for (int k=0;k<i;k++)
     82 			    {
     83 				first++;
     84 			    }
     85 			    return *first;
     86 			}
     87 			else throw std::out_of_range("list index out of range");
     88 		    }
     89 		void __setitem__(int i, const T& INPUT) throw (std::out_of_range)
     90 		    {
     91 			std::list<T>::iterator first = self->begin();
     92 			int size = int(self->size());
     93 			if (i<0) i += size;
     94 			if (i>=0 && i<size)
     95 			{
     96 			    for (int k=0;k<i;k++)
     97 			    {
     98 				first++;
     99 			    }
    100 			    *first = INPUT;
    101 			}
    102 			else throw std::out_of_range("list index out of range");
    103 		    }
    104 		void __delitem__(int i) throw (std::out_of_range)
    105 		    {
    106 			std::list<T>::iterator first = self->begin();
    107 			int size = int(self->size());
    108 			if (i<0) i += size;
    109 			if (i>=0 && i<size)
    110 			{
    111 			    for (int k=0;k<i;k++)
    112 			    {
    113 				first++;
    114 			    }
    115 			    self->erase(first);
    116 			}
    117 			else throw std::out_of_range("list index out of range");
    118 		    }
    119 		std::list<T> __getslice__(int i,int j)
    120 		    {
    121 			std::list<T>::iterator first = self->begin();
    122 			std::list<T>::iterator end = self->end();
    123 
    124 			int size = int(self->size());
    125 			if (i<0) i += size;
    126 			if (j<0) j += size;
    127 			if (i<0) i = 0;
    128 			if (j>size) j = size;
    129 			if (i>=j) i=j;
    130 			if (i>=0 && i<size && j>=0)
    131 			{
    132 			    for (int k=0;k<i;k++)
    133 			    {
    134 				first++;
    135 			    }
    136 			    for (int m=0;m<j;m++)
    137 			    {
    138 				end++;
    139 			    }
    140 			    std::list<T> tmp(j-i);
    141 			    if (j>i) std::copy(first,end,tmp.begin());
    142 			    return tmp;
    143 			}
    144 			else throw std::out_of_range("list index out of range");
    145 		    }
    146 		void __delslice__(int i,int j)
    147 		    {
    148 			std::list<T>::iterator first = self->begin();
    149 			std::list<T>::iterator end = self->end();
    150 
    151 			int size = int(self->size());
    152 			if (i<0) i += size;
    153 			if (j<0) j += size;
    154 			if (i<0) i = 0;
    155 			if (j>size) j = size;
    156 
    157 			for (int k=0;k<i;k++)
    158 			{
    159 			    first++;
    160 			}
    161 			for (int m=0;m<=j;m++)
    162 			{
    163 			    end++;
    164 			}
    165 			self->erase(first,end);
    166 		    }
    167 		void __setslice__(int i,int j, const std::list<T>& v)
    168 		    {
    169 			std::list<T>::iterator first = self->begin();
    170 			std::list<T>::iterator end = self->end();
    171 
    172 			int size = int(self->size());
    173 			if (i<0) i += size;
    174 			if (j<0) j += size;
    175 			if (i<0) i = 0;
    176 			if (j>size) j = size;
    177 
    178 			for (int k=0;k<i;k++)
    179 			{
    180 			    first++;
    181 			}
    182 			for (int m=0;m<=j;m++)
    183 			{
    184 			    end++;
    185 			}
    186 			if (int(v.size()) == j-i)
    187 			{
    188 			    std::copy(v.begin(),v.end(),first);
    189 			}
    190 			else {
    191 			    self->erase(first,end);
    192 			    if (i+1 <= int(self->size()))
    193 			    {
    194 				first = self->begin();
    195 				for (int k=0;k<i;k++)
    196 				{
    197 				    first++;
    198 				}
    199 				self->insert(first,v.begin(),v.end());
    200 			    }
    201 			    else self->insert(self->end(),v.begin(),v.end());
    202 			}
    203 
    204 		    }
    205 		unsigned int __len__()
    206 		    {
    207 			return self->size();
    208 		    }
    209 		bool __nonzero__()
    210 		    {
    211 			return !(self->empty());
    212 		    }
    213 		void append(const T& INPUT)
    214 		    {
    215 			self->push_back(INPUT);
    216 		    }
    217 		void pop()
    218 		    {
    219 			self->pop_back();
    220 		    }
    221 
    222 	    };
    223     };
    224 }
    225 
    226 
    227 
    228 
    229 
    230 
    231