PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/ruby/std_set.i

#
Swig | 212 lines | 160 code | 40 blank | 12 comment | 0 complexity | 5cd736e3fff57519f4ce82d530e00bb3 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /*
  2. Sets
  3. */
  4. %fragment("StdSetTraits","header",fragment="<stddef.h>",fragment="StdSequenceTraits")
  5. %{
  6. namespace swig {
  7. template <class RubySeq, class T>
  8. inline void
  9. assign(const RubySeq& rubyseq, std::set<T>* seq) {
  10. // seq->insert(rubyseq.begin(), rubyseq.end()); // not used as not always implemented
  11. typedef typename RubySeq::value_type value_type;
  12. typename RubySeq::const_iterator it = rubyseq.begin();
  13. for (;it != rubyseq.end(); ++it) {
  14. seq->insert(seq->end(),(value_type)(*it));
  15. }
  16. }
  17. template <class T>
  18. struct traits_asptr<std::set<T> > {
  19. static int asptr(VALUE obj, std::set<T> **s) {
  20. return traits_asptr_stdseq<std::set<T> >::asptr(obj, s);
  21. }
  22. };
  23. template <class T>
  24. struct traits_from<std::set<T> > {
  25. static VALUE from(const std::set<T>& vec) {
  26. return traits_from_stdseq<std::set<T> >::from(vec);
  27. }
  28. };
  29. /**
  30. * Set Iterator class for an iterator with no end() boundaries.
  31. *
  32. */
  33. template<typename InOutIterator,
  34. typename ValueType = typename std::iterator_traits<InOutIterator>::value_type,
  35. typename FromOper = from_oper<ValueType>,
  36. typename AsvalOper = asval_oper<ValueType> >
  37. class SetIteratorOpen_T : public Iterator_T<InOutIterator>
  38. {
  39. public:
  40. FromOper from;
  41. AsvalOper asval;
  42. typedef InOutIterator nonconst_iter;
  43. typedef ValueType value_type;
  44. typedef Iterator_T<nonconst_iter> base;
  45. typedef SetIteratorOpen_T<InOutIterator, ValueType, FromOper, AsvalOper> self_type;
  46. public:
  47. SetIteratorOpen_T(nonconst_iter curr, VALUE seq = Qnil)
  48. : Iterator_T<InOutIterator>(curr, seq)
  49. {
  50. }
  51. virtual VALUE value() const {
  52. return from(static_cast<const value_type&>(*(base::current)));
  53. }
  54. // no setValue allowed
  55. Iterator *dup() const
  56. {
  57. return new self_type(*this);
  58. }
  59. };
  60. /**
  61. * Set Iterator class for a iterator where begin() and end() boundaries
  62. are known.
  63. *
  64. */
  65. template<typename InOutIterator,
  66. typename ValueType = typename std::iterator_traits<InOutIterator>::value_type,
  67. typename FromOper = from_oper<ValueType>,
  68. typename AsvalOper = asval_oper<ValueType> >
  69. class SetIteratorClosed_T : public Iterator_T<InOutIterator>
  70. {
  71. public:
  72. FromOper from;
  73. AsvalOper asval;
  74. typedef InOutIterator nonconst_iter;
  75. typedef ValueType value_type;
  76. typedef Iterator_T<nonconst_iter> base;
  77. typedef SetIteratorClosed_T<InOutIterator, ValueType, FromOper, AsvalOper> self_type;
  78. protected:
  79. virtual Iterator* advance(ptrdiff_t n)
  80. {
  81. std::advance( base::current, n );
  82. if ( base::current == end )
  83. throw stop_iteration();
  84. return this;
  85. }
  86. public:
  87. SetIteratorClosed_T(nonconst_iter curr, nonconst_iter first,
  88. nonconst_iter last, VALUE seq = Qnil)
  89. : Iterator_T<InOutIterator>(curr, seq), begin(first), end(last)
  90. {
  91. }
  92. virtual VALUE value() const {
  93. if (base::current == end) {
  94. throw stop_iteration();
  95. } else {
  96. return from(static_cast<const value_type&>(*(base::current)));
  97. }
  98. }
  99. // no setValue allowed
  100. Iterator *dup() const
  101. {
  102. return new self_type(*this);
  103. }
  104. private:
  105. nonconst_iter begin;
  106. nonconst_iter end;
  107. };
  108. // Template specialization to construct a closed iterator for sets
  109. // this turns a nonconst iterator into a const one for ruby to avoid
  110. // allowing the user to change the value
  111. template< typename InOutIter >
  112. inline Iterator*
  113. make_set_nonconst_iterator(const InOutIter& current,
  114. const InOutIter& begin,
  115. const InOutIter& end,
  116. VALUE seq = Qnil)
  117. {
  118. return new SetIteratorClosed_T< InOutIter >(current,
  119. begin, end, seq);
  120. }
  121. // Template specialization to construct an open iterator for sets
  122. // this turns a nonconst iterator into a const one for ruby to avoid
  123. // allowing the user to change the value
  124. template< typename InOutIter >
  125. inline Iterator*
  126. make_set_nonconst_iterator(const InOutIter& current,
  127. VALUE seq = Qnil)
  128. {
  129. return new SetIteratorOpen_T< InOutIter >(current, seq);
  130. }
  131. }
  132. %}
  133. %define %swig_set_methods(set...)
  134. %swig_sequence_methods_common(%arg(set));
  135. %fragment("RubyPairBoolOutputIterator","header",fragment=SWIG_From_frag(bool),fragment="RubySequence_Cont") {}
  136. // Redefine std::set iterator/reverse_iterator typemap
  137. %typemap(out,noblock=1) iterator, reverse_iterator {
  138. $result = SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,const $type &),
  139. self),
  140. swig::Iterator::descriptor(),SWIG_POINTER_OWN);
  141. }
  142. // Redefine std::set std::pair<iterator, bool> typemap
  143. %typemap(out,noblock=1,fragment="RubyPairBoolOutputIterator")
  144. std::pair<iterator, bool> {
  145. $result = rb_ary_new2(2);
  146. rb_ary_push($result, SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,$type &).first),
  147. swig::Iterator::descriptor(),SWIG_POINTER_OWN));
  148. rb_ary_push($result, SWIG_From(bool)(%static_cast($1,const $type &).second));
  149. }
  150. %extend {
  151. %alias push "<<";
  152. value_type push(const value_type& x) {
  153. self->insert(x);
  154. return x;
  155. }
  156. bool __contains__(const value_type& x) {
  157. return self->find(x) != self->end();
  158. }
  159. value_type __getitem__(difference_type i) const throw (std::out_of_range) {
  160. return *(swig::cgetpos(self, i));
  161. }
  162. };
  163. %enddef
  164. %mixin std::set "Enumerable";
  165. %rename("delete") std::set::__delete__;
  166. %rename("reject!") std::set::reject_bang;
  167. %rename("map!") std::set::map_bang;
  168. %rename("empty?") std::set::empty;
  169. %rename("include?" ) std::set::__contains__ const;
  170. %rename("has_key?" ) std::set::has_key const;
  171. %alias std::set::push "<<";
  172. %include <std/std_set.i>