tclbdd

Check-in [8077309f9f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Make finite domain descriptions contain inverted lists of the variable numbers for each column.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8077309f9fa3586b596d9277df593e4b36284a77
User & Date: kbk 2013-12-18 18:54:54
Context
2013-12-18
18:59
Fix commentary on 'project' check-in: ebbab87a43 user: kbk tags: trunk
18:54
Make finite domain descriptions contain inverted lists of the variable numbers for each column. check-in: 8077309f9f user: kbk tags: trunk
16:06
Added 'replace' method to bdd::system in support of relational algebra. Fixed a typo in '===' method that caused it to fail always check-in: db99db21ba user: kbk tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to library/tclfddd.tcl.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

























24
25
26
27
28
29
30
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#-----------------------------------------------------------------------------
#
# The next few procedures are for working with domain descriptions, which
# describe a finite, multivalued domain within a BDD. A domain description
# consists of two parts:
#	    
#	(1) A dictionary whose keys are domain names and whose values
#	    are the number of bits used to encode values in the given
#	    domains.
#
#	(2) A list that gives the layout of BDD variables that
#	    represent values in the given domains. The list appears in
#	    order by variable index. It consists of alternating domain
#	    names and bit positions in values in the domain (with
#	    position zero being the least significant bit).
#
#-----------------------------------------------------------------------------


























# bdd::fddd::domain --
#
#	Defines a new finite domain
#
# Usage:
#	bdd::fddd::domain name width ?endian?
................................................................................
	    }
	}
	default {
	    return -code error -errorcode [list FDDD BadEndian $endian] \
		"unknown endian \"$endian\": must be bigendian or littleendian"
	}
    }
    return [list [dict create $name $width] $l]
}

# bdd::bdd::fddd::interleave --
#
#	Interleaves some number of finite domains so that their bit positions
#	in a BDD alternate.
#
................................................................................
# the process ceases taking bits from the shorter ones when they run out.

proc bdd::fddd::interleave {args} {
    set N 0
    set names {}
    set bits {}
    foreach domain $args {
	dict for {name width} [lindex $domain 0] {
	    if {[dict exists $names $name]} {
		return -code error -errorcode [list FDDD DuplicateName $name] \
		    "domain named \"$name\" appears in multiple places"
	    }
	    incr N $width
	    dict set names $name $width
	}
	lappend bits [lindex $domain 1]

    }
    set processed 0
    set scrambled {}
    while {$processed < $N} {
	set i 0
	foreach b $bits {
	    if {[llength $b] > 0} {
................................................................................
		lset bits $i [lassign $b name pos]
		lappend scrambled $name $pos
		incr processed
	    }
	    incr i
	}
    }
    return [list $names $scrambled]
}

# bdd::fddd::concatenate --
#
#	Concatenates the descriptions of a set of finite domains
#
# Usage:
................................................................................
#	{FDDD DuplicateName $name} if any domain is not distinct

proc bdd::fddd::concatenate {args} {
    set N 0
    set names {}
    set bits {}
    foreach domain $args {
	dict for {name width} [lindex $domain 0] {
	    if {[dict exists $names $name]} {
		return -code error -errorcode [list FDDD DuplicateName $name] \
		    "domain named \"$name\" appears in multiple places"
	    }
	    incr N $width
	    dict set names $name $width
	}
	lappend bits [lindex $domain 1]
    }
    set chain {}
    foreach b $bits {
	lappend chain {*}$b
    }
    return [list $names $chain]
}

# bdd::fddd::reader --
#
#	Makes a call to the BDD engine to construct a term corresponding
#	to a tuple in a finite domain.
#







|
|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|




|
|


<







 







|







 







|




|
|







|







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
..
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#-----------------------------------------------------------------------------
#
# The next few procedures are for working with domain descriptions, which
# describe a finite, multivalued domain within a BDD. A domain description
# consists of two parts:
#	    
#	(1) A dictionary whose keys are domain names and whose values
#	    are lists of variable indices used to encode values in the given
#	    domains. The lists appear least significant bit first.
#
#	(2) A list that gives the layout of BDD variables that
#	    represent values in the given domains. The list appears in
#	    order by variable index. It consists of alternating domain
#	    names and bit positions in values in the domain (with
#	    position zero being the least significant bit).
#
#-----------------------------------------------------------------------------

# bdd::fddd::Invert --
#
#	Given the list of column names and bit positions in the database
#	layout, makes an inverted list indexed by column giving the
#	bit positions of that column

proc bdd::fddd::Invert {bitlist} {
    set i 0
    set result {}
    foreach {name pos} $bitlist {
	if {[dict exists $result $name]} {
	    set l [dict get $result $name]
	} else {
	    set l {}
	}
	while {[llength $l] <= $pos} {
	    lappend l {}
	}
	lset l $pos $i
	dict set result $name $l
	incr i
    }
    return $result
}

# bdd::fddd::domain --
#
#	Defines a new finite domain
#
# Usage:
#	bdd::fddd::domain name width ?endian?
................................................................................
	    }
	}
	default {
	    return -code error -errorcode [list FDDD BadEndian $endian] \
		"unknown endian \"$endian\": must be bigendian or littleendian"
	}
    }
    return [list [Invert $l] $l]
}

# bdd::bdd::fddd::interleave --
#
#	Interleaves some number of finite domains so that their bit positions
#	in a BDD alternate.
#
................................................................................
# the process ceases taking bits from the shorter ones when they run out.

proc bdd::fddd::interleave {args} {
    set N 0
    set names {}
    set bits {}
    foreach domain $args {
	dict for {name poslist} [lindex $domain 0] {
	    if {[dict exists $names $name]} {
		return -code error -errorcode [list FDDD DuplicateName $name] \
		    "domain named \"$name\" appears in multiple places"
	    }
	    incr N [llength $poslist]
	    dict set names $name {}
	}
	lappend bits [lindex $domain 1]

    }
    set processed 0
    set scrambled {}
    while {$processed < $N} {
	set i 0
	foreach b $bits {
	    if {[llength $b] > 0} {
................................................................................
		lset bits $i [lassign $b name pos]
		lappend scrambled $name $pos
		incr processed
	    }
	    incr i
	}
    }
    return [list [Invert $scrambled] $scrambled]
}

# bdd::fddd::concatenate --
#
#	Concatenates the descriptions of a set of finite domains
#
# Usage:
................................................................................
#	{FDDD DuplicateName $name} if any domain is not distinct

proc bdd::fddd::concatenate {args} {
    set N 0
    set names {}
    set bits {}
    foreach domain $args {
	dict for {name poslist} [lindex $domain 0] {
	    if {[dict exists $names $name]} {
		return -code error -errorcode [list FDDD DuplicateName $name] \
		    "domain named \"$name\" appears in multiple places"
	    }
	    incr N [llength $poslist]
	    dict set names $name {}
	}
	lappend bits [lindex $domain 1]
    }
    set chain {}
    foreach b $bits {
	lappend chain {*}$b
    }
    return [list [Invert $chain] $chain]
}

# bdd::fddd::reader --
#
#	Makes a call to the BDD engine to construct a term corresponding
#	to a tuple in a finite domain.
#

Changes to tests/fddd.test.

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
test fddd-2.2 {domain - bigendian default, littleendian available} {*}{
    -body {
	list \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 littleendian] \
	    [bdd::fddd::domain c 3 bigendian]
    }
    -result {{{a 3} {a 0 a 1 a 2}} {{b 3} {b 0 b 1 b 2}} {{c 3} {c 2 c 1 c 0}}}
}

test fddd-2.3 {interleave - duplicated domains} {*}{
    -body {
	list [catch {
	    bdd::fddd::interleave \
		[bdd::fddd::domain a 3] \
................................................................................

test fddd-2.4 {interleave - two domains} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3]
    }
    -result {{a 3 b 3} {a 0 b 0 a 1 b 1 a 2 b 2}}
}

test fddd-2.5 {interleave - three domains} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 bigendian] \
	    [bdd::fddd::domain c 3]
    }
    -result {{a 3 b 3 c 3} {a 0 b 2 c 0 a 1 b 1 c 1 a 2 b 0 c 2}}
}

test fddd-2.6 {interleave - domains of unequal lengths} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 4] \
	    [bdd::fddd::domain b 3 bigendian] \
	    [bdd::fddd::domain c 2]
    }
    -result {{a 4 b 3 c 2} {a 0 b 2 c 0 a 1 b 1 c 1 a 2 b 0 a 3}}
}

test fddd-2.7 {concatenate - duplicated domains} {*}{
    -body {
	list [catch {
	    bdd::fddd::concatenate \
		[bdd::fddd::domain a 3] \
................................................................................

test fddd-2.8 {concatenate - two domains} {*}{
    -body {
	bdd::fddd::concatenate \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 bigendian]
    }
    -result {{a 3 b 3} {a 0 a 1 a 2 b 2 b 1 b 0}}
}

test fddd-2.9 {concatenate of interleaved} {*}{
    -body {
	bdd::fddd::concatenate \
	    [bdd::fddd::interleave \
		 [bdd::fddd::domain a 3] \
		 [bdd::fddd::domain b 3]] \
	    [bdd::fddd::interleave \
		 [bdd::fddd::domain c 3] \
		 [bdd::fddd::domain d 3]]
    }
    -result {{a 3 b 3 c 3 d 3} {a 0 b 0 a 1 b 1 a 2 b 2 c 0 d 0 c 1 d 1 c 2 d 2}}
}

test fddd-2.10 {interleave of concatenated} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::concatenate \
		 [bdd::fddd::domain a 3] \
		 [bdd::fddd::domain b 3]] \
	    [bdd::fddd::concatenate \
		 [bdd::fddd::domain c 3] \
		 [bdd::fddd::domain d 3]]
    }
    -result {{a 3 b 3 c 3 d 3} {a 0 c 0 a 1 c 1 a 2 c 2 b 0 d 0 b 1 d 1 b 2 d 2}}
}

test fddd-3.1 {reader - 1 column} {*}{
    -setup {
	bdd::system create sys
	set layout \
	    [bdd::fddd::concatenate \







|







 







|









|









|







 







|












|












|







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
test fddd-2.2 {domain - bigendian default, littleendian available} {*}{
    -body {
	list \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 littleendian] \
	    [bdd::fddd::domain c 3 bigendian]
    }
    -result {{{a {0 1 2}} {a 0 a 1 a 2}} {{b {0 1 2}} {b 0 b 1 b 2}} {{c {2 1 0}} {c 2 c 1 c 0}}}
}

test fddd-2.3 {interleave - duplicated domains} {*}{
    -body {
	list [catch {
	    bdd::fddd::interleave \
		[bdd::fddd::domain a 3] \
................................................................................

test fddd-2.4 {interleave - two domains} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3]
    }
    -result {{a {0 2 4} b {1 3 5}} {a 0 b 0 a 1 b 1 a 2 b 2}}
}

test fddd-2.5 {interleave - three domains} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 bigendian] \
	    [bdd::fddd::domain c 3]
    }
    -result {{a {0 3 6} b {7 4 1} c {2 5 8}} {a 0 b 2 c 0 a 1 b 1 c 1 a 2 b 0 c 2}}
}

test fddd-2.6 {interleave - domains of unequal lengths} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::domain a 4] \
	    [bdd::fddd::domain b 3 bigendian] \
	    [bdd::fddd::domain c 2]
    }
    -result {{a {0 3 6 8} b {7 4 1} c {2 5}} {a 0 b 2 c 0 a 1 b 1 c 1 a 2 b 0 a 3}}
}

test fddd-2.7 {concatenate - duplicated domains} {*}{
    -body {
	list [catch {
	    bdd::fddd::concatenate \
		[bdd::fddd::domain a 3] \
................................................................................

test fddd-2.8 {concatenate - two domains} {*}{
    -body {
	bdd::fddd::concatenate \
	    [bdd::fddd::domain a 3] \
	    [bdd::fddd::domain b 3 bigendian]
    }
    -result {{a {0 1 2} b {5 4 3}} {a 0 a 1 a 2 b 2 b 1 b 0}}
}

test fddd-2.9 {concatenate of interleaved} {*}{
    -body {
	bdd::fddd::concatenate \
	    [bdd::fddd::interleave \
		 [bdd::fddd::domain a 3] \
		 [bdd::fddd::domain b 3]] \
	    [bdd::fddd::interleave \
		 [bdd::fddd::domain c 3] \
		 [bdd::fddd::domain d 3]]
    }
    -result {{a {0 2 4} b {1 3 5} c {6 8 10} d {7 9 11}} {a 0 b 0 a 1 b 1 a 2 b 2 c 0 d 0 c 1 d 1 c 2 d 2}}
}

test fddd-2.10 {interleave of concatenated} {*}{
    -body {
	bdd::fddd::interleave \
	    [bdd::fddd::concatenate \
		 [bdd::fddd::domain a 3] \
		 [bdd::fddd::domain b 3]] \
	    [bdd::fddd::concatenate \
		 [bdd::fddd::domain c 3] \
		 [bdd::fddd::domain d 3]]
    }
    -result {{a {0 2 4} c {1 3 5} b {6 8 10} d {7 9 11}} {a 0 c 0 a 1 c 1 a 2 c 2 b 0 d 0 b 1 d 1 b 2 d 2}}
}

test fddd-3.1 {reader - 1 column} {*}{
    -setup {
	bdd::system create sys
	set layout \
	    [bdd::fddd::concatenate \