Пример #1
0
Node* List_create_internal_cycle(int size)
{
  check(size >= 3, "Size should be equal to at least 3.");

  Node* head = Node_create(0);
  Node* neck = Node_create(1);

  head->next = neck;

  Node* backbone = List_create(size - 2);

  neck->next = backbone;

  Node* it = head;

  while (it->next) {
    it = it->next;
  }

  it->next = neck;

  return head;

error:
  return NULL;
}
Пример #2
0
static List *neighbours_list(World *world, Point *point, Point *destination, Hashmap *nodes)
{
  List *neighbours = List_create();
  int nx, ny;

  for(nx = point->x - 1; nx <= point->x + 1; nx++) {
    if(nx < 0 || nx >= world->width) continue;
    for(ny = point->y - 1; ny <= point->y + 1; ny++) {
      if(ny < 0 || ny >= world->height ||
	 (ny == point->y && nx == point->x) ||
	 (!World_can_enter(world, nx, ny, point->z) &&
	  !(nx == destination->x && ny == destination->y))) continue;

      Point *p  = Point_create(nx, ny, point->z);
      Node *node = Node_create(p, 0, 0, NULL);

      Node *old_node = Hashmap_get(nodes, node);
      if(old_node) {
	Node_destroy(node);
	node = old_node;
      } else {
	Hashmap_set(nodes, node, node);
      }

      List_push(neighbours, node);
    }
  }

  return neighbours;
}
Пример #3
0
char* test_for_list_built_from_one_element()
{
  Node* head = Node_create(0);

  mu_assert(List_has_cycle(head) == 0, "List from one element does not have cycle.");

  return NULL;
}
Пример #4
0
char* test_for_one_element_tied_in_loop()
{
  Node* head = Node_create(0);
  head->next = head;

  mu_assert(List_has_cycle(head) == 1, "Cycle on list with one node.");

  return NULL;
}
Пример #5
0
List *Path(World *world, Point *source, Point *destination)
{
  int tentative_gscore;
  List *result = NULL;
  int tries = 0;

  Hashmap *nodes = Hashmap_create(cmp, hash);
  Hashmap *closedset = Hashmap_create(cmp, hash);
  PQueue *openset = PQueue_create(cmp, hash);

  Node *current;
  Node *start = Node_create(source, 0, 0, NULL);
  Hashmap_set(nodes, start, start);

  start->fscore = start->gscore + heuristic_cost_estimate(start->point, destination);
  PQueue_push(openset, start, start->fscore);

  while(!PQueue_empty(openset) &&
	tries < 300) {
    tries++;


    current = PQueue_pop(openset);
    Hashmap_set(closedset, current, current);

    if(POINT_EQ(current->point, destination)) {
      result = reconstruct_path(current);
      break;

    } else {

      List *neighbours = neighbours_list(world, current->point, destination, nodes);

      LIST_FOREACH(neighbours, first, next, cur) {
	Node *neighbour = cur->value;

	if(Hashmap_get(closedset, neighbour) == NULL) {
	  tentative_gscore =  current->gscore + 1;

	  if(!PQueue_contains(openset, neighbour) ||
	     tentative_gscore > neighbour->gscore) {
	    if(!PQueue_contains(openset, neighbour)) {
	      neighbour->came_from = current;
	      neighbour->gscore = tentative_gscore;
	      neighbour->fscore = neighbour->gscore + heuristic_cost_estimate(neighbour->point, destination);
	      PQueue_push(openset, neighbour, neighbour->fscore);
	    }
	  }
	}
      }

      List_destroy(neighbours);
    }
  }
Пример #6
0
Node* List_create(int size)
{
  check(size > 0, "Size should be greater than 0.");

  Node* head = Node_create(0);
  Node* it = head;

  int i = 1;

  for (i = 1; i < size; ++i) {
    Node* actual = Node_create(i);

    it->next = actual;
    it = actual;
  }

  return head;

error:
  return NULL;
}
Пример #7
0
FileIndex *FileIndex_create() {
    FileIndex *fi = calloc(sizeof(*fi), 1);
    check(fi != NULL, "Out of memory");

    fi->trie = Node_create();
    check(fi->trie != NULL, "Node_create failed");
    
    return fi;

error:
    FileIndex_free(fi);
    return NULL;
}
Пример #8
0
void Tree_add_somewhere(Node *node, int add_to, int value)
{
    Node *parent = Tree_search(node, add_to);
    if(parent==NULL)
    {
        printf("Position you specified for adding does not exist.\n");
    }
    else
    {
        Node *child = Node_create(value);
        Tree_add_child(parent, child); 
        child->parent = parent;
    }
}
Пример #9
0
Node* List_create_cycle(int size)
{
  check(size >= 2, "Size should be equal to at least 2.");

  Node* head = Node_create(0);
  Node* neck = List_create(size - 1);

  head->next = neck;

  Node* it = head;

  while (it->next) {
    it = it->next;
  }

  it->next = head;

  return head;

error:
  return NULL;
}
Пример #10
0
int FileIndex_add(FileIndex *fi, const char *name, FileInfo *info) {
    const char *p;
    Node *cur;

    check(fi != NULL && name != NULL, "Invalid Arguments");

    for(p = name, cur = fi->trie; *p; p++) {
        if(cur->nodes[(int) *p] == NULL) {
            cur->nodes[(int) *p] = Node_create();
            check(cur->nodes[(int) *p] != NULL, "Failed to create node");
        }
        cur = cur->nodes[(int) *p];
    }

    cur->occupied = 1;
    cur->info = *info;

    return 0;

error:
    return -1;
}
Пример #11
0
int main(){
  Node* nodo = Node_create(8);
  Node* node_to_delete2 = Node_append_to_tail(nodo, 9);
  Node_append_to_tail(nodo, 10);
  Node_append_to_tail(nodo, 11);
  Node_append_to_tail(nodo, 12);
  Node* node_to_delete1 = Node_append_to_tail(nodo, 13);
  Node_append_to_tail(nodo, 14);
  Node* node_to_delete3 = Node_append_to_tail(nodo, 15);

  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete1);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete2);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(node_to_delete3);
  Node_print_each_element(nodo);
  Node_delete_in_the_middle(nodo);
  Node_print_each_element(nodo);
  
  Node_destroy(nodo);
  return 0;
}
Пример #12
0
static struct Node *KDTree_build_tree(struct KDTree* tree, long int offset_begin, long int offset_end, int depth)
{
    int localdim;

    if (depth==0)
    {
        /* start with [begin, end+1] */
        offset_begin=0;
        offset_end=tree->_data_point_list_size;
        localdim=0;
    }
    else
    {
        localdim=depth%tree->dim;
    }

    if ((offset_end-offset_begin)<=tree->_bucket_size) 
    {
        /* leaf node */
        return Node_create(-1, localdim, offset_begin, offset_end);
    }
    else
    {
        long int offset_split;
        long int left_offset_begin, left_offset_end;
        long int right_offset_begin, right_offset_end;
        long int d;
        float cut_value;
        struct DataPoint data_point;
        struct Node *left_node, *right_node, *new_node;

        DataPoint_sort(tree->_data_point_list+offset_begin, offset_end-offset_begin, localdim);

        /* calculate index of split point */
        d=offset_end-offset_begin;
        offset_split=d/2+d%2;

        data_point=tree->_data_point_list[offset_begin+offset_split-1];
        cut_value = data_point._coord[localdim];

        /* create new node and bind to left & right nodes */
        new_node=Node_create(cut_value, localdim, offset_begin, offset_end);
        if (new_node==NULL) return NULL;

        /* left */
        left_offset_begin=offset_begin;
        left_offset_end=offset_begin+offset_split;
        left_node=KDTree_build_tree(tree, left_offset_begin, left_offset_end, depth+1);

        /* right */
        right_offset_begin=left_offset_end;
        right_offset_end=offset_end;
        right_node=KDTree_build_tree(tree, right_offset_begin, right_offset_end, depth+1);

        new_node->_left = left_node;
        new_node->_right = right_node;

        if (left_node==NULL || right_node==NULL)
        {
            Node_destroy(new_node);
            return NULL;
        }

        return new_node;
    }
}
Пример #13
0
Node *Tree_add_root(int data)
{
    Node *node = Node_create(data);
    return node;
}
Пример #14
0
int main(int argc, char *argv[])
{
    Node *node1 = Tree_add_root(1);
    
    Node *node11 = Node_create(11);
    Tree_add_child(node1, node11);

    Node *node111 = Node_create(111);
    Tree_add_child(node11, node111);

    Node *node12 = Node_create(12);
    Tree_add_child(node1, node12);

    Node *node13 = Node_create(13);
    Tree_add_child(node1, node13);

    Node *node131 = Node_create(131);
    Tree_add_child(node13, node131);

    /*printf("Print entire tree\n");
    Tree_print(node1);

    printf("Print a subtree\n");
    Tree_print(node13);*/

    /*int find = 131;
    Node *found = Tree_search(node1, find);
    if(found!=NULL)
    {
        printf("%d found\n", find);
    }
    else
    {
        printf("%d not found\n", find);
    }

    find = 1311;
    found = Tree_search(node1, find);
    if(found==1)
    {
        printf("%d found\n", find);
    }
    else
    {
        printf("%d not found\n", find);
    }*/

    /*printf("Tree before adding at a position.\n");
    Tree_print(node1);*/

    Tree_add_somewhere(node1, 12, 121);

    /*printf("Tree after adding at a position.\n");
    Tree_print(node1);*/

    Node *node1111 = Node_create(1111);
    Tree_add_child(node111, node1111);

    Node *node1112 = Node_create(1112);
    Tree_add_child(node111, node1112);

    printf("Print tree\n");
    Tree_print(node1);

    int find = 131;
    Node *found = Tree_find_parent(node1, find);
    if(found==NULL)
    {
        printf("No parent found\n");
    }
    else
    {
        printf("Parent for %d is %d\n", find, found->value);
    }

    return 0;
}
Пример #15
0
void push(List *list, int data)
{
    Node *node = Node_create(data);
    List_push(list, node);
}
Пример #16
0
Node *Node_new_group(Node *parent)
{
  return Node_create(parent, TYPE_GROUP);
}