PySDD 0.2.12__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PySDD-0.2.12.dist-info/LICENSE +13 -0
- PySDD-0.2.12.dist-info/METADATA +225 -0
- PySDD-0.2.12.dist-info/RECORD +97 -0
- PySDD-0.2.12.dist-info/WHEEL +5 -0
- PySDD-0.2.12.dist-info/entry_points.txt +2 -0
- PySDD-0.2.12.dist-info/top_level.txt +1 -0
- pysdd/__init__.py +1 -0
- pysdd/__main__.py +19 -0
- pysdd/cli.py +261 -0
- pysdd/compiler_c.pxd +37 -0
- pysdd/fnf_c.pxd +17 -0
- pysdd/io_c.pxd +22 -0
- pysdd/iterator.py +223 -0
- pysdd/lib/libsdd-2.0/build/sdd.lib +0 -0
- pysdd/lib/libsdd-2.0/include/iterators.h +243 -0
- pysdd/lib/libsdd-2.0/include/macros.h +272 -0
- pysdd/lib/libsdd-2.0/include/parameters.h +71 -0
- pysdd/lib/libsdd-2.0/include/sdd.h +822 -0
- pysdd/lib/libsdd-2.0/include/stacks.h +86 -0
- pysdd/lib/libsdd-2.0/sddapi.h +247 -0
- pysdd/lib/libsdd-2.0/src/basic/computed.c +91 -0
- pysdd/lib/libsdd-2.0/src/basic/count_and_size.c +173 -0
- pysdd/lib/libsdd-2.0/src/basic/gc.c +104 -0
- pysdd/lib/libsdd-2.0/src/basic/hash.c +220 -0
- pysdd/lib/libsdd-2.0/src/basic/memory.c +179 -0
- pysdd/lib/libsdd-2.0/src/basic/multiply.c +209 -0
- pysdd/lib/libsdd-2.0/src/basic/nodes.c +174 -0
- pysdd/lib/libsdd-2.0/src/basic/partitions.c +351 -0
- pysdd/lib/libsdd-2.0/src/basic/references.c +79 -0
- pysdd/lib/libsdd-2.0/src/basic/replace.c +91 -0
- pysdd/lib/libsdd-2.0/src/basic/shadows.c +384 -0
- pysdd/lib/libsdd-2.0/src/basic/sort.c +148 -0
- pysdd/lib/libsdd-2.0/src/fnf/io.c +125 -0
- pysdd/lib/libsdd-2.0/src/manager/copy.c +84 -0
- pysdd/lib/libsdd-2.0/src/manager/interface.c +410 -0
- pysdd/lib/libsdd-2.0/src/manager/manager.c +308 -0
- pysdd/lib/libsdd-2.0/src/manager/stats.c +121 -0
- pysdd/lib/libsdd-2.0/src/manager/variables.c +267 -0
- pysdd/lib/libsdd-2.0/src/sdds/apply.c +396 -0
- pysdd/lib/libsdd-2.0/src/sdds/bits.c +156 -0
- pysdd/lib/libsdd-2.0/src/sdds/cardinality.c +313 -0
- pysdd/lib/libsdd-2.0/src/sdds/condition.c +107 -0
- pysdd/lib/libsdd-2.0/src/sdds/copy.c +117 -0
- pysdd/lib/libsdd-2.0/src/sdds/count.c +78 -0
- pysdd/lib/libsdd-2.0/src/sdds/essential_vars.c +128 -0
- pysdd/lib/libsdd-2.0/src/sdds/exists.c +29 -0
- pysdd/lib/libsdd-2.0/src/sdds/exists_multiple.c +310 -0
- pysdd/lib/libsdd-2.0/src/sdds/exists_multiple_static.c +165 -0
- pysdd/lib/libsdd-2.0/src/sdds/forall.c +28 -0
- pysdd/lib/libsdd-2.0/src/sdds/io.c +447 -0
- pysdd/lib/libsdd-2.0/src/sdds/model_count.c +146 -0
- pysdd/lib/libsdd-2.0/src/sdds/rename_vars.c +113 -0
- pysdd/lib/libsdd-2.0/src/sdds/size.c +65 -0
- pysdd/lib/libsdd-2.0/src/sdds/wmc.c +412 -0
- pysdd/lib/libsdd-2.0/src/util.c +254 -0
- pysdd/lib/libsdd-2.0/src/verify.c +236 -0
- pysdd/lib/libsdd-2.0/src/version.c +14 -0
- pysdd/lib/libsdd-2.0/src/vtree_fragments/construction.c +329 -0
- pysdd/lib/libsdd-2.0/src/vtree_fragments/moves.c +100 -0
- pysdd/lib/libsdd-2.0/src/vtree_fragments/operations.c +302 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/cartesian_product.c +166 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/dissect.c +78 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/limits.c +237 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/op_left_rotate.c +143 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/op_right_rotate.c +150 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/op_swap.c +112 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/rollback.c +68 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/split.c +180 -0
- pysdd/lib/libsdd-2.0/src/vtree_operations/vtree_ops.h +48 -0
- pysdd/lib/libsdd-2.0/src/vtree_search/auto.c +186 -0
- pysdd/lib/libsdd-2.0/src/vtree_search/search.c +332 -0
- pysdd/lib/libsdd-2.0/src/vtree_search/state.c +127 -0
- pysdd/lib/libsdd-2.0/src/vtrees/compare.c +137 -0
- pysdd/lib/libsdd-2.0/src/vtrees/edit.c +199 -0
- pysdd/lib/libsdd-2.0/src/vtrees/io.c +207 -0
- pysdd/lib/libsdd-2.0/src/vtrees/maps.c +42 -0
- pysdd/lib/libsdd-2.0/src/vtrees/moves.c +156 -0
- pysdd/lib/libsdd-2.0/src/vtrees/static.c +269 -0
- pysdd/lib/libsdd-2.0/src/vtrees/vtree.c +197 -0
- pysdd/lib/sdd-2.0/include/compiler.h +103 -0
- pysdd/lib/sdd-2.0/include/fnf.h +13 -0
- pysdd/lib/sdd-2.0/include/io.h +11 -0
- pysdd/lib/sdd-2.0/include/sddapi.h +340 -0
- pysdd/lib/sdd-2.0/lib/sdd.lib +0 -0
- pysdd/lib/sdd-2.0/src/fnf/compiler.c +119 -0
- pysdd/lib/sdd-2.0/src/fnf/fnf.c +56 -0
- pysdd/lib/sdd-2.0/src/fnf/io.c +200 -0
- pysdd/lib/sdd-2.0/src/fnf/utils.c +71 -0
- pysdd/lib/sdd-2.0/src/getopt.c +146 -0
- pysdd/lib/sdd-2.0/src/main.c +159 -0
- pysdd/lib/sdd_extra/include/sddapi_extra.h +22 -0
- pysdd/sdd.c +84299 -0
- pysdd/sdd.cp313-win_amd64.pyd +0 -0
- pysdd/sdd.pyx +1804 -0
- pysdd/sddapi_c.pxd +240 -0
- pysdd/util.py +450 -0
- pysdd/wmcstochastic.py +188 -0
pysdd/fnf_c.pxd
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
pysdd.fnf_c
|
|
4
|
+
~~~~~~~~~~~
|
|
5
|
+
|
|
6
|
+
:author: Wannes Meert, Arthur Choi
|
|
7
|
+
:copyright: Copyright 2017-2018 KU Leuven and Regents of the University of California.
|
|
8
|
+
:license: Apache License, Version 2.0, see LICENSE for details.
|
|
9
|
+
"""
|
|
10
|
+
from . cimport sddapi_c
|
|
11
|
+
from . cimport compiler_c
|
|
12
|
+
|
|
13
|
+
cdef extern from "fnf.h":
|
|
14
|
+
void free_fnf(compiler_c.Fnf* fnf)
|
|
15
|
+
|
|
16
|
+
# cdef extern from "fnf_wrapper.h":
|
|
17
|
+
# void free_fnf_wrapper(compiler_c.Fnf* fnf)
|
pysdd/io_c.pxd
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
pysdd.io_c
|
|
4
|
+
~~~~~~~~~~
|
|
5
|
+
|
|
6
|
+
:author: Wannes Meert, Arthur Choi
|
|
7
|
+
:copyright: Copyright 2017-2018 KU Leuven and Regents of the University of California.
|
|
8
|
+
:license: Apache License, Version 2.0, see LICENSE for details.
|
|
9
|
+
"""
|
|
10
|
+
from . cimport sddapi_c
|
|
11
|
+
from . cimport compiler_c
|
|
12
|
+
|
|
13
|
+
cdef extern from "io.h":
|
|
14
|
+
compiler_c.Fnf* read_fnf(const char* filename);
|
|
15
|
+
compiler_c.Cnf* read_cnf(const char* filename);
|
|
16
|
+
compiler_c.Dnf* read_dnf(const char* filename);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# cdef extern from "io_wrapper.h":
|
|
20
|
+
# compiler_c.Fnf* read_fnf_wrapper(const char* filename)
|
|
21
|
+
# compiler_c.Cnf* read_cnf_wrapper(const char* filename)
|
|
22
|
+
# compiler_c.Dnf* read_dnf_wrapper(const char* filename)
|
pysdd/iterator.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
pysdd.iterator
|
|
4
|
+
~~~~~~~~~~~~~~
|
|
5
|
+
|
|
6
|
+
:author: Wannes Meert, Arthur Choi
|
|
7
|
+
:copyright: Copyright 2017-2019 KU Leuven and Regents of the University of California.
|
|
8
|
+
:license: Apache License, Version 2.0, see LICENSE for details.
|
|
9
|
+
"""
|
|
10
|
+
from collections import deque
|
|
11
|
+
from .sdd import SddManager, Vtree, SddNode
|
|
12
|
+
from .util import BitArray
|
|
13
|
+
|
|
14
|
+
MYPY = False
|
|
15
|
+
if MYPY:
|
|
16
|
+
from typing import Dict, Set, Optional, List, Tuple, Callable, Union
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SddIterator:
|
|
20
|
+
def __init__(self, sdd, smooth=True, smooth_to_root=False):
|
|
21
|
+
"""Simple iterator to iterate over the SDD graph.
|
|
22
|
+
|
|
23
|
+
Supports smoothing: An arithmetic circuit AC(X) is smooth iff
|
|
24
|
+
(1) it contains at least one indicator for each variable in X, and
|
|
25
|
+
(2) for every child c of '+'-node n, we have vars(n) = vars(c).
|
|
26
|
+
|
|
27
|
+
Note, if you know that you will be performing WMC, smoothing can be implemented more efficient
|
|
28
|
+
by keeping track of the expected WMC of used and unused variables in the vtree instead of keeping
|
|
29
|
+
track of the sets of variables as is done in this iterator.
|
|
30
|
+
|
|
31
|
+
:param sdd: WmcManager
|
|
32
|
+
:param smooth: Perform smoothing while iterating over the graph
|
|
33
|
+
:param smooth_to_root: Perform smoothing wrt root instead of a given node
|
|
34
|
+
"""
|
|
35
|
+
self.sdd = sdd # type: SddManager
|
|
36
|
+
self.vtree = sdd.vtree() # type: Vtree
|
|
37
|
+
self._wmc_cache = dict() # type: Dict[SddNode, Union[float, int]]
|
|
38
|
+
# Map Vtree node positions to expected variables
|
|
39
|
+
self._expected_vars = None # type: Optional[Dict[int, Set[int]]]
|
|
40
|
+
# Map Sdd nodes to missing variables
|
|
41
|
+
self._missing_vars = dict() # type: Dict[SddNode, Set[int]]
|
|
42
|
+
self.smooth = smooth # type: bool
|
|
43
|
+
self.smooth_to_root = smooth_to_root # type: bool
|
|
44
|
+
|
|
45
|
+
if self.smooth:
|
|
46
|
+
self._cache_expected_vars()
|
|
47
|
+
|
|
48
|
+
def _cache_expected_vars(self):
|
|
49
|
+
self._expected_vars = dict()
|
|
50
|
+
nb_vtree_nodes = self.sdd.var_count() * 2 - 1
|
|
51
|
+
# visited = [False] * nb_vtree_nodes
|
|
52
|
+
visited = BitArray(nb_vtree_nodes)
|
|
53
|
+
queue = deque([self.vtree])
|
|
54
|
+
while len(queue) > 0:
|
|
55
|
+
node = queue.pop() # type: Vtree
|
|
56
|
+
pos = node.position()
|
|
57
|
+
if node.is_leaf():
|
|
58
|
+
self._expected_vars[pos] = {node.var()}
|
|
59
|
+
else:
|
|
60
|
+
if visited[pos]:
|
|
61
|
+
self._expected_vars[pos] = self._expected_vars[node.left().position()] | \
|
|
62
|
+
self._expected_vars[node.right().position()]
|
|
63
|
+
else:
|
|
64
|
+
visited[pos] = True
|
|
65
|
+
queue.append(node)
|
|
66
|
+
queue.append(node.right())
|
|
67
|
+
queue.append(node.left())
|
|
68
|
+
|
|
69
|
+
def depth_first_from_root(self, func):
|
|
70
|
+
# type: (SddIterator, Callable) -> List[Union[int, float]]
|
|
71
|
+
"""Depth first iterator starting from the root nodes.
|
|
72
|
+
|
|
73
|
+
See `depth_first` for more details.
|
|
74
|
+
"""
|
|
75
|
+
results = []
|
|
76
|
+
for node in self.vtree.get_sdd_rootnodes(self.sdd):
|
|
77
|
+
results.append(self.depth_first(node, func))
|
|
78
|
+
return results
|
|
79
|
+
|
|
80
|
+
def depth_first(self, node, func):
|
|
81
|
+
# type: (SddIterator, SddNode, Callable) -> Union[int, float]
|
|
82
|
+
"""Depth first iterator
|
|
83
|
+
|
|
84
|
+
:param node: Start node
|
|
85
|
+
:param func: Function to be called for each node:
|
|
86
|
+
``rvalue = func(node, [(prime_rvalue, sub_rvalue, prime_vars, sub_vars)],
|
|
87
|
+
expected_prime_vars, expected_sub_vars)``
|
|
88
|
+
The given arguments are:
|
|
89
|
+
* node: Current node, if None, it should be considered a decision node
|
|
90
|
+
* prime_rvalue: Return value from prime node
|
|
91
|
+
* sub_rvalue: Return value from sub node
|
|
92
|
+
* prime_vars: Variables present in prime
|
|
93
|
+
* sub_vars: Variables present in sub
|
|
94
|
+
* expected_prime_vars: Variables that are expected in prime
|
|
95
|
+
* expected_sub_vars: Variables that are expected in sub
|
|
96
|
+
The return value can be any type, there are no assumptions.
|
|
97
|
+
For WMC this is typically float, and for MC int.
|
|
98
|
+
:return:
|
|
99
|
+
"""
|
|
100
|
+
self._wmc_cache = dict()
|
|
101
|
+
if self.smooth and self._expected_vars is None:
|
|
102
|
+
self._cache_expected_vars()
|
|
103
|
+
if self.smooth and (node.is_true() or node.is_literal()):
|
|
104
|
+
wmc = func(node, None, self._expected_vars[self.vtree.position()], set())
|
|
105
|
+
else:
|
|
106
|
+
wmc = self.depth_first_rec(node, func)
|
|
107
|
+
if self.smooth_to_root and not (node.is_true() or node.is_literal() or node.is_false()):
|
|
108
|
+
root = self.vtree.root()
|
|
109
|
+
if root != node.vtree():
|
|
110
|
+
wmc_prime = wmc
|
|
111
|
+
wmc_sub = func(self.sdd.true(), None, None, None)
|
|
112
|
+
used_prime_vars = self._expected_vars[node.vtree().position()]
|
|
113
|
+
used_sub_vars = set()
|
|
114
|
+
rvalues = [(wmc_prime, wmc_sub, used_prime_vars, used_sub_vars)]
|
|
115
|
+
expected_prime_vars = used_prime_vars
|
|
116
|
+
expected_sub_vars = self._expected_vars[root.position()] - used_prime_vars
|
|
117
|
+
wmc = func(None, rvalues, expected_prime_vars, expected_sub_vars)
|
|
118
|
+
return wmc
|
|
119
|
+
|
|
120
|
+
def depth_first_rec(self, node, func):
|
|
121
|
+
# type: (SddIterator, SddNode, Callable) -> Union[int, float]
|
|
122
|
+
if node in self._wmc_cache:
|
|
123
|
+
return self._wmc_cache[node]
|
|
124
|
+
if node.is_false():
|
|
125
|
+
result = func(node, None, None, None)
|
|
126
|
+
self._wmc_cache[node] = result
|
|
127
|
+
return result
|
|
128
|
+
if node.is_true():
|
|
129
|
+
result = func(node, None, None, None)
|
|
130
|
+
self._wmc_cache[node] = result
|
|
131
|
+
return result
|
|
132
|
+
vtree = node.vtree()
|
|
133
|
+
if node.is_decision():
|
|
134
|
+
rvalues = []
|
|
135
|
+
vtree_left = vtree.left()
|
|
136
|
+
if not self.smooth or vtree_left is None:
|
|
137
|
+
expected_prime_vars = set()
|
|
138
|
+
else:
|
|
139
|
+
expected_prime_vars = self._expected_vars[vtree_left.position()]
|
|
140
|
+
vtree_right = vtree.right()
|
|
141
|
+
if not self.smooth or vtree_right is None:
|
|
142
|
+
expected_sub_vars = set()
|
|
143
|
+
else:
|
|
144
|
+
expected_sub_vars = self._expected_vars[vtree_right.position()]
|
|
145
|
+
for prime, sub in node.elements():
|
|
146
|
+
wmc_prime = self.depth_first_rec(prime, func)
|
|
147
|
+
wmc_sub = self.depth_first_rec(sub, func)
|
|
148
|
+
if not self.smooth:
|
|
149
|
+
used_prime_vars = None
|
|
150
|
+
used_sub_vars = None
|
|
151
|
+
else:
|
|
152
|
+
if prime.vtree() is None:
|
|
153
|
+
used_prime_vars = set()
|
|
154
|
+
else:
|
|
155
|
+
used_prime_vars = self._expected_vars[prime.vtree().position()]
|
|
156
|
+
if sub.vtree() is None:
|
|
157
|
+
used_sub_vars = set()
|
|
158
|
+
else:
|
|
159
|
+
used_sub_vars = self._expected_vars[sub.vtree().position()]
|
|
160
|
+
rvalues.append((wmc_prime, wmc_sub, used_prime_vars, used_sub_vars))
|
|
161
|
+
rvalue = func(node, rvalues, expected_prime_vars, expected_sub_vars)
|
|
162
|
+
else:
|
|
163
|
+
rvalue = func(node, None, None, None)
|
|
164
|
+
if self._wmc_cache is not None:
|
|
165
|
+
self._wmc_cache[node] = rvalue
|
|
166
|
+
return rvalue
|
|
167
|
+
|
|
168
|
+
@staticmethod
|
|
169
|
+
def func_modelcounting(node, rvalues, expected_prime_vars, expected_sub_vars):
|
|
170
|
+
# type: (SddNode, List[Tuple[int, int, Set[int], Set[int]]], Set[int], Set[int]) -> int
|
|
171
|
+
"""Method to pass on to ``depth_first`` to perform model counting.
|
|
172
|
+
|
|
173
|
+
Note that the WmcManager method to perform WMC is much more efficient, and supports weights.
|
|
174
|
+
"""
|
|
175
|
+
if rvalues is None:
|
|
176
|
+
# Leaf
|
|
177
|
+
if node.is_true():
|
|
178
|
+
prime_smooth_factor = 2**len(expected_prime_vars) if expected_prime_vars is not None else 1
|
|
179
|
+
sub_smooth_factor = 2**len(expected_sub_vars) if expected_sub_vars is not None else 1
|
|
180
|
+
return prime_smooth_factor * sub_smooth_factor
|
|
181
|
+
|
|
182
|
+
elif node.is_false():
|
|
183
|
+
return 0
|
|
184
|
+
|
|
185
|
+
elif node.is_literal():
|
|
186
|
+
if expected_prime_vars is not None:
|
|
187
|
+
nb_missing_vars = len(expected_prime_vars)
|
|
188
|
+
if abs(node.literal) in expected_prime_vars:
|
|
189
|
+
nb_missing_vars -= 1
|
|
190
|
+
|
|
191
|
+
prime_smooth_factor = 2**nb_missing_vars
|
|
192
|
+
else:
|
|
193
|
+
prime_smooth_factor = 1
|
|
194
|
+
|
|
195
|
+
if expected_sub_vars is not None:
|
|
196
|
+
nb_missing_vars = len(expected_sub_vars)
|
|
197
|
+
if abs(node.literal) in expected_sub_vars:
|
|
198
|
+
nb_missing_vars -= 1
|
|
199
|
+
sub_smooth_factor = 2**nb_missing_vars
|
|
200
|
+
else:
|
|
201
|
+
sub_smooth_factor = 1
|
|
202
|
+
|
|
203
|
+
return prime_smooth_factor * sub_smooth_factor
|
|
204
|
+
else:
|
|
205
|
+
raise Exception("Unknown leaf type for node {}".format(node))
|
|
206
|
+
else:
|
|
207
|
+
# Decision node
|
|
208
|
+
if node is not None and not node.is_decision():
|
|
209
|
+
raise Exception("Expected a decision node for node {}".format(node))
|
|
210
|
+
rvalue = 0
|
|
211
|
+
for mc_prime, mc_sub, prime_vars, sub_vars in rvalues:
|
|
212
|
+
if prime_vars is not None:
|
|
213
|
+
nb_missing_vars = len(expected_prime_vars) - len(prime_vars)
|
|
214
|
+
prime_smooth_factor = 2**nb_missing_vars
|
|
215
|
+
else:
|
|
216
|
+
prime_smooth_factor = 1
|
|
217
|
+
if sub_vars is not None:
|
|
218
|
+
nb_missing_vars = len(expected_sub_vars) - len(sub_vars)
|
|
219
|
+
sub_smooth_factor = 2**nb_missing_vars
|
|
220
|
+
else:
|
|
221
|
+
sub_smooth_factor = 1
|
|
222
|
+
rvalue += mc_prime * prime_smooth_factor * mc_sub * sub_smooth_factor
|
|
223
|
+
return rvalue
|
|
Binary file
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/****************************************************************************************
|
|
2
|
+
* The Sentential Decision Diagram Package
|
|
3
|
+
* sdd version 2.0, January 8, 2018
|
|
4
|
+
* http://reasoning.cs.ucla.edu/sdd
|
|
5
|
+
****************************************************************************************/
|
|
6
|
+
|
|
7
|
+
#ifndef ITERATORS_H_
|
|
8
|
+
#define ITERATORS_H_
|
|
9
|
+
|
|
10
|
+
/****************************************************************************************
|
|
11
|
+
* iterator over ALL vtree nodes (linked using ->next, ->prev, ->first and ->last)
|
|
12
|
+
****************************************************************************************/
|
|
13
|
+
|
|
14
|
+
//iterates over each node V in a vtree rooted at R, executing code B for each node
|
|
15
|
+
//
|
|
16
|
+
//V: vtree node
|
|
17
|
+
//R: vtree root
|
|
18
|
+
//B: code
|
|
19
|
+
#define FOR_each_vtree_node(V,R,B) {\
|
|
20
|
+
Vtree* V = (R)->first; /* head of linked list */\
|
|
21
|
+
Vtree* _out = (R)->last->next; /* node following last node of vtree (can be NULL) */\
|
|
22
|
+
while(V!=_out) {\
|
|
23
|
+
assert(V);\
|
|
24
|
+
B; /* execute code */\
|
|
25
|
+
V = V->next; /* next node of vtree */\
|
|
26
|
+
}\
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/****************************************************************************************
|
|
30
|
+
* iterator over INTERNAL vtree nodes (linked using ->next, ->prev, ->first and ->last)
|
|
31
|
+
*
|
|
32
|
+
* the nodes of a vtree linked list alternate between leaf and internal nodes, with
|
|
33
|
+
* the first and last nodes being leaves
|
|
34
|
+
****************************************************************************************/
|
|
35
|
+
|
|
36
|
+
//iterates over each internal node V in a vtree rooted at R, executing code B for each node
|
|
37
|
+
//
|
|
38
|
+
//V: vtree node
|
|
39
|
+
//R: vtree root
|
|
40
|
+
//B: code
|
|
41
|
+
#define FOR_each_internal_vtree_node(V,R,B) {\
|
|
42
|
+
Vtree* V = (R)->first; /* first leaf node of vtree */\
|
|
43
|
+
Vtree* _last_leaf = (R)->last; /* last leaf node of vtree */\
|
|
44
|
+
while(V!=_last_leaf) {\
|
|
45
|
+
V = V->next; /* V is internal */\
|
|
46
|
+
assert(INTERNAL(V));\
|
|
47
|
+
B; /* execute code */\
|
|
48
|
+
V = V->next; /* V is leaf */\
|
|
49
|
+
assert(LEAF(V));\
|
|
50
|
+
}\
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/****************************************************************************************
|
|
54
|
+
* iterator over LEAF vtree nodes (linked using ->next, ->prev, ->first and ->last)
|
|
55
|
+
*
|
|
56
|
+
* the nodes of a vtree linked list alternate between leaf and internal nodes, with
|
|
57
|
+
* the first and last nodes being leaves
|
|
58
|
+
****************************************************************************************/
|
|
59
|
+
|
|
60
|
+
//iterates over each leaf node V in a vtree rooted at R, executing code B for each node
|
|
61
|
+
//
|
|
62
|
+
//V: vtree node
|
|
63
|
+
//R: vtree root
|
|
64
|
+
//B: code
|
|
65
|
+
#define FOR_each_leaf_vtree_node(V,R,B) {\
|
|
66
|
+
Vtree* V = (R)->first; /* first leaf node of vtree */\
|
|
67
|
+
Vtree* _last_leaf = (R)->last; /* last leaf node of vtree */\
|
|
68
|
+
while(V!=_last_leaf) {\
|
|
69
|
+
assert(LEAF(V));\
|
|
70
|
+
B; /* execute code */\
|
|
71
|
+
V = V->next->next; /* V is leaf */\
|
|
72
|
+
}\
|
|
73
|
+
assert(LEAF(V));\
|
|
74
|
+
B; /* execute code on last leaf */\
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/****************************************************************************************
|
|
78
|
+
* iterator over ANCESTOR vtree nodes (visiting children before parents)
|
|
79
|
+
****************************************************************************************/
|
|
80
|
+
|
|
81
|
+
//iterates over each ancestor V in a vtree rooted at R, executing code B for each node
|
|
82
|
+
//
|
|
83
|
+
//V: vtree node
|
|
84
|
+
//R: vtree root
|
|
85
|
+
//B: code
|
|
86
|
+
#define FOR_each_ancestral_vtree_node(V,R,B) {\
|
|
87
|
+
Vtree* V = (R)->parent;\
|
|
88
|
+
while(V) {\
|
|
89
|
+
B; /* execute code */\
|
|
90
|
+
V = V->parent;\
|
|
91
|
+
}\
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/****************************************************************************************
|
|
96
|
+
* iterator over nodes normalized for a vtree
|
|
97
|
+
* (linked using the ->vtree_next and ->vtree_prev fields)
|
|
98
|
+
****************************************************************************************/
|
|
99
|
+
|
|
100
|
+
//iterate over the sdd nodes normalized for vtree node V, executing code B for each node
|
|
101
|
+
//this iterator allows freeing sdd nodes or changing their ->vtree_next and ->vtree_prev
|
|
102
|
+
//fields in code B
|
|
103
|
+
//
|
|
104
|
+
//N: node
|
|
105
|
+
//V: vtree
|
|
106
|
+
//B: code
|
|
107
|
+
#define FOR_each_sdd_node_normalized_for(N,V,B) {\
|
|
108
|
+
SddNode* N = (V)->nodes; /* head of linked list */\
|
|
109
|
+
while(N) {\
|
|
110
|
+
/* save N->vtree_next in case N is freed or N->vtree_next is changed in code B */\
|
|
111
|
+
SddNode* _next = N->vtree_next;\
|
|
112
|
+
/* execute code */\
|
|
113
|
+
B;\
|
|
114
|
+
/* advance to next node */\
|
|
115
|
+
N = _next;\
|
|
116
|
+
}\
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/****************************************************************************************
|
|
120
|
+
* iterator over sdd nodes normalized for nodes in vtree
|
|
121
|
+
****************************************************************************************/
|
|
122
|
+
|
|
123
|
+
//N: node
|
|
124
|
+
//V: vtree
|
|
125
|
+
//B: code
|
|
126
|
+
#define FOR_each_decomposition_in(N,V,B) FOR_each_internal_vtree_node(v,V,FOR_each_sdd_node_normalized_for(N,v,B))
|
|
127
|
+
#define FOR_each_decomposition_above(N,V,B) FOR_each_ancestral_vtree_node(v,V,FOR_each_sdd_node_normalized_for(N,v,B))
|
|
128
|
+
#define FOR_each_literal_in(N,V,B) FOR_each_leaf_vtree_node(v,V,FOR_each_sdd_node_normalized_for(N,v,B))
|
|
129
|
+
|
|
130
|
+
/****************************************************************************************
|
|
131
|
+
* iterator for sdd nodes (linked using the ->next field)
|
|
132
|
+
****************************************************************************************/
|
|
133
|
+
|
|
134
|
+
//this iterator allows freeing nodes or changing their ->next field in code B
|
|
135
|
+
//
|
|
136
|
+
//N: node
|
|
137
|
+
//L: head of linked list (node)
|
|
138
|
+
//B: code
|
|
139
|
+
#define FOR_each_linked_node(N,L,B) {\
|
|
140
|
+
SddNode* N = L; /* head of linked list */\
|
|
141
|
+
while(N) {\
|
|
142
|
+
/* save N->next in case N is freed or N->next is changed in code B */\
|
|
143
|
+
SddNode* _next = N->next;\
|
|
144
|
+
/* execute code */\
|
|
145
|
+
B;\
|
|
146
|
+
/* advance to next node */\
|
|
147
|
+
N = _next;\
|
|
148
|
+
}\
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/****************************************************************************************
|
|
152
|
+
* iterator for unique nodes
|
|
153
|
+
****************************************************************************************/
|
|
154
|
+
|
|
155
|
+
//iterates over unique nodes of manager, executing a bit of code for each node
|
|
156
|
+
//this iterator allows freeing nodes or changing their ->next field in code B
|
|
157
|
+
//
|
|
158
|
+
//N: node
|
|
159
|
+
//M: manager
|
|
160
|
+
//B: code to be executed for each node (references the variable E)
|
|
161
|
+
#define FOR_each_unique_node(N,M,B) {\
|
|
162
|
+
SddHash* H = M->unique_nodes;\
|
|
163
|
+
if(H->count) {\
|
|
164
|
+
SddSize _size = H->size;\
|
|
165
|
+
SddNode** _clists = H->clists;\
|
|
166
|
+
while(_size--) {\
|
|
167
|
+
SddNode* N = *_clists++;\
|
|
168
|
+
while(N) {\
|
|
169
|
+
/* save next in case N is freed or N->next is changed in code B */\
|
|
170
|
+
SddNode* _next = N->next;\
|
|
171
|
+
/* execute code */\
|
|
172
|
+
B;\
|
|
173
|
+
/* advance to next entry in hash table */\
|
|
174
|
+
N = _next;\
|
|
175
|
+
}\
|
|
176
|
+
}\
|
|
177
|
+
}\
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/****************************************************************************************
|
|
181
|
+
* macros for iterating over primes, subs and elements of an sdd node
|
|
182
|
+
****************************************************************************************/
|
|
183
|
+
|
|
184
|
+
//iterate over elements, executing code B for each element
|
|
185
|
+
//
|
|
186
|
+
//P: prime
|
|
187
|
+
//S: sub
|
|
188
|
+
//C: element count
|
|
189
|
+
//ES: elements array
|
|
190
|
+
//B: code
|
|
191
|
+
#define FOR_each_prime_sub_of_elements(P,S,C,ES,B) {\
|
|
192
|
+
for(SddElement* _e=ES; _e<(ES)+(C); _e++) {\
|
|
193
|
+
SddNode* P = _e->prime;\
|
|
194
|
+
SddNode* S = _e->sub;\
|
|
195
|
+
/* execute code */\
|
|
196
|
+
B;\
|
|
197
|
+
}\
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//iterate over primes and subs P/S of sdd node N, executing code B for each P/S pair
|
|
201
|
+
//
|
|
202
|
+
//P: prime
|
|
203
|
+
//S: sub
|
|
204
|
+
//N: sdd node
|
|
205
|
+
//B: code
|
|
206
|
+
#define FOR_each_prime_sub_of_node(P,S,N,B) {\
|
|
207
|
+
assert(IS_DECOMPOSITION(N));\
|
|
208
|
+
FOR_each_prime_sub_of_elements(P,S,(N)->size,ELEMENTS_OF(N),B)\
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//iterate over primes P of sdd node N, executing code B for each prime
|
|
212
|
+
//
|
|
213
|
+
//P: prime
|
|
214
|
+
//N: sdd node
|
|
215
|
+
//B: code
|
|
216
|
+
#define FOR_each_prime_of_node(P,N,B) {\
|
|
217
|
+
assert(IS_DECOMPOSITION(N));\
|
|
218
|
+
for(SddElement* _e=ELEMENTS_OF(N); _e<ELEMENTS_OF(N)+(N)->size; _e++) {\
|
|
219
|
+
SddNode* P = _e->prime;\
|
|
220
|
+
/* execute code */\
|
|
221
|
+
B;\
|
|
222
|
+
}\
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
//iterate over subs S of sdd node N, executing code B for each sub
|
|
226
|
+
//
|
|
227
|
+
//S: sub
|
|
228
|
+
//N: sdd node
|
|
229
|
+
//B: code
|
|
230
|
+
#define FOR_each_sub_of_node(S,N,B) {\
|
|
231
|
+
assert(IS_DECOMPOSITION(N));\
|
|
232
|
+
for(SddElement* _e=ELEMENTS_OF(N); _e<ELEMENTS_OF(N)+(N)->size; _e++) {\
|
|
233
|
+
SddNode* S = _e->sub;\
|
|
234
|
+
/* execute code */\
|
|
235
|
+
B;\
|
|
236
|
+
}\
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#endif // ITERATORS_H_
|
|
240
|
+
|
|
241
|
+
/****************************************************************************************
|
|
242
|
+
* end
|
|
243
|
+
****************************************************************************************/
|