Mercurial > hg > Members > nobuyasu > CbC
annotate DPP/state_db.c @ 33:3946f8d26710 draft default tip
add benchmarck/binary-trees
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 09 Apr 2013 16:41:30 +0900 |
parents | a89b61162c29 |
children |
rev | line source |
---|---|
0 | 1 #include <stdlib.h> |
2 | |
3 #include "state_db.h" | |
4 #include "memory.h" | |
5 | |
6 StateDB | |
7 create_stateDB() | |
8 { | |
9 StateDB s = (StateDB)malloc(sizeof(StateNode)); | |
10 if (!s) die_exit("Cann't alloc state db node."); | |
11 return s; | |
12 } | |
13 | |
14 static MemoryPtr mem_db; | |
15 | |
16 static int state_count0; | |
17 | |
18 void | |
19 reset_state_count() | |
20 { | |
21 state_count0 = 0; | |
22 } | |
23 | |
24 int | |
25 state_count() | |
26 { | |
27 return state_count0; | |
28 } | |
29 | |
30 | |
31 /* | |
32 | |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
33 lookup_StateDB(struct state *s, StateDB *parent, StatePtr *out) |
0 | 34 |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
35 s->memory points the real memory |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
36 if s is new, it is copied in the database (parent). |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
37 if s is in the database, existing state is returned. |
0 | 38 |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
39 if return value is 0, it returns new state. |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
40 if out is null, no copy_state is created. (lookup mode) |
0 | 41 |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
42 Founded state or newly created state is returned in out. |
0 | 43 |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
44 */ |
0 | 45 |
46 int | |
47 lookup_StateDB(StateDB s, StateDB *parent, StateDB *out) | |
48 { | |
49 StateDB db; | |
50 int r; | |
51 | |
52 while(1) { | |
33
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
53 db = *parent; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
54 if (!db) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
55 /* not found */ |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
56 if (out) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
57 db = create_stateDB(); |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
58 db->left = db->right = 0; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
59 db->memory = copy_memory(s->memory,&mem_db); |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
60 db->hash = s->hash; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
61 state_count0 ++; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
62 *parent = db; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
63 *out = db; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
64 } |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
65 return 0; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
66 } |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
67 if (s->hash == db->hash) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
68 r = cmp_memory(s->memory,db->memory); |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
69 } else |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
70 r = (s->hash > db->hash)? 1 : -1; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
71 if(!r) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
72 /* bingo */ |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
73 if (out) *out = db; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
74 return 1; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
75 } else if (r>0) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
76 parent = &db->left; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
77 } else if (r<0) { |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
78 parent = &db->right; |
3946f8d26710
add benchmarck/binary-trees
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
79 } |
0 | 80 } |
81 } | |
82 | |
83 /* end */ |