コード例 #1
0
int call_debug(struct re_printf *pf, const struct call *call)
{
	int err = 0;

	err |= re_hprintf(pf, "  call:      %p\n", call);
	err |= re_hprintf(pf, "  convid:    %s\n", call_convid(call));
	err |= re_hprintf(pf, "  sessionid: %s\n", flowmgr_call_sessid(call));
	err |= re_hprintf(pf, "  mediacat:  %s\n",
			  flowmgr_mediacat_name(call_mcat(call)));
	err |= re_hprintf(pf, "  flows:     %u   (active flows is %u)\n",
			  dict_count(call->flows),
			  flowmgr_call_count_active_flows(call));

	dict_apply(call->flows, flow_debug_handler, pf);
	err |= re_hprintf(pf, "\n");

	err |= re_hprintf(pf, "  users:     %u\n",
			  dict_count(call->users));
	dict_apply(call->users, userflow_debug_handler, pf);
	err |= re_hprintf(pf, "\n");

	err |= re_hprintf(pf, "%H", conf_pos_print, &call->conf_parts);
	err |= re_hprintf(pf, "\n");

	return err;
}
コード例 #2
0
ファイル: table_proc.c プロジェクト: nmandery/deb-opensmtpd
static int
imsg_add_params(struct ibuf *buf, struct dict *params)
{
	size_t count;
	const char *key;
	char *value;
	void *iter;

	count = 0;
	if (params)
		count = dict_count(params);

	if (imsg_add(buf, &count, sizeof(count)) == -1)
		return (-1);

	if (count == 0)
		return (0);

	iter = NULL;
	while (dict_iter(params, &iter, &key, (void **)&value)) {
		if (imsg_add(buf, key, strlen(key) + 1) == -1)
			return (-1);
		if (imsg_add(buf, value, strlen(value) + 1) == -1)
			return (-1);
	}

	return (0);
}
コード例 #3
0
int  call_count_flows(struct call *call)
{
	if (!call)
		return 0;

	return dict_count(call->flows);
}
コード例 #4
0
ファイル: ds.cpp プロジェクト: IDA-RE-things/cyrplw
int
RP_mapping::read_file(FILE *fp)
{
  clear();
  if ( mapping == NULL )
  {
   if ( verbose )
    msg("mapping problems");
   return 0;
  }
  Rubber_mem *str = new_Rubber();
  char *ptr;
  int linenum = 0;

  while( !feof(fp) )
  {
   rp_read_string(fp, str);
   linenum++;
   ptr = trimstring(str->p);
   if ( ! *ptr || *ptr == '#' ) // empty strings and comments are ignored
    continue;
   process_string(ptr, linenum);
  }
  if ( NULL != str )
   del_Rubber(str);
  return dict_count(mapping);
}
コード例 #5
0
ファイル: shop.c プロジェクト: denji/audited-objects
/// Used to find out how many, if any, PTXes remain viable.
/// @return the number of PTXes in the internal database
static long
_shop_ptx_count(shopping_state_s *ssp)
{
    long count;

    count = ssp->ptx_dict ? (long)dict_count(ssp->ptx_dict) : 0;
    return count;
}
コード例 #6
0
ファイル: dict.c プロジェクト: amuraru/libm2handler
void dict_set_allocator(dict_t *dict, dnode_alloc_t al,
        dnode_free_t fr, void *context)
{
    assert (dict_count(dict) == 0);
    assert ((al == NULL && fr == NULL) || (al != NULL && fr != NULL));

    dict->allocnode = al ? al : dnode_alloc;
    dict->freenode = fr ? fr : dnode_free;
    dict->context = context;
}
コード例 #7
0
ファイル: ta_dict.c プロジェクト: royratcliffe/ta-lib
unsigned int TA_DictSize( TA_Dict *dict )
{
   TA_PrivDictInfo *theDict;

   theDict = (TA_PrivDictInfo *)dict;

   if( theDict == NULL)
      return 0;

   return (unsigned int)dict_count( &theDict->d );
}
コード例 #8
0
int call_post_flows(struct call *call)
{
	if (!call)
		return EINVAL;

	info("flowmgr: call_post_flows (call=%p, users=%d)\n",
	     call, (int)dict_count(call->users));

	/* Empty post if there are no users in this conversation */
	if (dict_count(call->users) == 0)
		return flowmgr_post_flows(call);

	/* If there are users set, then when gathering is complete,
	 * we will create a async POST
	 */
	dict_apply(call->users, userflow_post_state_handler, NULL);
	dict_apply(call->users, userflow_post_handler, NULL);

	return 0;
}
コード例 #9
0
ファイル: types.c プロジェクト: 8l/awl
awlval* awlval_vals_dict(awlval* x) {
    int count = dict_count(x->d);
    awlval** vals = (awlval**)dict_all_vals(x->d);

    awlval* v = awlval_qexpr();
    for (int i = 0; i < count; i++) {
        awlval_add(v, awlval_copy(vals[i]));
    }

    free(vals);
    return v;
}
コード例 #10
0
ファイル: types.c プロジェクト: 8l/awl
awlval* awlval_keys_dict(awlval* x) {
    int count = dict_count(x->d);
    char** keys = dict_all_keys(x->d);

    awlval* v = awlval_qexpr();
    for (int i = 0; i < count; i++) {
        awlval_add(v, awlval_qsym(keys[i]));
    }

    free(keys);
    return v;
}
コード例 #11
0
void call_check_and_post(struct call *call)
{
	struct userflow *uf;
	int err = 0;

	if (!call)
		return;

	info("flowmgr: call_check_and_post(%p): users=%u flows=%u\n",
	     call, dict_count(call->users), dict_count(call->flows));

	uf = dict_apply(call->users, userflow_check_sdp_handler, call);
	if (uf == NULL) {
		err = flowmgr_post_flows(call);
		if (err) {
			warning("flowmgr: call: flowmgr_post_flows (%m)\n",
				err);
		}

		dict_apply(call->users, userflow_reset_state_handler, NULL);
	}
}
コード例 #12
0
ファイル: ca.c プロジェクト: gisco/audited-objects
/// Serializes the CmdAction and writes it to the specified file descriptor.
/// On Windows this must send to a socket. On Unix it can use any
/// kind of file descriptor.
/// @param[in] ca       the object pointer
/// @param[in] fd       file descriptor to which the serialized form is sent
void
ca_write(ca_o ca, int fd)
{
    dict_t *dict;
    dnode_t *dnp, *next;
    pa_o pa;
    CCS pabuf;

    dict = ca->ca_raw_pa_dict;

    if (dict_count(dict) <= 0) {
        return;
    }

    // In case buffered files were left open by sloppy audited programs.
    fflush(NULL);

    for (dnp = dict_first(dict); dnp;) {
        pa = (pa_o)dnode_getkey(dnp);

        // Unfortunately it's too early to dcode any written files
        // since they may still be open. But we should get stats of
        // non-member read ops since they may differ in the event
        // of a distributed build. We must assume logical coherence
        // for all write ops, member and non-member.
        // In other words, read ops are allowed to be on physically
        // separate files with the same path (e.g. /usr/include/stdio.h)
        // but write ops to the same path are assumed to be to a shared file.
        // This matters in the case of a distributed build.
        if (pa_is_read(pa) && !pa_is_member(pa)) {
            (void)pa_stat(pa, 0);
        }

        if ((pabuf = pa_toCSVString(pa))) {
            if (write(fd, pabuf, strlen(pabuf)) == -1) {
                putil_syserr(0, "write()");
            }
            putil_free(pabuf);
        }

        // Dictionary bookkeeping.
        next = dict_next(dict, dnp);

        dict_delete(dict, dnp);
        dnp = next;

        pa_destroy(pa);
    }
}
コード例 #13
0
computePhraseLMProbs ()
{
    int32	wcnt = dict_count (word_dict);
    int32	i;
    char	stmp[256];
    char	*p, *q, *r;

    for (i = 0; i < wcnt; i++) {
        int32	prob = 0;
        /*
         * Is this a phrase word ?
         */
        if (word_dict->dict_list[i]->wid != word_dict->dict_list[i]->fwid) {
            strcpy (stmp, word_dict->dict_list[i]->word);

            q = stmp;
            p = index (q, '_');
            if (p) {
                *p = '\0';
                p++;
            }
            while (q && p) {
                r = index (p, '_');
                if (r) {
                    *r = '\0';
                    r++;
                }
                /*
                     * Look out for alternate pronuciations in phrases and strip the
                     * modifier
                     */
                {
                    char *lp = rindex(p, '(');
                    char *rp = rindex(p, ')');
                    if (lp && rp) {
                        *lp = '\0';
                    }
                }
                prob += lmLogProbability (langModel, q, p);
                q = p;
                p = r;
            }

            log_info ("Phrase log prob [%20s] = %8d\n",
                      word_dict->dict_list[i]->word, prob);
            word_dict->dict_list[i]->lm_pprob = prob;
        }
    }
}
コード例 #14
0
ファイル: mproc.c プロジェクト: OpenSMTPD/OpenSMTPD
void
m_add_params(struct mproc *m, struct dict *d)
{
	const char *key;
	char *value;
	void *iter;

	if (d == NULL) {
		m_add_size(m, 0);
		return;
	}
	m_add_size(m, dict_count(d));
	iter = NULL;
	while (dict_iter(d, &iter, &key, (void **)&value)) {
		m_add_string(m, key);
		m_add_string(m, value);
	}
}
コード例 #15
0
ファイル: classtree.cpp プロジェクト: IDA-RE-things/cyrplw
void
process_from_founded_sig(dict_t *dict)
{
  if ( !dict || !dict_count(dict) )
   return;
  if ( !check_rpd_count(true) )
   return;
  dnode_t *node;
  pdb_class *pc;
  char *c_name;
  ea_t ea;
  for ( node = dict_first(dict); node; node = dict_next(dict, node) )
  {
    c_name = (char *)dnode_get(node);
    ea = (ea_t)dnode_getkey(node);
    pc = p_pool->find_class(c_name);
    if ( !pc )
     continue;
    fill_vtbl(c_name, ea, pc);
  }
}
コード例 #16
0
ファイル: print.c プロジェクト: Gioyik/Spow
static void zlval_dict_print(stringbuilder_t* sb, const dict* d) {
    stringbuilder_write(sb, "[");

    int count = dict_count(d);
    char** keys = dict_all_keys(d);
    zlval** vals = (zlval**)dict_all_vals(d);

    for (int i = 0; i < count; i++) {
        stringbuilder_write(sb, ":'%s'", keys[i]);
        stringbuilder_write(sb, " ");
        zlval_write_sb(sb, vals[i]);

        if (i != (count - 1)) {
            stringbuilder_write(sb, " ");
        }
    }

    free(keys);
    free(vals);

    stringbuilder_write(sb, "]");
}
コード例 #17
0
ファイル: ta_dict.c プロジェクト: royratcliffe/ta-lib
int TA_DictAccessFirst( TA_Dict *dict )
{
   TA_PrivDictInfo *theDict;
   dict_t *kazlibDict;

   theDict = (TA_PrivDictInfo *)dict;

   if( theDict == NULL )
      return (int)NULL;
   kazlibDict = &theDict->d;

   if( !(theDict->flags & TA_DICT_KEY_ONE_STRING) )
      return 0; /* All other dictionary type are not supported. */

   /* Get the first node. */
   theDict->accessNode = dict_first( theDict->libHandle, kazlibDict );

   if( !theDict->accessNode )
      return 0;

   return dict_count( kazlibDict );
}
コード例 #18
0
ファイル: dict.c プロジェクト: amuraru/libm2handler
int dict_verify(dict_t *dict)
{
    dnode_t *nil = dict_nil(dict), *root = dict_root(dict);

    /* check that the sentinel node and root node are black */
    if (root->color != dnode_black)
        return 0;
    if (nil->color != dnode_black)
        return 0;
    if (nil->right != nil)
        return 0;
    /* nil->left is the root node; check that its parent pointer is nil */
    if (nil->left->parent != nil)
        return 0;
    /* perform a weak test that the tree is a binary search tree */
    if (!verify_bintree(dict))
        return 0;
    /* verify that the tree is a red-black tree */
    if (!verify_redblack(nil, root))
        return 0;
    if (verify_node_count(nil, root) != dict_count(dict))
        return 0;
    return 1;
}
コード例 #19
0
ファイル: unit_tests.c プロジェクト: weixu8/libdict
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
    CU_ASSERT_TRUE(dict_verify(dct));

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));

	for (unsigned j = 0; j <= i; ++j)
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].value);
	for (unsigned j = i + 1; j < nkeys; ++j)
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
	/* Verify that hashtable_resize works as expected. */
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_TRUE(dict_verify(dct));
	CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
	CU_ASSERT_TRUE(dict_verify(dct));
	for (unsigned j = 0; j < nkeys; ++j)
	    CU_ASSERT_EQUAL(dict_search(clone, keys[j].key), keys[j].value);
	dict_free(clone);
    }

    if (dct->_vtable->clone) {
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_PTR_NOT_NULL(clone);
	CU_ASSERT_TRUE(dict_verify(clone));
	CU_ASSERT_EQUAL(dict_count(clone), nkeys);
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_EQUAL(dict_search(clone, keys[i].key), keys[i].value);
	}
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
	}
	dict_free(clone);
    }

    for (unsigned i = 0; i < nkeys; ++i)
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].value);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_EQUAL(*datum_location, keys[i].value);

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    dict_itor *itor = dict_itor_new(dct);
    CU_ASSERT_PTR_NOT_NULL(itor);
    char *last_key = NULL;
    unsigned n = 0;
    for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	++n;
	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
	    }
	    last_key = dict_itor_key(itor);
	}
    }
    CU_ASSERT_EQUAL(n, nkeys);
    last_key = NULL;
    n = 0;
    for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	++n;
	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
	    }
	    last_key = dict_itor_key(itor);
	}
    }
    CU_ASSERT_EQUAL(n, nkeys);
    dict_itor_free(itor);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].alt;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i)
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);

    for (unsigned i = 0; i < nkeys; ++i) {
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);
	CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
	CU_ASSERT_TRUE(dict_verify(dct));

	CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
	for (unsigned j = 0; j <= i; ++j) {
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
	}
	for (unsigned j = i + 1; j < nkeys; ++j) {
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].alt);
	}
    }

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_clear(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_free(dct), nkeys);
}
コード例 #20
0
ファイル: demo.c プロジェクト: weixu8/libdict
int
main(int argc, char **argv)
{
    char buf[512], *p, *ptr, *ptr2;
    int rv;
    dict *dct;

    if (argc != 2)
	quit("usage: %s [type]", appname);

    srand((unsigned)time(NULL));

    dict_malloc_func = xmalloc;

    ++argv;
    switch (argv[0][0]) {
	case 'h':
	    dct = hb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'p':
	    dct = pr_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'r':
	    dct = rb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 't':
	    dct = tr_dict_new((dict_compare_func)strcmp, NULL, key_val_free);
	    break;
	case 's':
	    dct = sp_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'w':
	    dct = wb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'H':
	    dct = hashtable_dict_new((dict_compare_func)strcmp,
				     dict_str_hash,
				     key_val_free, HSIZE);
	    break;
	default:
	    quit("type must be one of h, p, r, t, s, w, or H");
    }

    if (!dct)
	quit("can't create container");

    for (;;) {
	printf("> ");
	fflush(stdout);
	if (fgets(buf, sizeof(buf), stdin) == NULL)
	    break;
	if ((p = strchr(buf, '\n')) != NULL)
	    *p = 0;
	for (p = buf; isspace(*p); p++)
	    /* void */;
	strcpy(buf, p);
	ptr2 = (ptr = strtok(buf, " ") ? strtok(NULL, " ") : NULL) ?
	    strtok(NULL, " ") : NULL;
	if (*buf == 0)
	    continue;
	if (strcmp(buf, "insert") == 0) {
	    if (!ptr2) {
		printf("usage: insert <key> <data>\n");
		continue;
	    }
	    void **datum_location;
	    if (dict_insert(dct, xstrdup(ptr), &datum_location)) {
		*datum_location = xstrdup(ptr2);
		printf("inserted '%s': '%s'\n",
		       ptr, *datum_location);
	    } else {
		printf("key '%s' already in dict: '%s'\n",
		       ptr, *datum_location);
	    }
	} else if (strcmp(buf, "search") == 0) {
	    if (ptr2) {
		printf("usage: search <key>\n");
		continue;
	    }
	    ptr2 = dict_search(dct, ptr);
	    if (ptr2)
		printf("found '%s': '%s'\n", ptr, ptr2);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "remove") == 0) {
	    if (!ptr || ptr2) {
		printf("usage: remove <key>\n");
		continue;
	    }
	    rv = dict_remove(dct, ptr);
	    if (rv == 0)
		printf("removed '%s' from dict\n", ptr);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "show") == 0) {
	    if (ptr) {
		printf("usage: show\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_first(itor);
	    for (; dict_itor_valid(itor); dict_itor_next(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "reverse") == 0) {
	    if (ptr) {
		printf("usage: reverse\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_last(itor);
	    for (; dict_itor_valid(itor); dict_itor_prev(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "clear") == 0) {
	    if (ptr) {
		printf("usage: clear\n");
		continue;
	    }
	    dict_clear(dct);
	} else if (strcmp(buf, "count") == 0) {
	    if (ptr) {
		printf("usage: count\n");
		continue;
	    }
	    printf("count = %zu\n", dict_count(dct));
	} else if (strcmp(buf, "quit") == 0) {
	    break;
	} else {
	    printf("Usage summary:\n");
	    printf("  insert <key> <data>\n");
	    printf("  search <key>\n");
	    printf("  remove <key>\n");
	    printf("  clear\n");
	    printf("  count\n");
	    printf("  show\n");
	    printf("  reverse\n");
	    printf("  quit\n");
	}
    }

    dict_free(dct);

    exit(0);
}
コード例 #21
0
ファイル: types.c プロジェクト: 8l/awl
awlval* awlval_rm_dict(awlval* x, awlval* k) {
    dict_rm(x->d, k->sym);
    x->count = x->length = dict_count(x->d);
    return x;
}
コード例 #22
0
ファイル: kernel.cpp プロジェクト: whunmr/circa
void Dict__count(caStack* stack)
{
    caValue* dict = circa_input(stack, 0);
    set_int(circa_output(stack, 0), dict_count(dict));
}
コード例 #23
0
ファイル: ca.c プロジェクト: gisco/audited-objects
/// Returns the number of raw PathActions held in the CmdAction.
/// @param[in] ca       the object pointer
/// @return the number of raw PathActions
int
ca_get_pa_count(ca_o ca)
{
    return (int)dict_count(ca->ca_raw_pa_dict);
}
コード例 #24
0
ファイル: benchmark.c プロジェクト: fmela/libdict
int
main(int argc, char **argv)
{
    bool shuffle_keys = true;

    if (argc != 3) {
	fprintf(stderr, "usage: %s [type] [input]\n", appname);
	fprintf(stderr, "type: specifies the dictionary type:\n");
	fprintf(stderr, "   h: height-balanced tree\n");
	fprintf(stderr, "   p: path-reduction tree\n");
	fprintf(stderr, "   r: red-black tree\n");
	fprintf(stderr, "   t: treap\n");
	fprintf(stderr, "   s: splay tree\n");
	fprintf(stderr, "   w: weight-balanced tree\n");
	fprintf(stderr, "   S: skiplist\n");
	fprintf(stderr, "   H: hashtable\n");
	fprintf(stderr, "   2: hashtable 2\n");
	fprintf(stderr, "input: text file consisting of newline-separated keys\n");
	exit(EXIT_FAILURE);
    }

    srand(0xdeadbeef);

    dict_malloc_func = xmalloc;

    const char type = argv[1][0];
    const char *container_name = NULL;
    dict *dct = create_dictionary(type, &container_name);
    if (!dct)
	quit("can't create container");

    ASSERT(dict_verify(dct));
    ASSERT(comp_count == 0);
    ASSERT(hash_count == 0);

    const size_t malloced_save = malloced;

    FILE *fp = fopen(argv[2], "r");
    if (fp == NULL)
	quit("cant open file '%s': %s", argv[2], strerror(errno));

    size_t nwords = 0;
    char buf[512];
    while (fgets(buf, sizeof(buf), fp))
	++nwords;

    if (!nwords)
	quit("nothing read from file");

    char **words = xmalloc(sizeof(*words) * nwords);
    rewind(fp);
    size_t words_read = 0;
    while (words_read < nwords && fgets(buf, sizeof(buf), fp)) {
	strtok(buf, "\n");
	words[words_read++] = xstrdup(buf);
    }
    fclose(fp);
    if (words_read < nwords)
	quit("Only read %zu/%zu words!", words_read, nwords);
    printf("Loaded %zu keys from %s.\n", nwords, argv[2]);

    malloced = malloced_save;
    size_t total_comp = 0, total_hash = 0, total_rotations = 0;

    struct rusage start, end;
    struct timeval total = { 0, 0 };

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	dict_insert_result result = dict_insert(dct, words[i]);
	if (!result.inserted)
	    quit("insert #%d failed for '%s'", i, words[i]);
	ASSERT(result.datum_ptr != NULL);
	ASSERT(*result.datum_ptr == NULL);
	*result.datum_ptr = words[i];
    }
    timer_end(&start, &end, &total);
    printf("    %s container: %.02fkB\n", container_name, malloced_save * 1e-3);
    printf("       %s memory: %.02fkB\n", container_name, malloced * 1e-3);
    printf("       %s insert: %6.03fs %9zu cmp (%.02f/insert)",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, comp_count / (double) nwords);
    if (hash_count)
	printf(" %9zu hash", hash_count);
    printf("\n");
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (dict_is_sorted(dct) && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf(" min path length: %zu\n", tree_min_path_length(tree));
	printf(" max path length: %zu\n", tree_max_path_length(tree));
	printf(" tot path length: %zu\n", tree_total_path_length(tree));
	printf("insert rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    } else if (type == 'S') {
	size_t counts[16] = { 0 };
	size_t num_counts = skiplist_link_count_histogram(dict_private(dct), counts, sizeof(counts) / sizeof(counts[0]));
	size_t count_sum = 0;
	for (size_t i = 0; i <= num_counts; ++i) {
	    printf("skiplist %zu-node(s): %zu\n", i, counts[i]);
	    count_sum += counts[i];
	}
	ASSERT(count_sum == nwords);
    }

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    size_t n = dict_count(dct);
    if (n != nwords)
	quit("bad count (%u - should be %u)!", n, nwords);

    dict_itor *itor = dict_itor_new(dct);

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_first(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor));
	++n;
    } while (dict_itor_next(itor));
    timer_end(&start, &end, &total);
    printf("  %s fwd iterate: %6.03fs\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Fwd iteration returned %u items - should be %u", n, nwords);

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_last(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor));
	++n;
    } while (dict_itor_prev(itor));
    timer_end(&start, &end, &total);
    printf("  %s rev iterate: %6.03fs\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Rev iteration returned %u items - should be %u", n, nwords);

    dict_itor_free(itor);

    if (shuffle_keys) shuffle(words, nwords);

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	void **p = dict_search(dct, words[i]);
	if (!p)
	    quit("lookup failed for '%s'", buf);
	if (*p != words[i])
	    quit("bad data for '%s', got '%s' instead", words[i], *(char **)p);
    }
    timer_end(&start, &end, &total);
    printf("  %s good search: %6.03fs %9zu cmp (%.02f/search)",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, comp_count / (double) nwords);
    if (hash_count)
	printf(" %9zu hash", hash_count);
    printf("\n");
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("search rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	unsigned rv = dict_rand() % strlen(words[i]);
	words[i][rv]++;
	dict_search(dct, words[i]);
	words[i][rv]--;
    }
    timer_end(&start, &end, &total);
    printf("   %s bad search: %6.03fs %9zu cmp (%.02f/search)",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, comp_count / (double) nwords);
    if (hash_count)
	printf(" %9zu hash", hash_count);
    printf("\n");
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    if (shuffle_keys) shuffle(words, nwords);

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	dict_remove_result result = dict_remove(dct, words[i]);
	if (!result.removed)
	    quit("removing #%d '%s' failed!\n", i, words[i]);
	ASSERT(result.key == words[i]);
	ASSERT(result.datum == words[i]);
    }
    timer_end(&start, &end, &total);
    printf("       %s remove: %6.03fs %9zu cmp (%.2f/remove)",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, comp_count / (double)nwords);
    if (hash_count)
	printf(" %9zu hash", hash_count);
    printf("\n");
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("remove rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));
    comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */

    if ((n = dict_count(dct)) != 0)
	quit("error - count not zero (%u)!", n);

    dict_free(dct, key_str_free);

    printf("        %s total: %6.03fs %9zu cmp",
	   container_name,
	   (total.tv_sec * 1000000 + total.tv_usec) * 1e-6,
	   total_comp);
    if (total_hash)
	printf(" %9zu hash", total_hash);
    printf("\n");

    if (type != 'H' && type != '2' && type != 'S') {
	printf(" total rotations: %zu\n", total_rotations);
    }

    FREE(words);

    exit(EXIT_SUCCESS);
}
コード例 #25
0
ファイル: benchmark.c プロジェクト: cmdrclueless/libdict
int
main(int argc, char **argv)
{
    if (argc != 3) {
	fprintf(stderr, "usage: %s [type] [input]\n", appname);
	fprintf(stderr, "type: specifies the dictionary type:\n");
	fprintf(stderr, "   h: height-balanced tree\n");
	fprintf(stderr, "   p: path-reduction tree\n");
	fprintf(stderr, "   r: red-black tree\n");
	fprintf(stderr, "   t: treap\n");
	fprintf(stderr, "   s: splay tree\n");
	fprintf(stderr, "   w: weight-balanced tree\n");
	fprintf(stderr, "   S: skiplist\n");
	fprintf(stderr, "   H: hashtable\n");
	fprintf(stderr, "   2: hashtable 2\n");
	fprintf(stderr, "input: text file consisting of newline-separated keys\n");
	exit(EXIT_FAILURE);
    }

    srand(0xdeadbeef);

    dict_malloc_func = xmalloc;

    const char type = argv[1][0];
    const char *container_name = NULL;
    dict *dct = create_dictionary(type, &container_name);
    if (!dct)
	quit("can't create container");

    ASSERT(dict_verify(dct));

    const size_t malloced_save = malloced;

    FILE *fp = fopen(argv[2], "r");
    if (fp == NULL)
	quit("cant open file '%s': %s", argv[2], strerror(errno));

    unsigned nwords = 0;
    char buf[512];
    while (fgets(buf, sizeof(buf), fp))
	++nwords;

    if (!nwords)
	quit("nothing read from file");

    char **words = xmalloc(sizeof(*words) * nwords);
    rewind(fp);
    for (unsigned i = 0; i < nwords && fgets(buf, sizeof(buf), fp); i++) {
	strtok(buf, "\n");
	words[i] = xstrdup(buf);
    }
    fclose(fp);

    malloced = malloced_save;
    size_t total_comp = 0, total_hash = 0, total_rotations = 0;

    struct rusage start, end;
    struct timeval total = { 0, 0 };

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, words[i], &inserted);
	if (!inserted)
	    quit("insert #%d failed for '%s'", i, words[i]);
	ASSERT(datum_location != NULL);
	ASSERT(*datum_location == NULL);
	*datum_location = words[i];
    }
    timer_end(&start, &end, &total);
    printf("    %s container: %.02fkB\n", container_name, malloced_save * 1e-3);
    printf("       %s memory: %.02fkB\n", container_name, malloced * 1e-3);
    printf("       %s insert: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("insert rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));

    unsigned n = dict_count(dct);
    if (n != nwords)
	quit("bad count (%u - should be %u)!", n, nwords);

    dict_itor *itor = dict_itor_new(dct);

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_first(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_data(itor));
	++n;
    } while (dict_itor_next(itor));
    timer_end(&start, &end, &total);
    printf("  %s fwd iterate: %6.03f s\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Fwd iteration returned %u items - should be %u", n, nwords);

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_last(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_data(itor));
	++n;
    } while (dict_itor_prev(itor));
    timer_end(&start, &end, &total);
    printf("  %s rev iterate: %6.03f s\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Rev iteration returned %u items - should be %u", n, nwords);

    dict_itor_free(itor);

    /* shuffle(words, nwords); */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	char *p = dict_search(dct, words[i]);
	if (!p)
	    quit("lookup failed for '%s'", buf);
	if (p != words[i])
	    quit("bad data for '%s', got '%s' instead", words[i], p);
    }
    timer_end(&start, &end, &total);
    printf("  %s good search: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("search rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	int rv = rand() % strlen(words[i]);
	words[i][rv]++;
	dict_search(dct, words[i]);
	words[i][rv]--;
    }
    timer_end(&start, &end, &total);
    printf("   %s bad search: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;

    /* shuffle(words, nwords); */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	if (!dict_remove(dct, words[i]))
	    quit("removing #%d '%s' failed!\n", i, words[i]);
    }
    timer_end(&start, &end, &total);
    printf("       %s remove: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("remove rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));

    if ((n = dict_count(dct)) != 0)
	quit("error - count not zero (%u)!", n);

    dict_free(dct);

    printf("        %s total: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (total.tv_sec * 1000000 + total.tv_usec) * 1e-6,
	   total_comp, total_hash);

    if (type != 'H' && type != '2' && type != 'S') {
	printf(" total rotations: %zu\n", total_rotations);
    }

    FREE(words);

    exit(EXIT_SUCCESS);
}
コード例 #26
0
ファイル: ws_variable.c プロジェクト: amuraru/libm2handler
int main(int argc, char **args){
    if(argc != 3){
        fprintf(stderr,"%s RECV SEND\n",args[0]);
        exit(1);
    }
    signal(SIGINT,&call_for_stop);
    
    bstring pull_addr = bfromcstr(args[1]);
    bstring pub_addr  = bfromcstr(args[2]);

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
mongrel2_set_identity(pub_socket, bdata(&SENDER) );

    mongrel2_request *request;

    // Polling is done to show how to do a clean shutdown
    int poll_response;
    zmq_pollitem_t socket_tracker;
    socket_tracker.socket = pull_socket->zmq_socket;
    socket_tracker.events = ZMQ_POLLIN;

    // Let's try out some ADT goodness
    dict_t* dict = dict_create(DICTCOUNT_T_MAX, compare_session);
    dict_set_allocator(dict, alloc_dict, free_dict, NULL);

    dnode_t* tempnode = NULL;
    m2_ws_session_data *counter = NULL;
    int retval = 0;
    while(shutdown != 1){
        poll_response = zmq_poll(&socket_tracker,1,500*1000);
        if(poll_response > 0){
            request = mongrel2_recv(pull_socket);
            fprintf(stdout,"got something...\n");

            if(request != NULL && mongrel2_request_for_disconnect(request) != 1){
                m2_ws_session_id* incoming = calloc(1,sizeof(m2_ws_session_id));
                incoming->req = request;
                printf("Looking at incoming->conn_id = %d\n",incoming->req->conn_id);
                tempnode = dict_lookup(dict,incoming);

                if(tempnode == NULL){
                    mongrel2_ws_reply_upgrade(request,pub_socket);
                    counter = calloc(1,sizeof(m2_ws_session_data));
                    counter->times_seen = 0;
                    retval = dict_alloc_insert(dict,incoming,counter);
                    assert(retval == 1);
                } else {
                    free(incoming);
                    counter = dnode_get(tempnode);
                    counter->times_seen += 1;
                }

                if(blength(request->body) > 0){
                    if(tempnode && mongrel2_ws_frame_get_fin(blength(request->body),
                                                             (uint8_t*)bdata(request->body))){
                        printf("Hey, it's a close\n");
                        dict_delete_free(dict,tempnode);
                        mongrel2_disconnect(pub_socket,request);
                    }
                } else {
                    bstring randmsg = genrandmsg();
                    mongrel2_ws_reply(pub_socket,request,randmsg);
                }
                

                printf("FYI: we've got %ld entries\n",dict_count(dict));
            } else {
                fprintf(stdout,"Connection %d disconnected\n", request->conn_id);
            }
        } else if (poll_response < 0){
            fprintf(stdout, "Error on poll!");
            shutdown = 1;
        }
    }
    // bstring msg = bformat("{\"msg\" : \"hi there %d\"}", request->conn_id);
    // fprintf(stdout,"Sending new msg: '%*s'",blength(msg),bdata(msg));
    // mongrel2_ws_reply(pub_socket,request,msg);
    // bdestroy(msg);
    // mongrel2_request_finalize(request);
    // mongrel2_reply(pub_socket,request,bfromcstr(""));
    
    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    fprintf(stdout,"\nClean shutdown done! Thanks for playing!\n");
    return 0;
}
コード例 #27
0
ファイル: dict.c プロジェクト: amuraru/libm2handler
int main(void)
{
    input_t in;
    dict_t darray[10];
    dict_t *d = &darray[0];
    dnode_t *dn;
    size_t i;
    char *tok1, *tok2, *val;
    const char *key;

    char *help =
        "a <key> <val>          add value to dictionary\n"
        "d <key>                delete value from dictionary\n"
        "l <key>                lookup value in dictionary\n"
        "( <key>                lookup lower bound\n"
        ") <key>                lookup upper bound\n"
        "< <key>                lookup strict lower bound\n"
        "> <key>                lookup strict upper bound\n"
        "# <num>                switch to alternate dictionary (0-9)\n"
        "j <num> <num>          merge two dictionaries\n"
        "f                      free the whole dictionary\n"
        "k                      allow duplicate keys\n"
        "c                      show number of entries\n"
        "t                      dump whole dictionary in sort order\n"
        "m                      make dictionary out of sorted items\n"
        "p                      turn prompt on\n"
        "s                      switch to non-functioning allocator\n"
        "q                      quit";

    for (i = 0; i < sizeof darray / sizeof *darray; i++)
        dict_init(&darray[i], DICTCOUNT_T_MAX, comparef);

    for (;;) {
        if (prompt)
            putchar('>');
        fflush(stdout);

        if (!fgets(in, sizeof(input_t), stdin))
            break;

        switch(in[0]) {
            case '?':
                puts(help);
                break;
            case 'a':
                if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
                    puts("what?");
                    break;
                }
                key = dupstring(tok1);
                val = dupstring(tok2);

                if (!key || !val) {
                    puts("out of memory");
                    free((void *) key);
                    free(val);
                }

                if (!dict_alloc_insert(d, key, val)) {
                    puts("dict_alloc_insert failed");
                    free((void *) key);
                    free(val);
                    break;
                }
                break;
            case 'd':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                }
                dn = dict_lookup(d, tok1);
                if (!dn) {
                    puts("dict_lookup failed");
                    break;
                }
                val = (char *) dnode_get(dn);
                key = (char *) dnode_getkey(dn);
                dict_delete_free(d, dn);

                free(val);
                free((void *) key);
                break;
            case 'f':
                dict_free_nodes(d);
                break;
            case 'l':
            case '(':
            case ')':
            case '<':
            case '>':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                }
                dn = 0;
                switch (in[0]) {
                case 'l':
                    dn = dict_lookup(d, tok1);
                    break;
                case '(':
                    dn = dict_lower_bound(d, tok1);
                    break;
                case ')':
                    dn = dict_upper_bound(d, tok1);
                    break;
                case '<':
                    dn = dict_strict_lower_bound(d, tok1);
                    break;
                case '>':
                    dn = dict_strict_upper_bound(d, tok1);
                    break;
                }
                if (!dn) {
                    puts("lookup failed");
                    break;
                }
                val = (char *) dnode_get(dn);
                puts(val);
                break;
            case 'm':
                construct(d);
                break;
            case 'k':
                dict_allow_dupes(d);
                break;
            case 'c':
                printf("%lu\n", (unsigned long) dict_count(d));
                break;
            case 't':
                for (dn = dict_first(d); dn; dn = dict_next(d, dn)) {
                    printf("%s\t%s\n", (char *) dnode_getkey(dn),
                            (char *) dnode_get(dn));
                }
                break;
            case 'q':
                exit(0);
                break;
            case '\0':
                break;
            case 'p':
                prompt = 1;
                break;
            case 's':
                dict_set_allocator(d, new_node, del_node, NULL);
                break;
            case '#':
                if (tokenize(in+1, &tok1, (char **) 0) != 1) {
                    puts("what?");
                    break;
                } else {
                    int dictnum = atoi(tok1);
                    if (dictnum < 0 || dictnum > 9) {
                        puts("invalid number");
                        break;
                    }
                    d = &darray[dictnum];
                }
                break;
            case 'j':
                if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
                    puts("what?");
                    break;
                } else {
                    int dict1 = atoi(tok1), dict2 = atoi(tok2);
                    if (dict1 < 0 || dict1 > 9 || dict2 < 0 || dict2 > 9) {
                        puts("invalid number");
                        break;
                    }
                    dict_merge(&darray[dict1], &darray[dict2]);
                }
                break;
            default:
                putchar('?');
                putchar('\n');
                break;
        }
    }

    return 0;
}
コード例 #28
0
ファイル: types.c プロジェクト: 8l/awl
awlval* awlval_add_dict(awlval* x, awlval* k, awlval* v) {
    dict_put(x->d, k->sym, v);
    x->count = x->length = dict_count(x->d);
    return x;
}
コード例 #29
0
ファイル: unit_tests.c プロジェクト: cmdrclueless/libdict
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys,
		const struct closest_lookup_info *cl_infos,
		unsigned n_cl_infos) {
    dict_itor *itor = dict_itor_new(dct);

    CU_ASSERT_TRUE(dict_verify(dct));

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));

	for (unsigned j = 0; j <= i; ++j)
	    test_search(dct, itor, keys[j].key, keys[j].value);
	for (unsigned j = i + 1; j < nkeys; ++j)
	    test_search(dct, itor, keys[j].key, NULL);
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    if (dct->_vtable->insert == (dict_insert_func)hashtable_insert ||
	dct->_vtable->insert == (dict_insert_func)hashtable2_insert) {
	/* Verify that hashtable_resize works as expected. */
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_TRUE(dict_verify(dct));
	if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
	    CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
	} else {
	    CU_ASSERT_TRUE(hashtable2_resize(dict_private(clone), 3));
	}
	CU_ASSERT_TRUE(dict_verify(dct));
	for (unsigned j = 0; j < nkeys; ++j)
	    test_search(clone, NULL, keys[j].key, keys[j].value);
	dict_free(clone);
    }

    if (dct->_vtable->clone) {
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_PTR_NOT_NULL(clone);
	CU_ASSERT_TRUE(dict_verify(clone));
	CU_ASSERT_EQUAL(dict_count(clone), nkeys);
	for (unsigned i = 0; i < nkeys; ++i) {
	    test_search(clone, itor, keys[i].key, keys[i].value);
	}
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
	}
	dict_free(clone);
    }

    for (unsigned i = 0; i < nkeys; ++i)
	test_search(dct, itor, keys[i].key, keys[i].value);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_FALSE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_EQUAL(*datum_location, keys[i].value);

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    CU_ASSERT_PTR_NOT_NULL(itor);
    char *last_key = NULL;
    unsigned n = 0;
    for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));

	char *key = dict_itor_key(itor);
	bool key_matched = false;
	for (unsigned i = 0; i < nkeys; ++i) {
	    if (keys[i].key == key) {
		CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
		key_matched = true;
		break;
	    }
	}
	CU_ASSERT_TRUE(key_matched);

	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
	    dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
	    }
	    last_key = dict_itor_key(itor);
	}

	++n;
    }
    CU_ASSERT_EQUAL(n, nkeys);
    last_key = NULL;
    n = 0;
    for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));

	char *key = dict_itor_key(itor);
	bool key_matched = false;
	for (unsigned i = 0; i < nkeys; ++i) {
	    if (keys[i].key == key) {
		CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
		key_matched = true;
		break;
	    }
	}
	CU_ASSERT_TRUE(key_matched);

	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
	    dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
	    }
	    last_key = dict_itor_key(itor);
	}

	++n;
    }
    CU_ASSERT_EQUAL(n, nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_FALSE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NOT_NULL(*datum_location);
	*datum_location = keys[i].alt;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i)
	test_search(dct, itor, keys[i].key, keys[i].alt);

    for (unsigned i = 0; i < nkeys; ++i) {
	test_search(dct, itor, keys[i].key, keys[i].alt);
	CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
	CU_ASSERT_TRUE(dict_verify(dct));

	CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
	for (unsigned j = 0; j <= i; ++j) {
	    test_search(dct, itor, keys[j].key, NULL);
	}
	for (unsigned j = i + 1; j < nkeys; ++j) {
	    test_search(dct, itor, keys[j].key, keys[j].alt);
	}
    }

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_clear(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    test_closest_lookup(dct, cl_infos, n_cl_infos);
    dict_itor_free(itor);
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_free(dct), nkeys);
}
コード例 #30
0
ファイル: log.c プロジェクト: jeffreywugz/keystack
void
log_rewrite(const char *filename, struct server *s) {
	
	struct client c;
	struct cmd *cm;
	int fd = open(filename, O_RDONLY), tmp_fd, ret;
	char tmp_filename[] = "/tmp/keystack-rewrite-file-XXXXXX";

	c.s = s;

	s->log = NULL;

	printf("Importing existing data..."); fflush(stdout);
	/* first, import log */
	while(1) {
		int ret;
		uint32_t sz;
		char *buffer;

		/* read size */
		ret = read(fd, &sz, sizeof(uint32_t));
		if(ret != sizeof(uint32_t)) {
			break;
		}
		sz = ntohl(sz);
		
		/* read message */
		buffer = malloc(sz);
		ret = read(fd, buffer, sz);
		if(ret != (int)sz) {
			break;
		}

		/* process message */
		cm = cmd_parse(buffer, sz);
		cmd_run(s, cm);
		cmd_free(cm);
	}
	close(fd);	
	printf("done.\nRewriting log file... "); fflush(stdout);

	/* second, write to tmp file. */
	tmp_fd = mkstemp(tmp_filename);
	if(tmp_fd == -1) {
		fprintf(stderr, "Failed to create tmp log file.\n");
		return;
	}

	dict_foreach(s->d, log_add_cb, &tmp_fd);
	printf("done (%ld keys).\nSyncing... ", dict_count(s->d)); fflush(stdout);
	fdatasync(tmp_fd);
	close(tmp_fd);

	printf("done.\nReplacing log file... "); fflush(stdout);
	ret = rename(tmp_filename, filename);
	if(ret != 0) {
		fprintf(stderr, "Failed to replace log file.\n");
		return;
	}

	/* third, replace log file with tmp file */
	printf("done.\nStarting to serve clients.\n");
}