ruby: df_flagarray support

develop
jj 2012-04-20 17:57:41 +02:00
parent 66891fd23c
commit f57d63ed95
2 changed files with 55 additions and 11 deletions

@ -419,8 +419,29 @@ class StlDeque < MemStruct
end
class DfFlagarray < MemStruct
# TODO
def inspect ; "#<DfFlagarray>" ; end
def length
DFHack.memory_bitarray_length(@_memaddr)
end
def size ; length ; end
def resize(len)
DFHack.memory_bitarray_resize(@_memaddr, len)
end
def [](idx)
idx += length if idx < 0
DFHack.memory_bitarray_isset(@_memaddr, idx) if idx >= 0 and idx < length
end
def []=(idx, v)
idx += length if idx < 0
if idx >= length or idx < 0
raise 'invalid idx'
else
DFHack.memory_bitarray_set(@_memaddr, idx, v)
end
end
include Enumerable
def each ; (0...length).each { |i| yield self[i] } ; end
def inspect ; to_a.inspect ; end
end
class DfArray < Compound
attr_accessor :_tglen, :_tg

@ -278,6 +278,7 @@ static void df_rubythread(void *p)
}
#define BOOL_ISFALSE(v) ((v) == Qfalse || (v) == Qnil || (v) == FIX2INT(0))
// main DFHack ruby module
static VALUE rb_cDFHack;
@ -296,7 +297,7 @@ static VALUE rb_dfonupdateactive(VALUE self)
static VALUE rb_dfonupdateactiveset(VALUE self, VALUE val)
{
onupdate_active = (val == Qfalse || val == Qnil || val == INT2FIX(0)) ? 0 : 1;
onupdate_active = (BOOL_ISFALSE(val) ? 0 : 1);
return Qtrue;
}
@ -582,19 +583,13 @@ static VALUE rb_dfmemory_vecbool_at(VALUE self, VALUE addr, VALUE idx)
static VALUE rb_dfmemory_vecbool_setat(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
if (val == Qnil || val == Qfalse || val == FIX2INT(0))
v->at(FIX2INT(idx)) = 0;
else
v->at(FIX2INT(idx)) = 1;
v->at(FIX2INT(idx)) = (BOOL_ISFALSE(val) ? 0 : 1);
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_insert(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
if (val == Qnil || val == Qfalse || val == FIX2INT(0))
v->insert(v->begin()+FIX2INT(idx), 0);
else
v->insert(v->begin()+FIX2INT(idx), 1);
v->insert(v->begin()+FIX2INT(idx), (BOOL_ISFALSE(val) ? 0 : 1));
return Qtrue;
}
static VALUE rb_dfmemory_vecbool_delete(VALUE self, VALUE addr, VALUE idx)
@ -604,6 +599,30 @@ static VALUE rb_dfmemory_vecbool_delete(VALUE self, VALUE addr, VALUE idx)
return Qtrue;
}
// BitArray
static VALUE rb_dfmemory_bitarray_length(VALUE self, VALUE addr)
{
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
return rb_uint2inum(b->size);
}
static VALUE rb_dfmemory_bitarray_resize(VALUE self, VALUE addr, VALUE sz)
{
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
b->resize(rb_num2ulong(sz));
return Qtrue;
}
static VALUE rb_dfmemory_bitarray_isset(VALUE self, VALUE addr, VALUE idx)
{
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
return b->is_set(rb_num2ulong(idx)) ? Qtrue : Qfalse;
}
static VALUE rb_dfmemory_bitarray_set(VALUE self, VALUE addr, VALUE idx, VALUE val)
{
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
b->set(rb_num2ulong(idx), (BOOL_ISFALSE(val) ? 0 : 1));
return Qtrue;
}
// define module DFHack and its methods
@ -657,6 +676,10 @@ static void ruby_bind_dfhack(void) {
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_setat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_setat), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_insert", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_insert), 3);
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_delete", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_delete), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_length", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_length), 1);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_resize", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_resize), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_isset", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_isset), 2);
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_set", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_set), 3);
// load the default ruby-level definitions
int state=0;