本文整理汇总了C++中RHASH_TBL函数的典型用法代码示例。如果您正苦于以下问题:C++ RHASH_TBL函数的具体用法?C++ RHASH_TBL怎么用?C++ RHASH_TBL使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RHASH_TBL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mrb_hash_has_keyWithKey
static mrb_value
mrb_hash_has_keyWithKey(mrb_state *mrb, mrb_value hash, mrb_value key)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_bool result;
if (h) {
k = kh_get(ht, h, key);
result = (k != kh_end(h));
}
else {
result = 0;
}
return mrb_bool_value(result);
}
示例2: mrb_hash_keys
mrb_value
mrb_hash_keys(mrb_state *mrb, mrb_value hash)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_value ary;
if (!h) return mrb_ary_new(mrb);
ary = mrb_ary_new_capa(mrb, kh_size(h));
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h, k)) {
mrb_value v = kh_key(h,k);
mrb_ary_push(mrb, ary, v);
}
}
return ary;
}
示例3: mrb_hash_has_valueWithvalue
static mrb_value
mrb_hash_has_valueWithvalue(mrb_state *mrb, mrb_value hash, mrb_value value)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
if (h) {
for (k = kh_begin(h); k != kh_end(h); k++) {
if (!kh_exist(h, k)) continue;
if (mrb_equal(mrb, kh_value(h,k), value)) {
return mrb_true_value();
}
}
}
return mrb_false_value();
}
示例4: mrb_hash_values
static mrb_value
mrb_hash_values(mrb_state *mrb, mrb_value hash)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_value ary = mrb_ary_new(mrb);
if (!h) return ary;
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h, k)){
mrb_value v = kh_value(h,k);
if ( !mrb_special_const_p(v) )
v = mrb_obj_dup(mrb, v);
mrb_ary_push(mrb, ary, v);
}
}
return ary;
}
示例5: mrb_hash_get
mrb_value
mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
if (h) {
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h))
return kh_value(h, k);
}
/* not found */
if (MRB_RHASH_PROCDEFAULT_P(hash)) {
return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, key);
}
return RHASH_IFNONE(hash);
}
示例6: mapFromRubyHash
/**
* Convert a Ruby Hash to a map
*
* @param t the Ruby Hash to convert
* @return a new map
*/
map* mapFromRubyHash(VALUE t){
map* res=NULL;
VALUE list;
list = rb_ary_new();
typedef int (*HOOK)(...);
rb_hash_foreach(t,reinterpret_cast<HOOK>(keys_i), list);
int nb=RHASH_TBL(t)->num_entries;
int i;
for(i=0;i<nb;i++){
VALUE key=rb_ary_pop(list);
VALUE value=rb_hash_aref(t,key);
#ifdef DEBUG
fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
StringValueCStr(key),StringValueCStr(value));
#endif
if(strcmp(StringValueCStr(key),"value")==0){
char *buffer=NULL;
int size=RSTRING_LEN(value);
buffer=StringValueCStr(value);
if(res!=NULL){
addToMap(res,StringValueCStr(key),"");
}else{
res=createMap(StringValueCStr(key),"");
}
map* tmpR=getMap(res,"value");
free(tmpR->value);
tmpR->value=(char*)malloc((size+1)*sizeof(char));
memmove(tmpR->value,buffer,size*sizeof(char));
tmpR->value[size]=0;
char sin[1024];
sprintf(sin,"%d",size);
addToMap(res,"size",sin);
}else{
if(res!=NULL){
addToMap(res,StringValueCStr(key),StringValueCStr(value));
}
else{
res=
createMap(StringValueCStr(key),StringValueCStr(value));
}
}
}
return res;
}
示例7: mrb_hash_set
void
mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val) /* mrb_hash_aset */
{
khash_t(ht) *h;
khiter_t k;
mrb_hash_modify(mrb, hash);
h = RHASH_TBL(hash);
k = kh_get(ht, h, key);
if (k == kh_end(h)) {
/* expand */
k = kh_put(ht, h, KEY(key));
}
kh_value(h, k) = val;
mrb_write_barrier(mrb, (struct RBasic*)RHASH(hash));
return;
}
示例8: mrb_hash_delete_key
mrb_value
mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_value delVal;
if (h) {
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h)) {
delVal = kh_value(h, k);
kh_del(ht, mrb, h, k);
return delVal;
}
}
/* not found */
return mrb_nil_value();
}
示例9: setup_hash
static VALUE
setup_hash(int argc, VALUE *argv)
{
VALUE hash;
if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (!RB_TYPE_P(hash, T_HASH))
rb_raise(rb_eTypeError, "non-hash given");
}
if (hash == Qnil) {
hash = rb_hash_new();
}
else if (!RHASH_EMPTY_P(hash)) {
st_foreach(RHASH_TBL(hash), set_zero_i, hash);
}
return hash;
}
示例10: rxml_xpath_context_register_namespaces
/*
* call-seq:
* context.register_namespaces(["prefix:uri"]) -> self
*
* Register the specified namespaces in this context. There are
* three different forms that libxml accepts. These include
* a string, an array of strings, or a hash table:
*
* context.register_namespaces('xi:http://www.w3.org/2001/XInclude')
* context.register_namespaces(['xlink:http://www.w3.org/1999/xlink',
* 'xi:http://www.w3.org/2001/XInclude')
* context.register_namespaces('xlink' => 'http://www.w3.org/1999/xlink',
* 'xi' => 'http://www.w3.org/2001/XInclude')
*/
static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
{
char *cp;
long i;
VALUE rprefix, ruri;
/* Need to loop through the 2nd argument and iterate through the
* list of namespaces that we want to allow */
switch (TYPE(nslist))
{
case T_STRING:
cp = strchr(StringValuePtr(nslist), (int) ':');
if (cp == NULL)
{
rprefix = nslist;
ruri = Qnil;
}
else
{
rprefix = rb_str_new(StringValuePtr(nslist), (int) ((long) cp
- (long) StringValuePtr(nslist)));
ruri = rb_str_new2(&cp[1]);
}
/* Should test the results of this */
rxml_xpath_context_register_namespace(self, rprefix, ruri);
break;
case T_ARRAY:
for (i = 0; i < RARRAY_LEN(nslist); i++)
{
rxml_xpath_context_register_namespaces(self, RARRAY_PTR(nslist)[i]);
}
break;
case T_HASH:
st_foreach(RHASH_TBL(nslist), iterate_ns_hash, self);
break;
default:
rb_raise(
rb_eArgError,
"Invalid argument type, only accept string, array of strings, or an array of arrays");
}
return self;
}
示例11: mrb_hash_get
MRB_API mrb_value
mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_sym mid;
if (h) {
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h))
return kh_value(h, k).v;
}
mid = mrb_intern_lit(mrb, "default");
if (mrb_func_basic_p(mrb, hash, mid, mrb_hash_default)) {
return hash_default(mrb, hash, key);
}
/* xxx mrb_funcall_tailcall(mrb, hash, "default", 1, key); */
return mrb_funcall_argv(mrb, hash, mid, 1, &key);
}
示例12: mrb_hash_keys
mrb_value
mrb_hash_keys(mrb_state *mrb, mrb_value hash)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_value ary, *p;
if (!h || kh_size(h) == 0) return mrb_ary_new(mrb);
ary = mrb_ary_new_capa(mrb, kh_size(h));
mrb_ary_set(mrb, ary, kh_size(h)-1, mrb_nil_value());
p = RARRAY_PTR(ary);
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h, k)) {
mrb_value kv = kh_key(h,k);
mrb_hash_value hv = kh_value(h,k);
p[hv.n] = kv;
}
}
return ary;
}
示例13: mrb_hash_has_value
static mrb_value
mrb_hash_has_value(mrb_state *mrb, mrb_value hash)
{
mrb_value val;
khash_t(ht) *h;
khiter_t k;
mrb_get_args(mrb, "o", &val);
h = RHASH_TBL(hash);
if (h) {
for (k = kh_begin(h); k != kh_end(h); k++) {
if (!kh_exist(h, k)) continue;
if (mrb_equal(mrb, kh_value(h, k).v, val)) {
return mrb_true_value();
}
}
}
return mrb_false_value();
}
示例14: count_tdata_objects
static VALUE
count_tdata_objects(int argc, VALUE *argv, VALUE self)
{
VALUE hash;
if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (TYPE(hash) != T_HASH)
rb_raise(rb_eTypeError, "non-hash given");
}
if (hash == Qnil) {
hash = rb_hash_new();
}
else if (!RHASH_EMPTY_P(hash)) {
st_foreach(RHASH_TBL(hash), set_zero_i, hash);
}
rb_objspace_each_objects(cto_i, (void *)hash);
return hash;
}
示例15: mrb_hash_dup
static mrb_value
mrb_hash_dup(mrb_state *mrb, mrb_value hash)
{
struct RHash* ret;
khash_t(ht) *h, *ret_h;
khiter_t k, ret_k;
mrb_value ifnone, vret;
h = RHASH_TBL(hash);
ret = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
ret->ht = kh_init(ht, mrb);
if (h && kh_size(h) > 0) {
ret_h = ret->ht;
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h, k)) {
int ai = mrb_gc_arena_save(mrb);
ret_k = kh_put(ht, mrb, ret_h, KEY(kh_key(h, k)));
mrb_gc_arena_restore(mrb, ai);
kh_val(ret_h, ret_k).v = kh_val(h, k).v;
kh_val(ret_h, ret_k).n = kh_size(ret_h)-1;
}
}
}
if (MRB_RHASH_DEFAULT_P(hash)) {
ret->flags |= MRB_HASH_DEFAULT;
}
if (MRB_RHASH_PROCDEFAULT_P(hash)) {
ret->flags |= MRB_HASH_PROC_DEFAULT;
}
vret = mrb_obj_value(ret);
ifnone = RHASH_IFNONE(hash);
if (!mrb_nil_p(ifnone)) {
mrb_iv_set(mrb, vret, mrb_intern_lit(mrb, "ifnone"), ifnone);
}
return vret;
}