Beispiel #1
0
static obj_ptr _map_keys(obj_ptr arg, obj_ptr env)
{
    obj_ptr      res = NIL;
    map_iter_t   i;
    map_node_ptr n;

    if (NMAPP(arg))
        return MKERROR(MKSTRING("Expected a map in map-keys"), arg);

    i = map_get_iter(&MAP(arg));
    for (n = map_next(&i); n; n = map_next(&i))
        res = CONS(n->key, res);

    return res;
}
Beispiel #2
0
int main()
{
	map *m;
	string *k;
	string *v;
	string *q;
	map_iterator *n;
	pair *t;

	m = map_init(stcmp);
	q = string_init_cstring("");
	k = string_init();
	string_read_line(k);

	while(string_cmp(k, q))
	{
		v = string_init();
		string_read_line(v);
		map_add(m, k, v);

		k = string_init();
		string_read_line(k);
	}

	k = string_free(k);

	/* Iterate through map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_print(t->first);
		printf(" => ");
		string_println(t->second);
	}

	/* Free map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_free(t->first);
		string_free(t->second);
	}

	string_free(q);
	m = map_free(m);

	return 0;
}
Beispiel #3
0
void map_print(map *map, int t, int x0, int y0, int x1, int y1) {
	int width  = x1 - x0 + 1;
	int height = y1 - y0 + 1;
	
	bool arr[width][height];
	memset(arr, 0, width*height);
	
	int x, y;
	
	// collect living cells
	map_restart(map, t);
	while (map_next(map, t, &x, &y)) {
		if (x >= x0 && x <= x1 &&
			y >= y0 && y <= y1   )
		{
			arr[x-x0][y-y0] = true;
		}
	}
	
	// print cells
	printf("t=%d x=%d-%d y=%d-%d:\n", t, x0, x1, y0, y1);

	for (int y = 0; y < height; ++y) {
		for (int x = 0; x < width; ++x) {
			if (arr[x][y])
				printf("X");
			else
				printf(" ");
		}
		
		printf("\n");
	}
}
Beispiel #4
0
Datei: gc.c Projekt: CRogers/obc
/* map_next -- skip over a map item */
static int *map_next(int *p) {
    if (*p % 4 == 0)
        return p+1;		/* A pointer offset */

    switch (*p) {
    case GC_BASE:
    case GC_MAP:
        return p+2;

    case GC_REPEAT:
    case GC_FLEX:
        p += 4;
        while (*p != GC_END) p = map_next(p);
        return p+1;

    case GC_BLOCK:
        return p+3;

    case GC_END:
        return p+1;

    default:
        panic("*bad map code %d", *p);
        return NULL;
    }
}
Beispiel #5
0
static void test_map_stack(void) {
    Map *m1 = make_map(NULL);
    map_put(m1, "x", (void *)1);
    map_put(m1, "y", (void *)2);
    assert_int(1, (int)(intptr_t)map_get(m1, "x"));

    Map *m2 = make_map(m1);
    assert_int(1, (int)(intptr_t)map_get(m2, "x"));
    map_put(m2, "x", (void *)3);
    assert_int(3, (int)(intptr_t)map_get(m2, "x"));
    assert_int(1, (int)(intptr_t)map_get(m1, "x"));

    MapIter *iter = map_iter(m2);
    assert_string("x", map_next(iter, NULL));
    assert_string("y", map_next(iter, NULL));
    assert_null(map_next(iter, NULL));
}
Beispiel #6
0
int map_count(map *map, int t) {
	int sum = 0;
	
	int x, y;
	map_restart(map, t);
	while (map_next(map, t, &x, &y))
		++sum;
	
	return sum;
}
Beispiel #7
0
static void test_map(void) {
    Map *m = make_map(NULL);
    assert_null(map_get(m, "abc"));

    // Insert 10000 values
    for (int i = 0; i < 10000; i++) {
        char *k = format("%d", i);
        map_put(m, k, (void *)(intptr_t)i);
        assert_int(i, (int)(intptr_t)map_get(m, k));
    }

    // Insert again
    for (int i = 0; i < 1000; i++) {
        char *k = format("%d", i);
        map_put(m, k, (void *)(intptr_t)i);
        assert_int(i, (int)(intptr_t)map_get(m, k));
    }

    // Verify that the iterator iterates over all the elements
    {
	bool x[10000];
	for (int i = 0; i < 10000; i++)
	    x[i] = 0;
	MapIter *iter = map_iter(m);
	void *v;
	char *k = map_next(iter, &v);
	for (; k; k = map_next(iter, &v)) {
	    int i = (intptr_t)v;
	    x[i] = 1;
	}
	for (int i = 0; i < 10000; i++)
	    assert_true(x[i] == 1);
    }

    // Remove them
    for (int i = 0; i < 10000; i++) {
        char *k = format("%d", i);
        assert_int(i, (intptr_t)map_get(m, k));
        map_remove(m, k);
        assert_null(map_get(m, k));
    }
}
void attribute_destroy_recursive( container *attributes )
{
    iterator	it = map_begin(attributes);

    for (; it.valid; it=map_next(it))
	{
	    destroy(pair(map_val(it)).second.C);
	    attribute_destroy_recursive( pair(map_val(it)).first.C );
	}

    destroy(attributes);
}
/* a function that prints out the contents of the map */
void print_map(map *m)
{
	/* an iterator for iterating through all map entries */
	map_iterator i;

	/* the method `int map_size(map *) returns the current number of entries */
	printf("map (size: %d) {", map_size(m));

	/* just like lists, map is iterated with _iterate and _next */
	for (i=map_iterate(m); map_next(m, &i); ) {
		/* map_key_at and map_value_at return the key and value, respectively */
		printf(" %s:%d,", map_key_at(m, i), map_value_at(m, i));
	}
	printf("\b }\n");
}
Beispiel #10
0
static void *
_findnext(struct map *m, struct map_op * op)
{
	if (op->value == NULL) {
		return _findfirst(m,op);
	}
	struct node *v = map_next(m, op->value);
	op->value = v;
	if (v == NULL) {
		op->key.p = NULL;
		return NULL;
	}
	else {
		op->key.p = v->key;
	}
	return v->value;
}
Beispiel #11
0
inline void map_print( container *C, value a )
{
    int		i=0;
    iterator	it = map_begin(a.C);

    printf("(");
    for (; it.valid; it=map_next(it))
    {
        if (i==0) i++;
        else printf(" ");

        printv(((map_container_priv*)C->priv)->Key, map_key(it));
        printf(":");
        printv(((map_container_priv*)C->priv)->Data, map_val(it));
    }
    printf(")");
}
Beispiel #12
0
Datei: map.c Projekt: jash16/algo
int main() {
    map *m = map_create(&demo_type);
    assert(m != NULL);
    char *key = NULL;
    char *val = NULL;
    srand(time(NULL));
    int i = 0, j = 0;
    //key = malloc()
//    map_insert(m, "hellofjwoefjoewjfo", "world");
    for (i = 0; i < 100; i++) {
        key = malloc(sizeof(char) * 16);
        val = malloc(sizeof(char) * 16);
        for (j = 0; j < 15; j ++) {
            key[j] = 'a' + rand_range(0, 25);
        }   
        for (j = 0; j < 15; j++) {
            val[j] = 'a' + rand_range(0, 25);
        }   
        key[15] = '\0';
        val[15] = '\0';
        map_insert(m, key, val);
    }
    printf("map_size: %d\n", map_size(m));
    struct timeval start, end;
    gettimeofday(&start, NULL);
    void *n = map_find(m, "hellofjwoefjoewjfo");
    gettimeofday(&end, NULL);
    assert(n == NULL);
//    printf("%s\n", (char *)n);
    int waist = (end.tv_sec - start.tv_sec) * 100000 + (end.tv_usec - start.tv_usec);
    printf("waist time: %d, start:(sec: %d, usec: %d), end(sec: %d, usec: %d)\n", waist,
            start.tv_sec, start.tv_usec, end.tv_sec, end.tv_usec);

    map_iterator *mi = map_get_iterator(m);
    assert(mi != NULL);
    pair *data;
    while ((data = map_next(mi))) {
        printf("key: %s => val: %s\n", (char *)(((pair *)data)->first), (char *)(((pair*)data)->second));
    }
    map_release_iterator(mi);
    map_release(m);
}
void attribute_count_print( container *attributes, int attrib_count, int indent )
{
    iterator		it = map_begin(attributes);

    for (; it.valid; it=map_next(it))
	{
	    int		i;

	    for (i=0; i<indent; i++) printf(" ");
	    printf("%s\n", (char*)map_key(it).ptr);
	    /*
	    for (i=0; i<attrib_count; i++)
		{
		    if (i>0) printf(", ");
		    printf("%i", ((int*)pair(map_val(it)).second.ptr)[i]);
		}
	    printf(")\n");
	    */

	    attribute_count_print(pair(map_val(it)).first.C, attrib_count, indent+2);
	}
}
int _attribute_count_hits_(container *count, int filter, int group_filter_id)
{
    if (count==NULL) return 0;

    int		i;
    int		len = group_filter_id+1;
    int		alle = 0;

    for (i=0; i<len; i++) alle+= 1<<i;

    int		inverse = alle - filter;
    //printf("filter:");
    //for (i=0; i<len; i++) printf("%c", (inverse & (1<<i) ? '1':'0'));
    //printf("\n");

    int		hits = 0;
    int		__total__ = 0;
    iterator	it;

    it = map_begin(count);
    for (; it.valid; it=map_next(it))
	{
	    //printf("       ");
	    //for (i=0; i<len; i++) printf("%c", ((alle - map_key(it).i) & (1<<i) ? '1':'0'));

	    __total__++;
	    if (((alle - map_key(it).i) & inverse) == 0)
		{
		    hits+= map_val(it).i;
		    //printf("  <count>\n");
		}
	    //else printf("  <----->\n");
	}
    //printf("       match:%i/%i\n", hits, __total__);

    return hits;
}
struct _attr_ret_ _attribute_add_children_(container *attributes, container *attr_query, container *hide, container *A, int container_id)
{
    int		i, j;
    container	*list_items = vector_container( ptr_container() );
    iterator	it_m1 = map_begin(attributes);
    int		has_selected_child = 0;

    for (; it_m1.valid; it_m1=map_next(it_m1))
	{
	    vector_pushback(attr_query, (char*)map_key(it_m1).ptr);

	    int		is_hidden = 0;
	    for (i=0; i<vector_size(hide) && !is_hidden; i++)
		{
		    container	*hide_elem = vector_get(hide, i).C;

		    for (j=0; j<vector_size(hide_elem); j++)
			{
			    if (strcasecmp(vector_get(attr_query,j).ptr, vector_get(hide_elem,j).ptr)) break;
			}

		    if (j>=vector_size(hide_elem)) is_hidden = 1;
		}

	    if (is_hidden)
		{
		    vector_remove_last(attr_query);
		    continue;
		}

	    struct _attr_tree_	*this_item = _attribute_tree_malloc_();
	    int val = _attribute_is_selected_(A, attr_query, container_id);
	    if (val>=0) this_item->selected = val;

	    if (map_size(pair(map_val(it_m1)).first.ptr) > 0)
		{
		    struct _attr_ret_ ret = _attribute_add_children_(pair(map_val(it_m1)).first.ptr, attr_query, hide, A, container_id);
		    if (ret.selected_descendant)
			{
			    this_item->selected_descendant = 1;
			    has_selected_child = 1;
			}
		    this_item->children = ret.C;
		    this_item->query_param = vector_container( string_container() );
		    for (j=0; j<vector_size(attr_query); j++)
			vector_pushback(this_item->query_param, vector_get(attr_query, j).ptr);
		    //this_item->name = ;
		    this_item->count = pair(map_val(it_m1)).second.ptr;
		}
	    else
		{
		    this_item->query_param = vector_container( string_container() );
		    for (j=0; j<vector_size(attr_query); j++)
			vector_pushback(this_item->query_param, vector_get(attr_query, j).ptr);
		    //this_item->name = ;
		    this_item->count = pair(map_val(it_m1)).second.ptr;
		}

	    vector_remove_last(attr_query);

	    if (this_item->name == NULL && this_item->query_param == NULL && this_item->count == NULL && this_item->children == NULL)
		{
		    free(this_item);
		}
	    else
		{
		    vector_pushback(list_items, this_item);
		    if (this_item->selected >= 0) has_selected_child = 1;
		}
	}

    struct _attr_ret_	ret;
    ret.C = list_items;
    ret.selected_descendant = has_selected_child;
    return ret;
}
Beispiel #16
0
/**
 * Lookup tree_node with the key and search flag in the map.
 *
 * @tmap map data.
 * @key key
 * @search_flag search flag. (MAP_SEARCH_*).
 *
 * @return struct tree_node if found, or NULL.
 */
static struct tree_node* map_lookup_node_detail(const struct map *tmap, u64 key, int search_flag)
{
    struct rb_node *node;
    struct tree_node *t = NULL;

    ASSERT_TREEMAP(tmap);
    node = tmap->root.rb_node;

    if (search_flag == MAP_SEARCH_BEGIN ||
            search_flag == MAP_SEARCH_END) {
        return NULL;
    }

    /* Travserse tree. */
    while (node) {
        t = container_of(node, struct tree_node, node);

        if (key < t->key) {
            node = node->rb_left;
        } else if (key > t->key) {
            node = node->rb_right;
        } else {
            ASSERT(key == t->key);
            if (search_flag == MAP_SEARCH_EQ ||
                    search_flag == MAP_SEARCH_LE ||
                    search_flag == MAP_SEARCH_GE) {
                return t;
            } else {
                break;
            }
        }
    }
    if (!t) { /* Empty tree. */
        return NULL;
    }

    switch (search_flag) {
    case MAP_SEARCH_EQ:
        ASSERT(t->key != key);
        return NULL;

    case MAP_SEARCH_LT:
    case MAP_SEARCH_LE:
        if (t->key == key) {
            ASSERT(search_flag == MAP_SEARCH_LT);
            t = map_prev(t);
            ASSERT(!t || t->key < key);
            return t;

        } else if (t->key < key) {
            ASSERT(!map_next(t) || (map_next(t))->key > key);
            return t;
        } else {
            t = map_prev(t);
            ASSERT(!t || t->key < key);
            return t;
        }
        break;

    case MAP_SEARCH_GT:
    case MAP_SEARCH_GE:

        if (t->key == key) {
            ASSERT(search_flag == MAP_SEARCH_GT);
            t = map_next(t);
            ASSERT(!t || t->key > key);
            return t;

        } else if (t->key < key) {
            t = map_next(t);
            ASSERT(!t || t->key > key);
            return t;
        } else {
            ASSERT(!map_prev(t) || (map_prev(t))->key < key);
            return t;
        }
        break;
    default:
        BUG();
    }
    return NULL;
}
Beispiel #17
0
rt_private EIF_REFERENCE duplicate(EIF_REFERENCE source, EIF_REFERENCE enclosing, rt_uint_ptr offset)
/* Object to be duplicated */
/* Object where attachment is made */
/* Offset within enclosing where attachment is made */
{
    /* Duplicate the source object (shallow duplication) and attach the freshly
     * allocated copy at the address pointed to by receiver, which is protected
     * against GC movements. Returns the address of the cloned object.
     */

    RT_GET_CONTEXT
    union overhead *zone;			/* Malloc info zone */
    uint16 flags;					/* Object's flags */
    rt_uint_ptr size;					/* Object's size */
    EIF_REFERENCE *hash_zone;				/* Hash table entry recording duplication */
    EIF_REFERENCE clone;					/* Where clone is allocated */

    REQUIRE("source not null", source);
    REQUIRE("enclosing not null", enclosing);

    zone = HEADER(source);			/* Where eif_malloc stores its information */
    flags = zone->ov_flags;			/* Eiffel flags */

    if (flags & EO_SPEC) {
        size = RT_SPECIAL_VISIBLE_SIZE(source);
    } else {
        size = EIF_Size(zone->ov_dtype);
    }

    clone = eif_access(map_next());				/* Get next stacked object */
    *(EIF_REFERENCE *)  (enclosing + offset) = clone;	/* Attach new object */
    hash_zone = hash_search(&hclone, source);	/* Get an hash table entry */
    CHECK("Enough space", HEADER(clone)->ov_size >= size);
    memcpy (clone, source, size);				/* Block copy */
    *hash_zone = clone;					/* Fill it in with the clone address */

#ifdef ISE_GC
    /* Once the duplication is done, the receiving object might refer to a new
     * object (a newly allocated object is always new), and thus aging tests
     * must be performed, to eventually remember the enclosing object of the
     * receiver.
     */

    flags = HEADER(enclosing)->ov_flags;

    if (!(flags & EO_OLD))				/* Receiver is a new object */
        return clone;					/* No aging tests necessary */

    if (HEADER (clone)->ov_flags & EO_OLD)		/* Old object with old reference.*/
        return clone;					/* No further action necessary */

    CHECK ("Object is old and has reference to new one",
           (flags & EO_OLD) && !(HEADER (clone)->ov_flags & EO_OLD));

    if (flags & EO_REM)					/* Old object is already remembered */
        return clone;					/* No further action necessary */
    else
        erembq(enclosing);				/* Then remember the enclosing object */

#endif /* ISE_GC */

    return clone;
}