示例#1
0
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++) 
			{
				DArray *bucket = DArray_get(map->buckets, i);
				if(bucket) 
				{
					for(j = 0; j < DArray_count(bucket); j++) 
					{
						free(DArray_get(bucket,j));
					}
					DArray_destroy(bucket);
				}
			}
			DArray_destroy(map->buckets);
		}
		free(map);
	}
}
示例#2
0
DArray DArray_group_by(DArray array, DArray_group_by_fn *group_by_fn)
{
  if (DArray_is_empty(array) || !group_by_fn) return NULL;

  DArray groups = DArray_create(sizeof(DArray), 10);
  DArray group;
  int idx;
  void *el;

  size_t count = (size_t) DArray_count(array);
  for (size_t i = 0; i < count; i++) {
    el = DArray_remove(array, i);
    if (!el) continue;
    idx = group_by_fn(el);
    if (idx == -1) continue;

    group = DArray_get(groups, idx);

    if (!group) {
      group = DArray_create(array->element_size, DArray_count(array) + 1);
      DArray_ensure_capacity(groups, idx + 1);
      DArray_set(groups, idx, group);
    }

    DArray_push(group, el);
  }

  return groups;
}
示例#3
0
文件: hashmap.c 项目: imej/c
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++) {
                DArray *bucket = DArray_get(map->buckets, i);
                if (bucket) {
                    for (j = 0; j < DArray_count(bucket); j++) {
                        HashmapNode *node = DArray_get(bucket, j);
                        if (node != NULL) {
                            free(node->data);
                            map->free_key(node->key);
                        }
                    }

                    DArray_clear_destroy(bucket);

                    DArray_remove(map->buckets, i);
                }
            }
            DArray_clear_destroy(map->buckets);
        }

        free(map);
    }
}
示例#4
0
文件: hashmap.c 项目: deyimsf/journal
void 
Hashmap_destroy(Hashmap *map)
{
	int i = 0;
	int j = 0;

	if(!map){
		return;
	}

	if(!map->buckets){
		return;
	}

	for(i = 0; i < DArray_count(map->buckets); i++){
		//用DArray作桶,但桶里的每个元素也是DArray
		//用DArray代替链表避免hash碰撞
		//最后一层的DArray里放的才是HashmapNode
		DArray *bucket = DArray_get(map->buckets, i);
		if(!bucket){
			continue;	
		}

		for(j = 0; j < DArray_count(bucket); j++){
			free(DArray_get(bucket,j));
		}
		DArray_destroy(bucket);
	}
}
示例#5
0
void Hashmap_destroy(Hashmap *map, Hashmap_destroy_func destroy_f)
{
    int i = 0;
    int j = 0;

    if (map) {
        if (map->buckets) {
            for (i = 0; i < DArray_count(map->buckets); i++) {
                DArray *bucket = DArray_get(map->buckets, i);
                if (!bucket) continue;
                for (j = 0; j < DArray_count(bucket); j++) {
                    void *val = DArray_get(bucket, j);
                    HashmapNode *node = val;
                    bdestroy(node->key);
                    if (destroy_f != NULL) {
                        destroy_f(node->data);
                    }
                    free(val);
                }
                DArray_destroy(bucket);
            }
            DArray_destroy(map->buckets);
        }

        free(map);
    }
}
示例#6
0
文件: NS.c 项目: DebashisGanguly/SFTP
NodeAddress *getFSFromServicePool() {
	NodeAddress *fsAddress = NULL;

	if (DArray_count(fsList) > 0) {
		fsAddress = DArray_get(fsList, fsPosition++);

		if (fsPosition == DArray_count(fsList))
			fsPosition = 0;
	}

	return fsAddress;
}
示例#7
0
char *test_getlines()
{
  bstring str = bfromcstr("one\ntwo\nthree\nfour\nfive\n");
  struct bstrList *file = bsplit(str, '\n');

  DArray *lines = getlines(file, 2, 4);

  bstring two = bfromcstr("two");
  bstring three = bfromcstr("three");
  bstring four = bfromcstr("four");

  mu_assert(DArray_count(lines) == 3, "Wrong number of lines.");
  mu_assert(bstrcmp((bstring)DArray_at(lines, 0), two) == 0, "First line is wrong.");
  mu_assert(bstrcmp((bstring)DArray_at(lines, 1), three) == 0, "Second line is wrong.");
  mu_assert(bstrcmp((bstring)DArray_at(lines, 2), four) == 0, "Third line is wrong.");

  bstrListDestroy(file);
  bdestroy(str);
  bdestroy(two);
  bdestroy(three);
  bdestroy(four);
  DArray_destroy(lines);

  return NULL;
}
示例#8
0
文件: vm.c 项目: txus/terrorvm
void VM_start(bstring binary, bstring filename)
{
  STATE = State_new();

  Runtime_init(state);

  VALUE lobby  = Lobby_new(state); // toplevel object
  state->lobby = lobby;
  state->binary = binary;

  BytecodeFile *file = BytecodeFile_new(state, filename);

  int fn_count = DArray_count(file->function_names);
  for(int j=0; j < fn_count; j++) {
    bstring fn_name = (bstring)DArray_at(file->function_names, j);
    Function *fn = (Function*)Hashmap_get(file->functions, fn_name);
    Hashmap_set(state->functions, fn_name, fn);
  }

  bstring main_fn = bfromcstr("0_main");
  CallFrame *top_frame = CallFrame_new(lobby, STATE_FN(main_fn), NULL);
  bdestroy(main_fn);
  top_frame->name = "main";

  Stack_push(FRAMES, top_frame);

  // now we're ready to bootstrap
  State_bootstrap(state);

  // and run the codes!
  VM_run(state);

  State_destroy(state);
}
示例#9
0
文件: bootstrap.c 项目: txus/terrorvm
void
State_bootstrap(STATE)
{
  DArray *filenames = kernel_files(state);
  int count = DArray_count(filenames);

  // Expose toplevel constants
  expose_VM(state, state->lobby);

  int reenable_debugger = 0;

  // Disable debugger while loading kernel files
  if(Debug) {
    reenable_debugger = 1;
    Debug = 0;
  }

  // Load all files.
  for(int i=0; i < count; i++) {
    bstring filename = (bstring)DArray_at(filenames, i);
    bstring path = bfromcstr("kernel/");
    bconcat(path, filename);
    Primitive_require(state, String_new(state, bdata(path)), NULL, NULL);

    bdestroy(path);
    bdestroy(filename);
  }

  DArray_destroy(filenames);

  // Reenable debugger if needed
  if(reenable_debugger) Debug = 1;
}
示例#10
0
文件: close.c 项目: polysome/amok
DArray *
CloseNodes_GetNodes(CloseNodes *close)
{
    assert(close != NULL && "NULL CloseNodes pointer");

    DArray *nodes = DArray_create(sizeof(Node *),
            DArray_max(close->close_nodes));
    check(nodes != NULL, "DArray_create failed");

    int i = 0;
    for (i = 0; i < DArray_count(close->close_nodes); i++)
    {
        struct CloseNode *close_node = DArray_get(close->close_nodes, i);
        check(close_node != NULL, "NULL CloseNodes entry");
        check(close_node->node != NULL, "NULL Node pointer in CloseNodes entry");

        int rc = DArray_push(nodes, close_node->node);
        check(rc == 0, "DArray_push failed");
    }

    return nodes;
error:
    DArray_destroy(nodes);
    return NULL;
}
示例#11
0
文件: vm.c 项目: txus/terrorvm
void Value_print_all(STATE, DArray* objs)
{
  for(int i=0; i < DArray_count(objs); i++) {
    Value_print(state, (VALUE)DArray_at(objs, i));
    printf("\n");
  }
}
示例#12
0
文件: close.c 项目: polysome/amok
int 
CloseNodes_Add(CloseNodes *close, Node *node)
{
    assert(close != NULL && "NULL CloseNodes pointer");
    assert(node != NULL && "NULL Node pointer");

    Distance distance = Hash_Distance(&close->id, &node->id);
    int i = FindIndex(close->close_nodes, &distance);

    if (i < 0)
        return 0;

    struct CloseNode *close_node = malloc(sizeof(struct CloseNode));
    check_mem(close_node);

    close_node->node = node;
    close_node->distance = distance;

    if (DArray_count(close->close_nodes) < BUCKET_K)
        DArray_push(close->close_nodes, NULL);

    ShiftFrom(close->close_nodes, i);
    DArray_set(close->close_nodes, i, close_node);

    return 0;
error:
    return -1;
}
int DArray_qsort(DArray* array, DArray_compare cmp)
{
    DArray_check(array);
    qsort(array->contents, DArray_count(array), sizeof(void*), cmp);
    return 0;
error:
    return 1;
}
示例#14
0
int* get_parents(const FeaturedSentence sent) {

    int *parents = (int*) malloc(sizeof (int) * (DArray_count(sent->words) + 1));
    check_mem(parents);

    parents[0] = -1;
    for (int i = 0; i < DArray_count(sent->words); i++) {
        Word w = (Word) DArray_get(sent->words, i);
        parents[i + 1] = w->parent;
    }

    return parents;

error:
    log_err("Error in allocating parents");
    exit(1);
}
示例#15
0
文件: vector.c 项目: txus/terrorvm
void Vector_each_with_index(VALUE vector, Vector_iter_idx iter)
{
  DArray *array = VAL2ARY(vector);
  int count = DArray_count(array);

  for(int i=0; i<count; i++) {
    iter((VALUE)DArray_at(array, i), i);
  }
}
示例#16
0
文件: vector.c 项目: txus/terrorvm
void Vector_each(VALUE vector, Vector_iter iter)
{
  DArray *array = VAL2ARY(vector);
  int count = DArray_count(array);

  for(int i=0; i<count; i++) {
    iter((VALUE)DArray_at(array, i));
  }
}
示例#17
0
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++) {
        DArray* bucket = DArray_get(map->buckets, i);
        if(bucket) {
            for(j = 0; j < DArray_count(bucket); j++) {
                HashmapNode* node = DArray_get(bucket, j);
                rc = traverse_cb(node);
                if(rc != 0) return rc;
            }
        }
    }
    
    return 0;
}
示例#18
0
void free_sentence_structures(FeaturedSentence sentence) {
    for (int i = 0; i < DArray_count(sentence->words); i++) {
        free(sentence->adjacency_matrix[i]);
    }

    free(sentence->adjacency_matrix);

    sentence->adjacency_matrix = NULL;
}
示例#19
0
void fill_distribution(int *stats, DArray *keys, Hashmap_hash hash_func) {
	int i = 0;
	uint32_t hash = 0;

	for(i = 0; i < DArray_count(keys); i++) {
		hash = hash_func(DArray_get(keys, i));
		stats[hash % BUCKETS] += 1;
	}
}
示例#20
0
void fill_distribution(int *stats, DArray *keys, Hashmap_hash hash_func)//keys from gen keys to fill stats array
{   //goes through all keys does the hash then finds its bucket same as hashmap
    int i = 0;
    uint32_t hash = 0;

    for(i = 0; i < DArray_count(keys); i++) {
        hash = hash_func(DArray_get(keys, i));
        stats[hash % BUCKETS] += 1;
    }
}
示例#21
0
int Hashmap_traverse(Hashmap *map, Hashmap_traverse_cb traverse_cb)
{//walks every bucket if bucket has any values then traverse_cb on each value scans whole hash map for its values
    int i = 0;
    int j = 0;
    int rc = 0;

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

    return 0;
}
示例#22
0
int is_sorted(DArray *array)
{
    int i = 0;

    for (i = 0; i < DArray_count(array) - 1; i++) {
        if (strcmp(DArray_get(array, i), DArray_get(array, i+1)) > 0) {
            return 0;
        }
    }
    return 1;
}
示例#23
0
文件: object.c 项目: txus/oldterror
void Object_destroy(Object *object)
{
  if(object != NULL) {
    if(object->immortal == 1) return;

    switch(object->type) {
      case tString:
        bdestroy(object->value.string);
        break;
      case tFunction:
        if (object->native) {
          NativeMethod_destroy((NativeMethod*)object->value.other);
        } else {
          VMMethod_destroy((VMMethod*)object->value.other);
        }
        break;
      case tArray: {
        DArray *array = (DArray*)object->value.other;
        int i=0;
        for(i=0; i < DArray_count(array); i++) {
          release((Object*)DArray_at(array, i));
        }
        DArray_destroy(array);
        break;
      }
      case tHash: {
        Hashmap *map = (Hashmap*)object->value.other;
        Hashmap_traverse(map, delete_object_node);
        Hashmap_destroy(map);
        break;
      }
      default:
        break;
    }

    if(object->slots) {
      Hashmap_traverse(object->slots, delete_node);
      Hashmap_destroy(object->slots);
    }

    if(object->parent) {
      // FIXME: Releasing toplevel Object causes segfault :(
      /* printf("Me ("); */
      /* Object_print(object); */
      /* printf(") I am releasing my parent ("); */
      /* Object_print(object->parent); */
      /* printf(")\\n"); */
      /* release(object->parent); */
    }

    free(object);
  }
}
示例#24
0
文件: close.c 项目: polysome/amok
void 
CloseNodes_Destroy(CloseNodes *close)
{
    if (close == NULL)
        return;

    while (DArray_count(close->close_nodes) > 0)
    {
        free(DArray_pop(close->close_nodes));
    }

    DArray_destroy(close->close_nodes);
    free(close);
}
示例#25
0
static inline void TSTree_empty_content(DArray *darray)
{

  check(darray != NULL, "DArray can't be NULL.");

   int i = 0;
    if(!DArray_empty(darray)) {
        for(i = 0; i <= DArray_count(darray); i++) {
            DArray_pop(darray);
        }
    }

error:
  return;
}
示例#26
0
DArray DArray_find(DArray array, DArray_find_fn *filter_fn, void* args)
{
  if (DArray_is_empty(array)) return NULL;

  DArray filtered = DArray_create(array->element_size, array->size);

  for (int i = 0; i < DArray_count(array); i++) {
    void *item = DArray_get(array, i);

    if (filter_fn(item, args))
      DArray_push(filtered, item);
  }

  return filtered;
}
示例#27
0
char * test_get_name()
{
    DArray * array = DB_get_name("select db");
    
    int count = DArray_count(array);
    int i;
    for(i=0;i<count;i++)
    {
        snippet_print(DArray_get(array,i),stdout);
    }

    DArray_clear_destroy(array,snippet_destroy);

    return NULL;
}
示例#28
0
文件: object.c 项目: txus/oldterror
void Object_print(Object* object) {
  assert(object != NULL && "Cannot print null object");

  if(object == Lobby) {
    printf("Lobby");
    return;
  }

  switch(object->type) {
    case tInteger:
      printf("%i", object->value.integer);
      break;
    case tString:
      printf("%s", bdata(object->value.string));
      break;
    case tArray:
      printf("[");
      DArray *array = (DArray*)object->value.other;

      int i = 0, count = DArray_count(array);
      for(i=0; i < count; i++) {
        Object_print((Object*)DArray_at(array, i));
        if (i+1 != count) printf(", ");
      }

      printf("]");
      break;
    case tHash:
      printf("#<tHash:%p>", object);
      break;
    case tFunction:
      printf("#<tFunction:%p @method=\"%p\">", object, object->value.other);
      break;
    case tTrue:
      printf("true");
      break;
    case tFalse:
      printf("false");
      break;
    case tNil:
      printf("nil");
      break;
    case tObject:
      printf("#<tObject:%p>", object);
      break;
  }
}
示例#29
0
文件: object.c 项目: txus/shitdb
bstring
Object_to_string(Object *object)
{
  char *str = NULL;

  switch(object->type) {
    case tInteger: {
      str = calloc(100, sizeof(char));
      sprintf(str, "%i", object->value.as_integer);
      break;
    }
    case tString: {
      int size = blength(object->value.as_string);
      str = calloc(size, sizeof(char));

      sprintf(str, "\"%s\"", bdata(object->value.as_string));
      break;
    }
    case tArray: {
      DArray *ary = object->value.as_array;
      str = calloc(1024, sizeof(char));

      strcat(str, "[");

      int i = 0;
      int count = DArray_count(ary);

      for(i=0; i < count; i++) {
        bstring elm = Object_to_string(DArray_get(ary, i));
        strcat(str, bdata(elm));
        if(i != count - 1) strcat(str, ", ");
      }

      strcat(str, "]");
      break;
    }
    default: {
      str = calloc(4, sizeof(char));
      sprintf(str, "nil");
      break;
    }
  }

  return S(str);
}
示例#30
0
int main(int argc, char **argv) {
    FeaturedSentence sent = FeatureSentence_create();
    sent->section = 2;

    DArray *words = split(mysentence, "\n");

    for (int i = 0; i < DArray_count(words); ++i) {
        char *line = (char *) DArray_get(words, i);

        if (strcmp(line, "\n") != 0) {
            Word w = parse_word(line);
            add_word(sent, w);
            //printMatrix("Embedding", w->embedding, stdout);
        }
    }


    const char *template[] = {"p-1postag_p0postag_p1postag_c-1postag_c0v_c1postag_betweenpostag",