-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcola_puzzle.c
66 lines (52 loc) · 1.5 KB
/
cola_puzzle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "cola_puzzle.h"
#include "cola_impl.h"
#include <stdio.h>
inline unsigned int puzzle_list_hash_function(rb_tree *e) {
int hash;
int i;
for(i=0,hash=0; i!=5; i++)
hash = hash * 10 + e->p->tablero[i];
//no hace falta sacarle el modulo pues sglib ya lo hace
return hash;
}
cola_puzzle *make_cola_puzzle() {
cola_puzzle *cola = malloc(sizeof(cola_puzzle));
sglib_hashed_rb_tree_init(cola->phash);
cola->ptree = NULL;
return cola;
}
void insert(cola_puzzle *c, puzzle *p) {
struct sglib_puzzle_rb_tree_iterator it;
puzzle ptmp;
rb_tree *auxh,*member;
auxh = malloc(sizeof(rb_tree));
auxh->p = p;
puzzle_rb_tree *auxt,auxa;
auxt = malloc(sizeof(puzzle_rb_tree));
auxt->p = p;
//verifico si esta
if(sglib_hashed_rb_tree_add_if_not_member(c->phash,auxh,&member)){
sglib_puzzle_rb_tree_add(&c->ptree,auxt);
}
}
inline int contains(cola_puzzle *c, puzzle *p){
int is;
rb_tree auxh;
auxh.p = p;
return sglib_hashed_rb_tree_is_member(c->phash,&auxh);
}
int empty(cola_puzzle *c){
return c->ptree == NULL;
}
puzzle *top(cola_puzzle *c) {
rb_tree aux, *member;
struct sglib_puzzle_rb_tree_iterator it;
puzzle_rb_tree *te;
te=sglib_puzzle_rb_tree_it_init_inorder(&it,c->ptree);
//lo borro de ambas estructuras ademas de liberar la memoria
sglib_puzzle_rb_tree_delete(&c->ptree,te);
//hago un puzzle_list para poder eliminar este elemento del hash
aux.p = te->p;
sglib_hashed_rb_tree_delete_if_member(c->phash,&aux,&member);
return te->p;
}