Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.0 KiB

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