예제 #1
0
파일: hashmap.c 프로젝트: rumblesan/bclib
int hashmap_traverse(Hashmap *map, Hashmap_traverse_cb traverse_cb) {
    int i = 0;
    int j = 0;
    int rc = 0;

    for (i = 0; i < darray_count(map->buckets); i += 1) {
        DArray *bucket = darray_get(map->buckets, i);
        if (bucket) {
            for (j = 0; j < darray_count(bucket); j += 1) {
                HashmapNode *node = darray_get(bucket, j);
                rc = traverse_cb(node);
                if (rc != 0) return rc;
            }
        }
    }

    return 0;
}
예제 #2
0
파일: pbc.cpp 프로젝트: mahnushm/MpcLib
static val_ptr v_def_call(val_ptr v, tree_ptr t) {
	int i;
	const char* name = ((tree_ptr)darray_at(v->def->child, 0))->id;
	darray_ptr parm = ((tree_ptr)darray_at(v->def->child, 1))->child;
	int n = darray_count(parm);
	if (1 + n != darray_count(t->child)) {
		return val_new_error("%s: wrong number of arguments", name);
	}
	for (i = 0; i < n; i++) {
		const char *id = ((tree_ptr)darray_at(parm, i))->id;
		val_ptr v1 = tree_eval((tree_ptr)darray_at(t->child, i));
		// TODO: Stack frames for recursion.
		symtab_put(tab, v1, id);
	}
	// Evaluate function body.
	darray_ptr a = ((tree_ptr)darray_at(v->def->child, 2))->child;
	darray_forall(a, eval_stmt);
	return NULL;
}
예제 #3
0
파일: hashmap.c 프로젝트: rumblesan/bclib
void hashmap_destroy(Hashmap *map) {
    int i = 0;
    int j = 0;

    if (map) {
        if (map->buckets) {

            for (i = 0; i < darray_count(map->buckets); i += 1) {
                DArray *bucket = darray_get(map->buckets, i);
                if (bucket) {
                    for (j = 0; j < darray_count(bucket); j += 1) {
                        free(darray_get(bucket, j));
                    }
                    darray_destroy(bucket);
                }
            }

            darray_destroy(map->buckets);
        }
        free(map);
    }
}
예제 #4
0
파일: pbc.c 프로젝트: n0htyp/ABBE
static val_ptr v_builtin(val_ptr v, tree_ptr t) {
  fun_ptr fun = v->fun;
  int n = fun->arity;
  if (1 + n != darray_count(t->child)) {
    return val_new_error("%s: wrong number of arguments", fun->name);
  }
  val_ptr arg[n];
  int i;
  for(i = 0; i < n; i++) {
    arg[i] = tree_eval(darray_at(t->child, i));
    if (fun->sig[i] && arg[i]->type != fun->sig[i]) {
      return val_new_error("%s: argument %d type mismatch", fun->name, i + 1);
    }
  }
  return fun->run(arg);
}
예제 #5
0
파일: pbc.cpp 프로젝트: mahnushm/MpcLib
static val_ptr eval_list(tree_ptr t) {
	element_ptr e = NULL;
	int n = darray_count(t->child);
	int i;
	for (i = 0; i < n; i++) {
		val_ptr x = tree_eval((tree_ptr)darray_at(t->child, i));
		// TODO: Also check x is a multiz.
		if (v_error == x->type) {
			return x;
		}
		if (v_elem != x->type) {
			return val_new_error("element expected in list");
		}
		if (!i) e = multiz_new_list(x->elem);
		else multiz_append(e, x->elem);
	}
	return val_new_element(e);
}
예제 #6
0
파일: pbc.cpp 프로젝트: mahnushm/MpcLib
static val_ptr v_builtin(val_ptr v, tree_ptr t) {
	fun_ptr fun = v->fun;
	int n = fun->arity;
	if (1 + n != darray_count(t->child)) {
		return val_new_error("%s: wrong number of arguments", fun->name);
	}

#ifdef _MSC_VER		// for VC++ compatibility
	val_ptr arg[MAX_LIMBS];
#else
	val_ptr arg[n];
#endif

	int i;
	for (i = 0; i < n; i++) {
		arg[i] = tree_eval((tree_ptr)darray_at(t->child, i));
		if (fun->sig[i] && arg[i]->type != fun->sig[i]) {
			return val_new_error("%s: argument %d type mismatch", fun->name, i + 1);
		}
	}
	return fun->run(arg);
}
예제 #7
0
파일: zdd.c 프로젝트: blynn/zddfun
uint32_t zdd_intersection() {
  vmax_check();
  if (darray_count(stack) == 0) return 0;
  if (darray_count(stack) == 1) return (uint32_t) darray_last(stack);
  uint32_t z0 = (uint32_t) darray_at(stack, darray_count(stack) - 2);
  uint32_t z1 = (uint32_t) darray_remove_last(stack);
  struct node_template_s {
    uint16_t v;
    // NULL means this template have been instantiated.
    // Otherwise it points to the LO template.
    memo_it lo;
    union {
      // Points to HI template when template is not yet instantiated.
      memo_it hi;
      // During template instantiation we set n to the pool index
      // of the newly created node.
      uint32_t n;
    };
  };
  typedef struct node_template_s *node_template_ptr;
  typedef struct node_template_s node_template_t[1];

  node_template_t top, bot;
  bot->v = 0;
  bot->lo = NULL;
  bot->n = 0;
  top->v = 1;
  top->lo = NULL;
  top->n = 1;

  // Naive implementation with two tries. One stores templates, the other
  // unique nodes. See Knuth for how to meld using just memory allocated
  // for a pool of nodes.
  memo_t tab;
  memo_init(tab);

  memo_it insert_template(uint32_t k0, uint32_t k1) {
    uint32_t key[2];
    // Taking advantage of symmetry of intersection appears to help a tiny bit.
    if (k0 < k1) {
      key[0] = k0;
      key[1] = k1;
    } else {
      key[0] = k1;
      key[1] = k0;
    }
    memo_it it;
    int just_created = memo_it_insert_u(&it, tab, (void *) key, 8);
    if (!just_created) return it;
    if (!k0 || !k1) {
      memo_it_put(it, bot);
      return it;
    }
    if (k0 == 1 && k1 == 1) {
      memo_it_put(it, top);
      return it;
    }
    node_ptr n0 = pool[k0];
    node_ptr n1 = pool[k1];
    if (n0->v == n1->v) {
      node_template_ptr t = malloc(sizeof(*t));
      t->v = n0->v;
      if (n0->lo == n0->hi && n1->lo == n0->hi) {
	t->lo = t->hi = insert_template(n0->lo, n1->lo);
      } else {
	t->lo = insert_template(n0->lo, n1->lo);
	t->hi = insert_template(n0->hi, n1->hi);
      }
      memo_it_put(it, t);
      return it;
    } else if (n0->v < n1->v) {
      memo_it it2 = insert_template(n0->lo, k1);
      memo_it_put(it, memo_it_data(it2));
      return it2;
    } else {
      memo_it it2 = insert_template(k0, n1->lo);
      memo_it_put(it, memo_it_data(it2));
      return it2;
    }
  }
예제 #8
0
파일: csv2json.c 프로젝트: ttxie/comm
int main(int argc, char **argv)
{
        FILE *fd, *fdw;
        char *orgbuf,*buf, *p, *ptr, *tt;
        int size, n, j;
        struct darray_s *darr, *darr_line; 
        struct darray_s **item;
	char filename[50], outfile[50];

        if (argc < 2) {
                printf("format error: csv2json {filename}\n");
                exit(0);
        }
        strcpy(filename, argv[1]);
        strcpy(outfile, argv[1]);
        strcat(outfile, ".json");

        printf("filename:%s, outfile:%s\n", filename, outfile);

        fd = fopen(filename, "r+");
        if (fd == NULL) {
                perror("open input file err");        
                exit(0);
        }
        //fdw = fopen(strcat(argv[1],".json"), "w");
        fdw = fopen(outfile, "w");
        if (fdw == NULL) {
                perror("open write file err");        
                exit(0);
        }
        
/*   load data into dynamic array  */
        orgbuf = buf = (char*)calloc(1, LINE);
        darr = (u_char *)darray_calloc(linecount_fd(fd), sizeof(buf));

        while (fgets(buf, LINE, fd)) {
        //        printf("buf:%s\n", buf);
                darr_line = darray_calloc(strcount(buf, ',')+1, LINE);
                //printf("%p\n", darr_line);

                tt = darray_pushback(darr);
                //printf("1 values: %p\n", tt);
                memcpy(tt, &darr_line, sizeof(darr_line));
                
                trim(buf);
                while (p = strchr(buf, ',')) {
                        size = p-buf;
                        ptr = darray_pushback(darr_line); 
                        //printf("2 buf:%s, size:%d, ptr:%p\n", buf, size, ptr);
                        strncpy(ptr, buf, size);
                        buf = p+1;
                        trim(buf);
                } 

                ptr = darray_pushback(darr_line); 
                size = strlen(buf);
                //printf("2 buf:%s, size:%d, ptr:%p\n", buf, size, ptr);
                strncpy(ptr, buf, size);

                buf = orgbuf;
                memset(buf, 0, LINE);
        }


/*      write json file */
        item = darr->da_values;
        
        if (darray_count(darr) > 3) fprintf(fdw, "[");
        for (n=3; n<darray_count(darr); n++) {                
                fprintf(fdw, "{");
                
                for (j=0; j<darray_count(*item); j++) {

                        //printf("tt:%s\n", item[1]->da_values+j*item[1]->da_size);
                        if (strcmp(item[1]->da_values+j*item[1]->da_size, "0") == 0)
                                fprintf(fdw, "\"%s\":\"%s\"", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size);
                        else if (strcmp(item[1]->da_values+j*item[1]->da_size, "2") == 0) {
                                fprintf(fdw, "\"%s\":\"%s\"", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size);
/*
                                char tt[1024];
                                memset(tt, 0, 1024);
                                printf("len:%d\n",  strlen(*(item[n]->da_values+j*item[n]->da_size)));
                                memcpy(tt, item[n]->da_values+j*item[n]->da_size+1, strlen(item[n]->da_values+j*item[n]->da_size)-2);
                                fprintf(fdw, "\"%s\":%s", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size);
*/
                        } else if (strcmp(item[1]->da_values+j*item[1]->da_size, "1") == 0)
                                fprintf(fdw, "\"%s\":%d", item[0]->da_values+j*item[n]->da_size, atoi(item[n]->da_values+j*item[n]->da_size));
                        else
                                printf("data tyep err.");

                        if (j<darray_count(*item)-1)
                                fprintf(fdw, ", ");
                }
                
                if (n < darray_count(darr)-1)
                        fprintf(fdw, "}, \n");
                else
                        fprintf(fdw, "}");
        }
        if (darray_count(darr) > 3) fprintf(fdw, "]\n");

        free(orgbuf);
        darray_free(darr);
        fclose(fd);
        fclose(fdw);

        return 0;
}