PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/ruby/std_multimap.i

#
Swig | 227 lines | 200 code | 24 blank | 3 comment | 0 complexity | 14333228382c263b3bc6093805625988 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /*
  2. Multimaps
  3. */
  4. %include <std_map.i>
  5. %fragment("StdMultimapTraits","header",fragment="StdSequenceTraits")
  6. {
  7. namespace swig {
  8. template <class RubySeq, class K, class T >
  9. inline void
  10. assign(const RubySeq& rubyseq, std::multimap<K,T > *multimap) {
  11. typedef typename std::multimap<K,T>::value_type value_type;
  12. typename RubySeq::const_iterator it = rubyseq.begin();
  13. for (;it != rubyseq.end(); ++it) {
  14. multimap->insert(value_type(it->first, it->second));
  15. }
  16. }
  17. template <class K, class T>
  18. struct traits_asptr<std::multimap<K,T> > {
  19. typedef std::multimap<K,T> multimap_type;
  20. static int asptr(VALUE obj, std::multimap<K,T> **val) {
  21. int res = SWIG_ERROR;
  22. if ( TYPE(obj) == T_HASH ) {
  23. static ID id_to_a = rb_intern("to_a");
  24. VALUE items = rb_funcall(obj, id_to_a, 0);
  25. return traits_asptr_stdseq<std::multimap<K,T>, std::pair<K, T> >::asptr(items, val);
  26. } else {
  27. multimap_type *p;
  28. res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<multimap_type>(),0);
  29. if (SWIG_IsOK(res) && val) *val = p;
  30. }
  31. return res;
  32. }
  33. };
  34. template <class K, class T >
  35. struct traits_from<std::multimap<K,T> > {
  36. typedef std::multimap<K,T> multimap_type;
  37. typedef typename multimap_type::const_iterator const_iterator;
  38. typedef typename multimap_type::size_type size_type;
  39. static VALUE from(const multimap_type& multimap) {
  40. swig_type_info *desc = swig::type_info<multimap_type>();
  41. if (desc && desc->clientdata) {
  42. return SWIG_NewPointerObj(new multimap_type(multimap), desc, SWIG_POINTER_OWN);
  43. } else {
  44. size_type size = multimap.size();
  45. int rubysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
  46. if (rubysize < 0) {
  47. SWIG_RUBY_THREAD_BEGIN_BLOCK;
  48. rb_raise(rb_eRuntimeError,
  49. "multimap size not valid in Ruby");
  50. SWIG_RUBY_THREAD_END_BLOCK;
  51. return Qnil;
  52. }
  53. VALUE obj = rb_hash_new();
  54. for (const_iterator i= multimap.begin(); i!= multimap.end(); ++i) {
  55. VALUE key = swig::from(i->first);
  56. VALUE val = swig::from(i->second);
  57. VALUE oldval = rb_hash_aref( obj, key );
  58. if ( oldval == Qnil )
  59. rb_hash_aset(obj, key, val);
  60. else {
  61. // Multiple values for this key, create array if needed
  62. // and add a new element to it.
  63. VALUE ary;
  64. if ( TYPE(oldval) == T_ARRAY )
  65. ary = oldval;
  66. else
  67. {
  68. ary = rb_ary_new2(2);
  69. rb_ary_push( ary, oldval );
  70. rb_hash_aset( obj, key, ary );
  71. }
  72. rb_ary_push( ary, val );
  73. }
  74. }
  75. return obj;
  76. }
  77. }
  78. };
  79. }
  80. }
  81. %define %swig_multimap_methods(MultiMap...)
  82. %swig_map_common(%arg(MultiMap));
  83. %extend {
  84. VALUE __getitem__(const key_type& key) const {
  85. MultiMap::const_iterator i = self->find(key);
  86. if ( i != self->end() )
  87. {
  88. MultiMap::const_iterator e = $self->upper_bound(key);
  89. VALUE ary = rb_ary_new();
  90. for ( ; i != e; ++i )
  91. {
  92. rb_ary_push( ary, swig::from<MultiMap::mapped_type>( i->second ) );
  93. }
  94. if ( RARRAY_LEN(ary) == 1 )
  95. return RARRAY_PTR(ary)[0];
  96. return ary;
  97. }
  98. else
  99. return Qnil;
  100. }
  101. void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
  102. self->insert(MultiMap::value_type(key,x));
  103. }
  104. VALUE inspect()
  105. {
  106. MultiMap::iterator i = $self->begin();
  107. MultiMap::iterator e = $self->end();
  108. const char *type_name = swig::type_name< MultiMap >();
  109. VALUE str = rb_str_new2( type_name );
  110. str = rb_str_cat2( str, " {" );
  111. VALUE tmp;
  112. while ( i != e )
  113. {
  114. const MultiMap::key_type& key = i->first;
  115. const MultiMap::key_type& oldkey = key;
  116. tmp = swig::from( key );
  117. str = rb_str_buf_append( str, rb_inspect(tmp) );
  118. str = rb_str_cat2( str, "=>" );
  119. VALUE vals = rb_ary_new();
  120. for ( ; i != e && key == oldkey; ++i )
  121. {
  122. const MultiMap::mapped_type& val = i->second;
  123. tmp = swig::from( val );
  124. rb_ary_push( vals, tmp );
  125. }
  126. if ( RARRAY_LEN(vals) == 1 )
  127. {
  128. str = rb_str_buf_append( str, rb_inspect(tmp) );
  129. }
  130. else
  131. {
  132. str = rb_str_buf_append( str, rb_inspect(vals) );
  133. }
  134. }
  135. str = rb_str_cat2( str, "}" );
  136. return str;
  137. }
  138. VALUE to_a()
  139. {
  140. MultiMap::const_iterator i = $self->begin();
  141. MultiMap::const_iterator e = $self->end();
  142. VALUE ary = rb_ary_new2( std::distance( i, e ) );
  143. VALUE tmp;
  144. while ( i != e )
  145. {
  146. const MultiMap::key_type& key = i->first;
  147. const MultiMap::key_type& oldkey = key;
  148. tmp = swig::from( key );
  149. rb_ary_push( ary, tmp );
  150. VALUE vals = rb_ary_new();
  151. for ( ; i != e && key == oldkey; ++i )
  152. {
  153. const MultiMap::mapped_type& val = i->second;
  154. tmp = swig::from( val );
  155. rb_ary_push( vals, tmp );
  156. }
  157. if ( RARRAY_LEN(vals) == 1 )
  158. {
  159. rb_ary_push( ary, tmp );
  160. }
  161. else
  162. {
  163. rb_ary_push( ary, vals );
  164. }
  165. }
  166. return ary;
  167. }
  168. VALUE to_s()
  169. {
  170. MultiMap::iterator i = $self->begin();
  171. MultiMap::iterator e = $self->end();
  172. VALUE str = rb_str_new2( "" );
  173. VALUE tmp;
  174. while ( i != e )
  175. {
  176. const MultiMap::key_type& key = i->first;
  177. const MultiMap::key_type& oldkey = key;
  178. tmp = swig::from( key );
  179. tmp = rb_obj_as_string( tmp );
  180. str = rb_str_buf_append( str, tmp );
  181. VALUE vals = rb_ary_new();
  182. for ( ; i != e && key == oldkey; ++i )
  183. {
  184. const MultiMap::mapped_type& val = i->second;
  185. tmp = swig::from( val );
  186. rb_ary_push( vals, tmp );
  187. }
  188. tmp = rb_obj_as_string( vals );
  189. str = rb_str_buf_append( str, tmp );
  190. }
  191. return str;
  192. }
  193. }
  194. %enddef
  195. %mixin std::multimap "Enumerable";
  196. %rename("delete") std::multimap::__delete__;
  197. %rename("reject!") std::multimap::reject_bang;
  198. %rename("map!") std::multimap::map_bang;
  199. %rename("empty?") std::multimap::empty;
  200. %rename("include?" ) std::multimap::__contains__ const;
  201. %rename("has_key?" ) std::multimap::has_key const;
  202. %alias std::multimap::push "<<";
  203. %include <std/std_multimap.i>