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.

226 lines
5.9 KiB

  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. VALUE str = rb_str_new2( swig::type_name< MultiMap >() );
  109. str = rb_str_cat2( str, " {" );
  110. VALUE tmp;
  111. while ( i != e )
  112. {
  113. const MultiMap::key_type& key = i->first;
  114. const MultiMap::key_type& oldkey = key;
  115. tmp = swig::from( key );
  116. str = rb_str_buf_append( str, rb_inspect(tmp) );
  117. str = rb_str_cat2( str, "=>" );
  118. VALUE vals = rb_ary_new();
  119. for ( ; i != e && key == oldkey; ++i )
  120. {
  121. const MultiMap::mapped_type& val = i->second;
  122. tmp = swig::from( val );
  123. rb_ary_push( vals, tmp );
  124. }
  125. if ( RARRAY_LEN(vals) == 1 )
  126. {
  127. str = rb_str_buf_append( str, rb_inspect(tmp) );
  128. }
  129. else
  130. {
  131. str = rb_str_buf_append( str, rb_inspect(vals) );
  132. }
  133. }
  134. str = rb_str_cat2( str, "}" );
  135. return str;
  136. }
  137. VALUE to_a()
  138. {
  139. MultiMap::const_iterator i = $self->begin();
  140. MultiMap::const_iterator e = $self->end();
  141. VALUE ary = rb_ary_new2( std::distance( i, e ) );
  142. VALUE tmp;
  143. while ( i != e )
  144. {
  145. const MultiMap::key_type& key = i->first;
  146. const MultiMap::key_type& oldkey = key;
  147. tmp = swig::from( key );
  148. rb_ary_push( ary, tmp );
  149. VALUE vals = rb_ary_new();
  150. for ( ; i != e && key == oldkey; ++i )
  151. {
  152. const MultiMap::mapped_type& val = i->second;
  153. tmp = swig::from( val );
  154. rb_ary_push( vals, tmp );
  155. }
  156. if ( RARRAY_LEN(vals) == 1 )
  157. {
  158. rb_ary_push( ary, tmp );
  159. }
  160. else
  161. {
  162. rb_ary_push( ary, vals );
  163. }
  164. }
  165. return ary;
  166. }
  167. VALUE to_s()
  168. {
  169. MultiMap::iterator i = $self->begin();
  170. MultiMap::iterator e = $self->end();
  171. VALUE str = rb_str_new2( "" );
  172. VALUE tmp;
  173. while ( i != e )
  174. {
  175. const MultiMap::key_type& key = i->first;
  176. const MultiMap::key_type& oldkey = key;
  177. tmp = swig::from( key );
  178. tmp = rb_obj_as_string( tmp );
  179. str = rb_str_buf_append( str, tmp );
  180. VALUE vals = rb_ary_new();
  181. for ( ; i != e && key == oldkey; ++i )
  182. {
  183. const MultiMap::mapped_type& val = i->second;
  184. tmp = swig::from( val );
  185. rb_ary_push( vals, tmp );
  186. }
  187. tmp = rb_obj_as_string( vals );
  188. str = rb_str_buf_append( str, tmp );
  189. }
  190. return str;
  191. }
  192. }
  193. %enddef
  194. %mixin std::multimap "Enumerable";
  195. %rename("delete") std::multimap::__delete__;
  196. %rename("reject!") std::multimap::reject_bang;
  197. %rename("map!") std::multimap::map_bang;
  198. %rename("empty?") std::multimap::empty;
  199. %rename("include?" ) std::multimap::__contains__ const;
  200. %rename("has_key?" ) std::multimap::has_key const;
  201. %alias std::multimap::push "<<";
  202. %include <std/std_multimap.i>