[ruby] support for std unordered containers.
This commit is contained in:
parent
37af90a50e
commit
d29a325b99
4 changed files with 289 additions and 0 deletions
83
Lib/ruby/std_unordered_map.i
Normal file
83
Lib/ruby/std_unordered_map.i
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
//
|
||||||
|
// Maps
|
||||||
|
//
|
||||||
|
%include <std_map.i>
|
||||||
|
|
||||||
|
%fragment("StdUnorderedMapTraits","header",fragment="StdMapCommonTraits")
|
||||||
|
{
|
||||||
|
namespace swig {
|
||||||
|
template <class RubySeq, class K, class T >
|
||||||
|
inline void
|
||||||
|
assign(const RubySeq& rubyseq, std::unordered_map<K,T > *map) {
|
||||||
|
typedef typename std::unordered_map<K,T>::value_type value_type;
|
||||||
|
typename RubySeq::const_iterator it = rubyseq.begin();
|
||||||
|
for (;it != rubyseq.end(); ++it) {
|
||||||
|
map->insert(value_type(it->first, it->second));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class K, class T>
|
||||||
|
struct traits_asptr<std::unordered_map<K,T> > {
|
||||||
|
typedef std::unordered_map<K,T> map_type;
|
||||||
|
static int asptr(VALUE obj, map_type **val) {
|
||||||
|
int res = SWIG_ERROR;
|
||||||
|
if ( TYPE(obj) == T_HASH ) {
|
||||||
|
static ID id_to_a = rb_intern("to_a");
|
||||||
|
VALUE items = rb_funcall(obj, id_to_a, 0);
|
||||||
|
res = traits_asptr_stdseq<std::unordered_map<K,T>, std::pair<K, T> >::asptr(items, val);
|
||||||
|
} else {
|
||||||
|
map_type *p;
|
||||||
|
swig_type_info *descriptor = swig::type_info<map_type>();
|
||||||
|
res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR;
|
||||||
|
if (SWIG_IsOK(res) && val) *val = p;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class K, class T >
|
||||||
|
struct traits_from<std::unordered_map<K,T> > {
|
||||||
|
typedef std::unordered_map<K,T> map_type;
|
||||||
|
typedef typename map_type::const_iterator const_iterator;
|
||||||
|
typedef typename map_type::size_type size_type;
|
||||||
|
|
||||||
|
static VALUE from(const map_type& map) {
|
||||||
|
swig_type_info *desc = swig::type_info<map_type>();
|
||||||
|
if (desc && desc->clientdata) {
|
||||||
|
return SWIG_NewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN);
|
||||||
|
} else {
|
||||||
|
size_type size = map.size();
|
||||||
|
int rubysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
|
||||||
|
if (rubysize < 0) {
|
||||||
|
SWIG_RUBY_THREAD_BEGIN_BLOCK;
|
||||||
|
rb_raise( rb_eRuntimeError, "map size not valid in Ruby");
|
||||||
|
SWIG_RUBY_THREAD_END_BLOCK;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
VALUE obj = rb_hash_new();
|
||||||
|
for (const_iterator i= map.begin(); i!= map.end(); ++i) {
|
||||||
|
VALUE key = swig::from(i->first);
|
||||||
|
VALUE val = swig::from(i->second);
|
||||||
|
rb_hash_aset(obj, key, val);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define %swig_unordered_map_common(Map...) %swig_map_common(Map)
|
||||||
|
#define %swig_unordered_map_methods(Map...) %swig_map_methods(Map)
|
||||||
|
|
||||||
|
%rename("delete") std::unordered_map::__delete__;
|
||||||
|
%rename("reject!") std::unordered_map::reject_bang;
|
||||||
|
%rename("map!") std::unordered_map::map_bang;
|
||||||
|
%rename("empty?") std::unordered_map::empty;
|
||||||
|
%rename("include?") std::unordered_map::__contains__ const;
|
||||||
|
%rename("has_key?") std::unordered_map::has_key const;
|
||||||
|
|
||||||
|
%mixin std::map "Enumerable";
|
||||||
|
%alias std::unordered_map::push "<<";
|
||||||
|
|
||||||
|
%include <std/std_unordered_map.i>
|
||||||
102
Lib/ruby/std_unordered_multimap.i
Normal file
102
Lib/ruby/std_unordered_multimap.i
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
/*
|
||||||
|
Multimaps
|
||||||
|
*/
|
||||||
|
%include <std_multimap.i>
|
||||||
|
|
||||||
|
%fragment("StdUnorderedMultimapTraits","header",fragment="StdMapCommonTraits")
|
||||||
|
{
|
||||||
|
namespace swig {
|
||||||
|
template <class RubySeq, class K, class T >
|
||||||
|
inline void
|
||||||
|
assign(const RubySeq& rubyseq, std::unordered_multimap<K,T > *multimap) {
|
||||||
|
typedef typename std::unordered_multimap<K,T>::value_type value_type;
|
||||||
|
typename RubySeq::const_iterator it = rubyseq.begin();
|
||||||
|
for (;it != rubyseq.end(); ++it) {
|
||||||
|
multimap->insert(value_type(it->first, it->second));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class K, class T>
|
||||||
|
struct traits_asptr<std::unordered_multimap<K,T> > {
|
||||||
|
typedef std::unordered_multimap<K,T> multimap_type;
|
||||||
|
static int asptr(VALUE obj, std::unordered_multimap<K,T> **val) {
|
||||||
|
int res = SWIG_ERROR;
|
||||||
|
if ( TYPE(obj) == T_HASH ) {
|
||||||
|
static ID id_to_a = rb_intern("to_a");
|
||||||
|
VALUE items = rb_funcall(obj, id_to_a, 0);
|
||||||
|
return traits_asptr_stdseq<std::unordered_multimap<K,T>, std::pair<K, T> >::asptr(items, val);
|
||||||
|
} else {
|
||||||
|
multimap_type *p;
|
||||||
|
res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<multimap_type>(),0);
|
||||||
|
if (SWIG_IsOK(res) && val) *val = p;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class K, class T >
|
||||||
|
struct traits_from<std::unordered_multimap<K,T> > {
|
||||||
|
typedef std::unordered_multimap<K,T> multimap_type;
|
||||||
|
typedef typename multimap_type::const_iterator const_iterator;
|
||||||
|
typedef typename multimap_type::size_type size_type;
|
||||||
|
|
||||||
|
static VALUE from(const multimap_type& multimap) {
|
||||||
|
swig_type_info *desc = swig::type_info<multimap_type>();
|
||||||
|
if (desc && desc->clientdata) {
|
||||||
|
return SWIG_NewPointerObj(new multimap_type(multimap), desc, SWIG_POINTER_OWN);
|
||||||
|
} else {
|
||||||
|
size_type size = multimap.size();
|
||||||
|
int rubysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
|
||||||
|
if (rubysize < 0) {
|
||||||
|
SWIG_RUBY_THREAD_BEGIN_BLOCK;
|
||||||
|
rb_raise(rb_eRuntimeError,
|
||||||
|
"multimap_ size not valid in Ruby");
|
||||||
|
SWIG_RUBY_THREAD_END_BLOCK;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
VALUE obj = rb_hash_new();
|
||||||
|
for (const_iterator i= multimap.begin(); i!= multimap.end(); ++i) {
|
||||||
|
VALUE key = swig::from(i->first);
|
||||||
|
VALUE val = swig::from(i->second);
|
||||||
|
|
||||||
|
VALUE oldval = rb_hash_aref( obj, key );
|
||||||
|
if ( oldval == Qnil )
|
||||||
|
rb_hash_aset(obj, key, val);
|
||||||
|
else {
|
||||||
|
// Multiple values for this key, create array if needed
|
||||||
|
// and add a new element to it.
|
||||||
|
VALUE ary;
|
||||||
|
if ( TYPE(oldval) == T_ARRAY )
|
||||||
|
ary = oldval;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ary = rb_ary_new2(2);
|
||||||
|
rb_ary_push( ary, oldval );
|
||||||
|
rb_hash_aset( obj, key, ary );
|
||||||
|
}
|
||||||
|
rb_ary_push( ary, val );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define %swig_unordered_multimap_methods(MultiMap...) %swig_multimap_methods(MultiMap)
|
||||||
|
|
||||||
|
%mixin std::unordered_multimap "Enumerable";
|
||||||
|
|
||||||
|
%rename("delete") std::unordered_multimap::__delete__;
|
||||||
|
%rename("reject!") std::unordered_multimap::reject_bang;
|
||||||
|
%rename("map!") std::unordered_multimap::map_bang;
|
||||||
|
%rename("empty?") std::unordered_multimap::empty;
|
||||||
|
%rename("include?" ) std::unordered_multimap::__contains__ const;
|
||||||
|
%rename("has_key?" ) std::unordered_multimap::has_key const;
|
||||||
|
|
||||||
|
%alias std::unordered_multimap::push "<<";
|
||||||
|
|
||||||
|
%include <std/std_unordered_multimap.i>
|
||||||
|
|
||||||
50
Lib/ruby/std_unordered_multiset.i
Normal file
50
Lib/ruby/std_unordered_multiset.i
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
Multisets
|
||||||
|
*/
|
||||||
|
|
||||||
|
%include <std_unordered_set.i>
|
||||||
|
|
||||||
|
%fragment("StdUnorderedMultisetTraits","header",fragment="StdSequenceTraits")
|
||||||
|
%{
|
||||||
|
namespace swig {
|
||||||
|
template <class RubySeq, class T>
|
||||||
|
inline void
|
||||||
|
assign(const RubySeq& rubyseq, std::unordered_multiset<T>* seq) {
|
||||||
|
// seq->insert(rubyseq.begin(), rubyseq.end()); // not used as not always implemented
|
||||||
|
typedef typename RubySeq::value_type value_type;
|
||||||
|
typename RubySeq::const_iterator it = rubyseq.begin();
|
||||||
|
for (;it != rubyseq.end(); ++it) {
|
||||||
|
seq->insert(seq->end(),(value_type)(*it));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct traits_asptr<std::unordered_multiset<T> > {
|
||||||
|
static int asptr(VALUE obj, std::unordered_multiset<T> **m) {
|
||||||
|
return traits_asptr_stdseq<std::unordered_multiset<T> >::asptr(obj, m);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct traits_from<std::unordered_multiset<T> > {
|
||||||
|
static VALUE from(const std::unordered_multiset<T>& vec) {
|
||||||
|
return traits_from_stdseq<std::unordered_multiset<T> >::from(vec);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
#define %swig_unordered_multiset_methods(Set...) %swig_unordered_set_methods(Set)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%rename("delete") std::unordered_multiset::__delete__;
|
||||||
|
%rename("reject!") std::unordered_multiset::reject_bang;
|
||||||
|
%rename("map!") std::unordered_multiset::map_bang;
|
||||||
|
%rename("empty?") std::unordered_multiset::empty;
|
||||||
|
%rename("include?") std::unordered_multiset::__contains__ const;
|
||||||
|
%rename("has_key?") std::unordered_multiset::has_key const;
|
||||||
|
|
||||||
|
%alias std::unordered_multiset::push "<<";
|
||||||
|
|
||||||
|
%include <std/std_unordered_multiset.i>
|
||||||
54
Lib/ruby/std_unordered_set.i
Normal file
54
Lib/ruby/std_unordered_set.i
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
Sets
|
||||||
|
*/
|
||||||
|
|
||||||
|
%include <std_set.i>
|
||||||
|
|
||||||
|
%fragment("StdUnorderedSetTraits","header",fragment="<stddef.h>",fragment="StdSequenceTraits")
|
||||||
|
%{
|
||||||
|
namespace swig {
|
||||||
|
template <class RubySeq, class T>
|
||||||
|
inline void
|
||||||
|
assign(const RubySeq& rubyseq, std::unordered_set<T>* seq) {
|
||||||
|
// seq->insert(rubyseq.begin(), rubyseq.end()); // not used as not always implemented
|
||||||
|
typedef typename RubySeq::value_type value_type;
|
||||||
|
typename RubySeq::const_iterator it = rubyseq.begin();
|
||||||
|
for (;it != rubyseq.end(); ++it) {
|
||||||
|
seq->insert(seq->end(),(value_type)(*it));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct traits_asptr<std::unordered_set<T> > {
|
||||||
|
static int asptr(VALUE obj, std::unordered_set<T> **s) {
|
||||||
|
return traits_asptr_stdseq<std::unordered_set<T> >::asptr(obj, s);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct traits_from<std::unordered_set<T> > {
|
||||||
|
static VALUE from(const std::unordered_set<T>& vec) {
|
||||||
|
return traits_from_stdseq<std::unordered_set<T> >::from(vec);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
#define %swig_unordered_set_methods(set...) %swig_set_methods(set)
|
||||||
|
|
||||||
|
%mixin std::unordered_set "Enumerable";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%rename("delete") std::unordered_set::__delete__;
|
||||||
|
%rename("reject!") std::unordered_set::reject_bang;
|
||||||
|
%rename("map!") std::unordered_set::map_bang;
|
||||||
|
%rename("empty?") std::unordered_set::empty;
|
||||||
|
%rename("include?" ) std::unordered_set::__contains__ const;
|
||||||
|
%rename("has_key?" ) std::unordered_set::has_key const;
|
||||||
|
|
||||||
|
%alias std::unordered_set::push "<<";
|
||||||
|
|
||||||
|
|
||||||
|
%include <std/std_unordered_set.i>
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue