コード例 #1
0
ファイル: hashtest.c プロジェクト: namananaman/HoneyPi
int main(void) {
  struct hashmap aa;
  struct hashmap *a= &aa;



  hashtable_initialize(a, 200, default_hash, 4);

  uint8_t key[4] = {1,2,3,4};
  int i;
  for (i= 0; i < 10; i++) {
    key[0] = i;
    hashtable_add(a,key,(void*)5);

  }

  hashtable_iter(a,iter);
  printf("deleting\n");

  for (i= 0; i < 5; i++) {
    key[0] = i;
    hashtable_delete(a,key);
  }

  hashtable_iter(a,iter);
  return 0;
}
コード例 #2
0
ファイル: loader.c プロジェクト: clarete/bitu
bitu_plugin_t *
bitu_plugin_ctx_find_for_cmdline (bitu_plugin_ctx_t *plugin_ctx,
                                  const char *cmdline)
{
  void *iter;
  bitu_plugin_t *plugin = NULL;

  /* Iterating over all loaded plugins and looking for one that matches
   * the received command line. The first one that matches will be
   * returned. */
  if ((iter = hashtable_iter (plugin_ctx->plugins)) == NULL)
    return NULL;
  do
    {
      plugin = hashtable_iter_value (iter);
      if (plugin && plugin->match && plugin->match (cmdline))
        return plugin;
    }
  while ((iter = hashtable_iter_next (plugin_ctx->plugins, iter)));

  /* It was not possible to match the command line in any plugin. We'll
   * have to parse the command line, get the plugin name and try to
   * execute it. */
  if (bitu_util_extract_params (cmdline, NULL, NULL, NULL) == TA_OK)
    if ((plugin = bitu_plugin_ctx_find (plugin_ctx, cmdline)) != NULL)
      return plugin;
  return NULL;
}
コード例 #3
0
ファイル: value.c プロジェクト: fizx/node-avro
void *json_object_iter(json_t *json)
{
    json_object_t *object;

    if(!json_is_object(json))
        return NULL;

    object = json_to_object(json);
    return hashtable_iter(&object->hashtable);
}
コード例 #4
0
ファイル: loader.c プロジェクト: clarete/bitu
ta_list_t *
bitu_plugin_ctx_get_list (bitu_plugin_ctx_t *plugin_ctx)
{
  void *iter;
  ta_list_t *ret = NULL;
  iter = hashtable_iter (plugin_ctx->plugins);
  if (iter == NULL)
    return NULL;
  do
    ret = ta_list_append (ret, hashtable_iter_key (iter));
  while ((iter = hashtable_iter_next (plugin_ctx->plugins, iter)));
  return ret;
}
コード例 #5
0
ファイル: app.c プロジェクト: clarete/bitu
static char *
cmd_env (bitu_app_t *app, char **TA_UNUSED(params), int num_params)
{
  void *iter;
  char *error, *val, *tmp, *pos, *list = NULL;
  size_t val_size, current_size = 0, full_size = 0, step = 256, lastp = 0;
  if ((error = _validate_num_params ("env", 0, num_params)) != NULL)
    return error;
  iter = hashtable_iter (app->environment);
  if (iter == NULL)
    return NULL;
  do
    {
      val = hashtable_iter_key (iter);
      val_size = strlen (val);
      current_size += val_size + 1;
      if (full_size < current_size)
        {
          full_size += step;
          if ((tmp = realloc (list, full_size)) == NULL)
            {
              free (list);
              return NULL;
            }
          else
            list = tmp;
        }
      pos = list + lastp;
      memcpy (pos, val, val_size);
      memcpy (pos+val_size, "\n", 1);
      lastp += val_size + 1;
    }
  while ((iter = hashtable_iter_next (app->environment, iter)));
  list[current_size-1] = '\0';
  return list;
}
コード例 #6
0
ファイル: map.c プロジェクト: h31h31/libpomelo
void *pc_map_iter(pc_map_t *map) {
  return hashtable_iter(&map->table);
}
コード例 #7
0
ファイル: app.c プロジェクト: clarete/bitu
static char *
cmd_list (bitu_app_t *app, char **params, int num_params)
{
  char *action, *ret;
  char *error;
  if ((error = _validate_num_params ("list", 1, num_params)) != NULL)
    return error;

  ret = NULL;
  action = params[0];
  if (strcmp (action, "plugins") == 0)
    {
      ta_list_t *plugins, *tmp;
      char *val, *tmp_val, *current_pos_str;
      size_t val_size, current_pos, full_size = 0;
      plugins = bitu_plugin_ctx_get_list (app->plugin_ctx);
      for (tmp = plugins; tmp; tmp = tmp->next)
        {
          val = tmp->data;

          /* This +1 means the \n at the end of each line. */
          val_size = strlen (val) + 1;

          /* Remembering current end of the full string. */
          current_pos = full_size;
          full_size += val_size;

          if ((tmp_val = realloc (ret, full_size)) == NULL)
            {
              free (ret);
              return NULL;
            }
          else
            ret = tmp_val;

          current_pos_str = ret + current_pos;
          memcpy (current_pos_str, val, val_size);
          memcpy (current_pos_str + val_size - 1, "\n", 1);
        }

      /* Removing the last \n. It is not needed in the end of the
       * string */
      if (ret != NULL)
        {
          memcpy (ret + full_size - 1, "\0", 1);
          ta_list_free (plugins);
        }
    }
  else if (strcmp (action, "commands") == 0)
    {
      void *iter;
      char *val, *tmp, *current_pos_str;
      size_t val_size, current_pos, full_size = 0;
      iter = hashtable_iter (app->commands);
      if (iter == NULL)
        return NULL;
      do
        {
          val = hashtable_iter_key (iter);

          /* This +1 means the \n at the end of each line. */
          val_size = strlen (val) + 1;

          /* Remembering current end of the full string. */
          current_pos = full_size;
          full_size += val_size;

          if ((tmp = realloc (ret, full_size)) == NULL)
            {
              free (ret);
              return NULL;
            }
          else
            ret = tmp;

          current_pos_str = ret + current_pos;
          memcpy (current_pos_str, val, val_size);
          memcpy (current_pos_str + val_size - 1, "\n", 1);
        }
      while ((iter = hashtable_iter_next (app->commands, iter)));

      /* Removing the last \n. It is not needed in the end of the string */
      ret[full_size-1] = '\0';
    }
  else
    ret = strdup ("Possible values are `commands' or `plugins'");
  return ret;
}
コード例 #8
0
ファイル: hashtable_test.c プロジェクト: wooxo/librsync
/* Test driver for hashtable. */
int main(int argc, char **argv)
{
    /* Test key_hashtable instance. */
    hashtable_t *kt;
    hashtable_iter_t ki;
    key_t k1, k2;

    key_init(&k1, 1);
    key_init(&k2, 2);
    assert((kt = key_hashtable_new(16)) != NULL);
    assert(key_hashtable_add(kt, &k1) == &k1);
    assert(key_hashtable_find(kt, &k1) == &k1);
    assert(key_hashtable_find(kt, &k2) == NULL);
    assert(key_hashtable_iter(&ki, kt) == &k1);
    assert(key_hashtable_next(&ki) == NULL);

    /* Test hashtable instance. */
    hashtable_t *t;
    entry_t entry[256];
    entry_t e;
    match_t m;
    int i;

    entry_init(&e, 0);
    for (i = 0; i < 256; i++)
        entry_init(&entry[i], i);

    /* Test hashtable_new() */
    t = hashtable_new(256);
    assert(t->size == 512);
    assert(t->count == 0);
    assert(t->etable != NULL);
    assert(t->ktable != NULL);

    /* Test hashtable_add() */
    assert(hashtable_add(t, &e) == &e); /* Added duplicated copy. */
    assert(hashtable_add(t, &entry[0]) == &entry[0]);   /* Added duplicated instance. */
    for (i = 0; i < 256; i++)
        assert(hashtable_add(t, &entry[i]) == &entry[i]);
    assert(t->count == 258);

    /* Test hashtable_find() */
    match_init(&m, 0);
    assert(hashtable_find(t, &m) == &e);        /* Finds first duplicate added. */
    assert(m.value == m.source);        /* match_cmp() updated m.value. */
    for (i = 1; i < 256; i++) {
        match_init(&m, i);
        assert(hashtable_find(t, &m) == &entry[i]);
        assert(m.value == m.source);    /* match_cmp() updated m.value. */
    }
    match_init(&m, 256);
    assert(hashtable_find(t, &m) == NULL);      /* Find missing entry. */
    assert(m.value == 0);       /* match_cmp() didn't update m.value. */
#ifndef HASHTABLE_NSTATS
    assert(t->find_count == 257);
    assert(t->match_count == 256);
    assert(t->hashcmp_count >= 256);
    assert(t->entrycmp_count >= 256);
    hashtable_stats_init(t);
    assert(t->find_count == 0);
    assert(t->match_count == 0);
    assert(t->hashcmp_count == 0);
    assert(t->entrycmp_count == 0);
#endif

    /* Test hashtable iterators */
    entry_t *p;
    hashtable_iter_t iter;
    int count = 0;
    for (p = hashtable_iter(&iter, t); p != NULL; p = hashtable_next(&iter)) {
        assert(p == &e || (&entry[0] <= p && p <= &entry[255]));
        count++;
    }
    assert(count == 258);
    hashtable_free(t);

    return 0;
}
コード例 #9
0
ファイル: ai.c プロジェクト: perepujal/tuxhistory
th_path *ai_shortes_path(int player, int unit, th_point source, th_point goal)
{
    int i, a;
    int count;

    th_vector vector;
    th_point pt;
    th_point *solution;
    th_path *path;

    bheap *open;
    struct hashtable *closed;
    bheap_node *e;
    bheap_node *p;
    bheap_node *n;
    
    // Are the source and goal point valid?
    if( source.x >= 0 && source.x <= x_tildes   &&
        source.y >= 0 && source.y <= y_tildes   &&
        goal.x >= 0 && goal.x <= x_tildes       &&
        goal.y >= 0 && goal.y <= y_tildes       )
    {
        
        // TODO: Actual way to store binary heap nodes is a bad idea. We need make 
        //       te code to use the dinamic strucures in bheap_add.
        e = (bheap_node *)malloc(1000 *  x_tildes * y_tildes * sizeof(bheap_node));
        if(e == NULL)
            return NULL;

        count = 0;
        i = 0;

        // Creating open and closed lists
        open = bheap_init(x_tildes * y_tildes);
        closed = make_hashtable(hashtable_default_hash, x_tildes * y_tildes);

        // Defining the initial node 
        sprintf(e[count].id, "%03d%03d", source.x, source.y);
        //printf("====================== A* STARTING... =====================\n");
        //printf("Element id to store: %s\n", e[count].id);
        e[count].deph = 0;
        e[count].point = source;
        e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y);
        e[count].g = 0;
        e[count].val = e[count].g + e[count].h;
        e[count].index = count;
        e[count].parent = NULL;
        
        // Insert the initial node to the open list
        if(!bheap_add(open, &e[count]))
        {
            printf("Coudn't add element to the open list!\n");
            return NULL;
        }
        //bheap_print(open);

        while(open->count >= 0)
        {
            //printf("********** New Loop Cycle\n");
            // Remove the lowest element in open list
            // and add it to the closed list
            n = bheap_del(open);
            if(n == NULL)
            {
                printf("Error deleting the priority element from open list!\n");
                return NULL;
            }
            //printf("Removed id: %s\n", n->id);
            //bheap_print(open);
            
            //printf("Element id to store in loop: %s, index: %d\n", n->id, n->index);

            if(!hashtable_add(closed, e[n->index].id, &e[n->index]))
            {
                printf("Error adding to hashtable!\n");
                return NULL;
            }
            //hashtable_iter(closed, hashtable_default_hash);
            

            //Is this element the goal?
            if(n->point.x == goal.x && n->point.y == goal.y)
            {
                printf("Solution deph is %d\n", n->deph);
                solution = (th_point *)malloc(n->deph * sizeof(th_point));
                if(!solution)
                    return NULL;
                path = (th_path *)malloc(sizeof(th_path));
                if(!path)
                    return NULL;

                i=0;

                while(n->parent)
                {
                    printf("(%d,%d)\n",n->point.x, n->point.y);
                    solution[i] = n->point;
                    n = n->parent;
                    i++;
                } 
                
                path->path = solution;
                path->size = i - 1;

                free_hashtable(closed);
                bheap_free(open);
                FREE(e);
                return path;
            }

            //printf("This element is not the goal!.. Trying...\n");

            //For each valid move for n
            for(a = 0; a < NUM_DIRS; a++)
            {
                vector = get_vector(n->point, a);
                if(vector.x != -2 && vector.y != -2)
                {
                    //printf("Vector is valid... \n");
                    //printf("For %d direction tile in (%d,%d) is valid?\n", a, n->point.x, n->point.y);

                    pt.x = vector.x + n->point.x;
                    pt.y = vector.y + n->point.y;
                    if(ai_valid_tile(player, unit, pt))
                    {

                        //printf("Adding direction %d to open list!\n", a);

                        //New valid element
                        count++;
                        e[count].deph = n->deph + 1;
                        e[count].point = pt;
                        memset(e[count].id, 0, 7);
                        sprintf(e[count].id, "%03d%03d", pt.x, pt.y);
                        e[count].index = count;
                        e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y);
                        if( a == ISO_N || 
                            a == ISO_S ||
                            a == ISO_W ||
                            a == ISO_E)
                            e[count].g = n->g + 10; 
                        else
                            e[count].g = n->g + 14;
                        e[count].val = e[count].g + e[count].h; // F = G + H
                        e[count].parent = n;
                        //printf("Actual id: %s, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h,
                        //       e[count].g, e[count].val, e[count].deph);

                        //Is this element in closed list?
                        if((p = hashtable_lookup(closed, e[count].id)) != NULL)
                        {
                            //printf("P exists in cloded list!\n");
                            if(p->val > e[count].val)
                            {
                                if(!hashtable_remove(closed, p->id))
                                {
                                    printf("Error ocurred while trying to remove key in hashtable!\n");
                                    hashtable_iter(closed, hashtable_default_hash);
                                    return NULL;
                                }
                                //else
                                //{
                                    //printf("Removes OK, let's check integrity!\n");
                                    //hashtable_iter(closed, hashtable_default_hash);
                                //}
                                if(!bheap_add(open, p))
                                {
                                    printf("Error ocurred while adding a element to open list\n");
                                    return NULL;
                                }
                                //printf("Succesfully removed from closed list and added to open list\n");
                            }
                        }   
                        else
                        {
                            //printf("P doesn't exist in closed list!\n");
                            if(!bheap_add(open, &e[count]))
                            {
                                printf("Error ocurred while adding a new element to open list\n");
                                return NULL;
                            }
                        }
                        //bheap_print(open);
                    }
                }
            }
        }
        free_hashtable(closed);
        bheap_free(open);
        FREE(e);
    }
    else
    {
        printf("Bad point references : Origin(%d, %d) Dest(%d, %d)\n", source.x, source.y, goal.x, goal.y);
    }
}