Skip to content

Commit

Permalink
Tree configurations are immutable and created valid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Oct 11, 2024
1 parent c88992a commit 1e00868
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 348 deletions.
9 changes: 0 additions & 9 deletions bindings/python/cconfigspace/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ccs_tree_get_parent = _ccs_get_function("ccs_tree_get_parent", [ccs_tree, ct.POINTER(ccs_tree), ct.POINTER(ct.c_size_t)])
ccs_tree_get_position = _ccs_get_function("ccs_tree_get_position", [ccs_tree, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ct.c_size_t)])
ccs_tree_get_values = _ccs_get_function("ccs_tree_get_values", [ccs_tree, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ct.c_size_t)])
ccs_tree_position_is_valid = _ccs_get_function("ccs_tree_position_is_valid", [ccs_tree, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ccs_bool)])
ccs_tree_get_values_at_position = _ccs_get_function("ccs_tree_get_values_at_position", [ccs_tree, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.c_size_t, ct.POINTER(Datum)])
ccs_tree_get_node_at_position = _ccs_get_function("ccs_tree_get_node_at_position", [ccs_tree, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ccs_tree)])
ccs_tree_get_weight = _ccs_get_function("ccs_tree_get_weight", [ccs_tree, ct.POINTER(ccs_float)])
Expand Down Expand Up @@ -142,14 +141,6 @@ def values(self):
Error.check(res)
return [x.value for x in v]

def position_is_valid(self, position):
count = len(position)
v = (ct.c_size_t * count)(*position)
b = ccs_bool()
res = ccs_tree_position_is_valid(self.handle, count, v, ct.byref(b))
Error.check(res)
return not (b.value == 0)

def get_values_at_position(self, position):
count = len(position)
v1 = (ct.c_size_t * count)(*position)
Expand Down
7 changes: 0 additions & 7 deletions bindings/python/cconfigspace/tree_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ccs_tree_configuration_get_position = _ccs_get_function("ccs_tree_configuration_get_position", [ccs_tree_configuration, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ct.c_size_t)])
ccs_tree_configuration_get_values = _ccs_get_function("ccs_tree_configuration_get_values", [ccs_tree_configuration, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ct.c_size_t)])
ccs_tree_configuration_get_node = _ccs_get_function("ccs_tree_configuration_get_node", [ccs_tree_configuration, ct.POINTER(ccs_tree)])
ccs_tree_configuration_check = _ccs_get_function("ccs_tree_configuration_check", [ccs_tree_configuration, ct.POINTER(ccs_bool)])
ccs_tree_configuration_hash = _ccs_get_function("ccs_tree_configuration_hash", [ccs_tree_configuration, ct.POINTER(ccs_hash)])
ccs_tree_configuration_cmp = _ccs_get_function("ccs_tree_configuration_cmp", [ccs_tree_configuration, ccs_tree_configuration, ct.POINTER(ct.c_int)])

Expand Down Expand Up @@ -101,12 +100,6 @@ def node(self):
self._node = Tree.from_handle(v)
return self._node

def check(self):
valid = ccs_bool()
res = ccs_tree_configuration_check(self.handle, ct.byref(valid))
Error.check(res)
return not (valid.value == 0)

@property
def hash(self):
if hasattr(self, "_hash"):
Expand Down
16 changes: 0 additions & 16 deletions bindings/python/cconfigspace/tree_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class TreeSpaceType(CEnumeration):
ccs_tree_space_get_tree = _ccs_get_function("ccs_tree_space_get_tree", [ccs_tree_space, ct.POINTER(ccs_tree)])
ccs_tree_space_get_node_at_position = _ccs_get_function("ccs_tree_space_get_node_at_position", [ccs_tree_space, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ccs_tree)])
ccs_tree_space_get_values_at_position = _ccs_get_function("ccs_tree_space_get_values_at_position", [ccs_tree_space, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.c_size_t, ct.POINTER(Datum)])
ccs_tree_space_check_position = _ccs_get_function("ccs_tree_space_check_position", [ccs_tree_space, ct.c_size_t, ct.POINTER(ct.c_size_t), ct.POINTER(ccs_bool)])
ccs_tree_space_check_configuration = _ccs_get_function("ccs_tree_space_check_configuration", [ccs_tree_space, ccs_tree_configuration, ct.POINTER(ccs_bool)])
ccs_tree_space_sample = _ccs_get_function("ccs_tree_space_sample", [ccs_tree_space, ccs_features, ccs_rng, ct.POINTER(ccs_tree_configuration)])
ccs_tree_space_samples = _ccs_get_function("ccs_tree_space_samples", [ccs_tree_space, ccs_features, ccs_rng, ct.c_size_t, ct.POINTER(ccs_tree_configuration)])

Expand Down Expand Up @@ -106,20 +104,6 @@ def get_values_at_position(self, position):
Error.check(res)
return [x.value for x in v2]

def check_position(self, position):
count = len(position)
v = (ct.c_size_t * count)(*position)
b = ccs_bool()
res = ccs_tree_space_check_position(self.handle, count, v, ct.byref(b))
Error.check(res)
return not (b.value == 0)

def check_configuration(self, configuration):
b = ccs_bool()
res = ccs_tree_space_check_configuration(self.handle, configuration.handle, ct.byref(b))
Error.check(res)
return not (b.value == 0)

def sample(self, features = None, rng = None):
if features is not None:
features = features.handle
Expand Down
2 changes: 0 additions & 2 deletions bindings/python/test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def test_create(self):
for i in root.samples(rng, 100):
self.assertTrue( i is None or (i >= 0 and i < 4) )

self.assertFalse( root.position_is_valid([2, 1]) )
child = ccs.Tree(arity = 3, value = "bar" )
self.assertEqual( "bar", child.value )
root.set_child(2, child)
Expand All @@ -42,7 +41,6 @@ def test_create(self):
self.assertEqual( 1, child.depth )
self.assertEqual( child.handle.value, root.get_child(2).handle.value )
self.assertEqual( child.handle.value, root.get_node_at_position([2]).handle.value )
self.assertTrue( root.position_is_valid([2, 1]) )
self.assertEqual( ["foo", "bar"], root.get_values_at_position([2]) )

buff = root.serialize()
Expand Down
26 changes: 4 additions & 22 deletions bindings/python/test/test_tree_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ def test_static_tree_space(self):
self.assertEqual( tree.handle.value, ts.get_node_at_position([]).handle.value )
self.assertEqual( 201, ts.get_node_at_position([1, 1]).value )
self.assertEqual( [400, 301, 201], ts.get_values_at_position([1, 1]) )
self.assertTrue( ts.check_position([1, 1]) )
self.assertFalse( ts.check_position([1, 4]) )

tc = ts.sample()
self.assertTrue( ts.check_configuration(tc) )

for x in ts.samples(100):
self.assertTrue( ts.check_configuration(x) )
ts.sample()
ts.samples(100)

buff = ts.serialize()
ts2 = ccs.Object.deserialize(buffer = buff)
Expand Down Expand Up @@ -68,14 +63,8 @@ def get_vector_data(otype, name):
self.assertEqual( tree.handle.value, ts.get_node_at_position([]).handle.value )
self.assertEqual( 201, ts.get_node_at_position([1, 1]).value )
self.assertEqual( [400, 301, 201], ts.get_values_at_position([1, 1]) )
self.assertTrue( ts.check_position([1, 1]) )
self.assertFalse( ts.check_position([1, 4]) )
tc = ts.sample()
self.assertTrue( ts.check_configuration(tc) )

for i in range(100):
tc = ts.sample()
self.assertTrue( ts.check_configuration(tc) )
ts.sample()
ts.samples(100)

buff = ts.serialize()
ts2 = ccs.deserialize(buffer = buff, vector_callback = get_vector_data)
Expand All @@ -84,19 +73,12 @@ def get_vector_data(otype, name):
def test_tree_configuration(self):
tree = generate_tree(4, 0)
ts = ccs.StaticTreeSpace(name = 'space', tree = tree)
tc = ts.sample()
self.assertTrue( tc.check() )
for x in ts.samples(100):
self.assertTrue( x.check() )

tc = ccs.TreeConfiguration(tree_space = ts, position = [1, 1])
self.assertEqual( tc.tree_space.handle.value, ts.handle.value )
self.assertEqual( 2, tc.position_size )
self.assertEqual( [1, 1], tc.position )
self.assertEqual( [400, 301, 201], tc.values )
self.assertEqual( ts.get_node_at_position([1, 1]).handle.value, tc.node.handle.value )
tc2 = ccs.TreeConfiguration(tree_space = ts, position = [1, 0])
self.assertTrue( tc.check() )
self.assertTrue( tc2.check() )
self.assertNotEqual( tc.hash, tc2.hash )
self.assertTrue( tc < tc2 or tc > tc2 )
10 changes: 0 additions & 10 deletions bindings/ruby/lib/cconfigspace/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module CCS
attach_function :ccs_tree_get_parent, [:ccs_tree_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_get_position, [:ccs_tree_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_get_values, [:ccs_tree_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_position_is_valid, [:ccs_tree_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_get_values_at_position, [:ccs_tree_t, :size_t, :pointer, :size_t, :pointer], :ccs_result_t
attach_function :ccs_tree_get_node_at_position, [:ccs_tree_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_get_weight, [:ccs_tree_t, :pointer], :ccs_result_t
Expand Down Expand Up @@ -82,15 +81,6 @@ def index
alias depth num_position_items
alias position position_items

def position_is_valid?(position)
count = position.size
ptr1 = MemoryPointer::new(:size_t, count)
ptr1.write_array_of_size_t(position)
ptr2 = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_tree_position_is_valid(@handle, count, ptr1, ptr2)
ptr2.read_ccs_bool_t == CCS::FALSE ? false : true
end

def get_values_at_position(position)
count1 = position.size
ptr1 = MemoryPointer::new(:size_t, count1)
Expand Down
7 changes: 0 additions & 7 deletions bindings/ruby/lib/cconfigspace/tree_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module CCS
attach_function :ccs_tree_configuration_get_position, [:ccs_tree_configuration_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_configuration_get_values, [:ccs_tree_configuration_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_configuration_get_node, [:ccs_tree_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_tree_configuration_check, [:ccs_tree_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_tree_configuration_hash, [:ccs_tree_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_tree_configuration_cmp, [:ccs_tree_configuration_t, :ccs_tree_configuration_t, :pointer], :ccs_result_t

Expand Down Expand Up @@ -40,12 +39,6 @@ def self.from_handle(handle, retain: true, auto_release: true)
alias position_size num_position_items
alias position position_items

def check
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_tree_configuration_check(@handle, ptr)
ptr.read_ccs_bool_t == CCS::FALSE ? false : true
end

def <=>(other)
ptr = MemoryPointer::new(:int)
CCS.error_check CCS.ccs_tree_configuration_cmp(@handle, other, ptr)
Expand Down
17 changes: 0 additions & 17 deletions bindings/ruby/lib/cconfigspace/tree_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def read_ccs_tree_space_type_t
attach_function :ccs_tree_space_get_tree, [:ccs_tree_space_t, :pointer], :ccs_result_t
attach_function :ccs_tree_space_get_node_at_position, [:ccs_tree_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_space_get_values_at_position, [:ccs_tree_space_t, :size_t, :pointer, :size_t, :pointer], :ccs_result_t
attach_function :ccs_tree_space_check_position, [:ccs_tree_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tree_space_check_configuration, [:ccs_tree_space_t, :ccs_tree_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_tree_space_sample, [:ccs_tree_space_t, :ccs_features_t, :ccs_rng_t, :pointer], :ccs_result_t
attach_function :ccs_tree_space_samples, [:ccs_tree_space_t, :ccs_features_t, :ccs_rng_t, :size_t, :pointer], :ccs_result_t

Expand Down Expand Up @@ -68,21 +66,6 @@ def get_values_at_position(position)
count2.times.collect { |i| Datum::new(ptr2[i]).value }
end

def check_position(position)
count = position.size
ptr1 = MemoryPointer::new(:size_t, count)
ptr1.write_array_of_size_t(position)
ptr2 = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_tree_space_check_position(@handle, count, ptr1, ptr2)
ptr2.read_ccs_bool_t == CCS::FALSE ? false : true
end

def check_configuration(configuration)
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_tree_space_check_configuration(@handle, configuration, ptr)
ptr.read_ccs_bool_t == CCS::FALSE ? false : true
end

def sample(features: nil, rng: nil)
ptr = MemoryPointer::new(:ccs_tree_configuration_t)
CCS.error_check CCS.ccs_tree_space_sample(@handle, features, rng, ptr)
Expand Down
2 changes: 0 additions & 2 deletions bindings/ruby/test/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test_create
assert( i.nil? || (i >= 0 and i < 4) )
}

refute( root.position_is_valid?([2, 1]) )
child = CCS::Tree.new(arity: 3, value: "bar" )
assert_equal( "bar", child.value )
root.set_child(2, child)
Expand All @@ -39,7 +38,6 @@ def test_create
assert_equal( 1, child.depth )
assert_equal( child.handle, root.get_child(2).handle )
assert_equal( child.handle, root.get_node_at_position([2]).handle )
assert( root.position_is_valid?([2, 1]) )
assert_equal( ["foo", "bar"], root.get_values_at_position([2]) )

buff = root.serialize
Expand Down
26 changes: 4 additions & 22 deletions bindings/ruby/test/test_tree_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ def test_static_tree_space
assert_equal( tree.handle, ts.get_node_at_position([]).handle )
assert_equal( 201, ts.get_node_at_position([1, 1]).value )
assert_equal( [400, 301, 201], ts.get_values_at_position([1, 1]) )
assert( ts.check_position([1, 1]) )
refute( ts.check_position([1, 4]) )

tc = ts.sample
assert( ts.check_configuration(tc) )

ts.samples(100).each{ |x|
assert( ts.check_configuration(x) )
}
ts.sample
ts.samples(100)

buff = ts.serialize
ts2 = CCS.deserialize(buffer: buff)
Expand Down Expand Up @@ -64,15 +58,9 @@ def test_dynamic_tree_space
assert_equal( tree.handle, ts.get_node_at_position([]).handle )
assert_equal( 201, ts.get_node_at_position([1, 1]).value )
assert_equal( [400, 301, 201], ts.get_values_at_position([1, 1]) )
assert( ts.check_position([1, 1]) )
refute( ts.check_position([1, 4]) )
tc = ts.sample
assert( ts.check_configuration(tc) )

100.times {
tc = ts.sample
assert( ts.check_configuration(tc) )
}
ts.sample
ts.samples(100)

buff = ts.serialize
ts2 = CCS::deserialize(buffer: buff, vector_callback: get_vector_data)
Expand All @@ -82,19 +70,13 @@ def test_dynamic_tree_space
def test_tree_configuration
tree = generate_tree(4, 0)
ts = CCS::StaticTreeSpace.new(name: 'space', tree: tree)
tc = ts.sample
assert( tc.check )
ts.samples(100).each { |x| assert( x.check ) }

tc = CCS::TreeConfiguration.new(tree_space: ts, position: [1, 1])
assert_equal( tc.tree_space.handle, ts.handle )
assert_equal( 2, tc.position_size )
assert_equal( [1, 1], tc.position )
assert_equal( [400, 301, 201], tc.values )
assert_equal( ts.get_node_at_position([1, 1]).handle, tc.node.handle )
tc2 = CCS::TreeConfiguration.new(tree_space: ts, position: [1, 0])
assert( tc.check() )
assert( tc2.check() )
refute_equal( tc.hash, tc2.hash )
assert( tc < tc2 || tc > tc2 )
end
Expand Down
36 changes: 8 additions & 28 deletions include/cconfigspace/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,6 @@ ccs_tree_get_values(
ccs_datum_t *values,
size_t *num_values_ret);

/**
* Check if a position can be reached from a tree node.
* @param[in] tree
* @param[in] position_size the number of entries in the \p position array
* @param[in] position an array of indexes defining a location in the tree.
* @param[out] is_valid_ret a pointer to a variable that will hold the result
* of the check. Result will be CCS_TRUE if the
* configuration is valid. Result will be CCS_FALSE if
* the position does not reference a node of the tree.
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p tree is not a valid CCS tree
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p is_valid_ret is NULL; or if \p
* position is NULL and \p position_size is greater than 0
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_tree_position_is_valid(
ccs_tree_t tree,
size_t position_size,
const size_t *position,
ccs_bool_t *is_valid_ret);

/**
* Get the values along the path to a given position from a tree node.
* @param[in] tree
Expand All @@ -226,8 +203,10 @@ ccs_tree_position_is_valid(
* are set to #CCS_DATA_TYPE_NONE
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p tree is not a valid CCS tree
* @return #CCS_RESULT_ERROR_INVALID_TREE if the position does not reference
* node in the tree.
* @return #CCS_RESULT_ERROR_OUT_OF_BOUNDS if \p position does not define a
* valid position in \p tree
* @return #CCS_RESULT_ERROR_INVALID_TREE if one child node described by \p
* position is undefined in \p tree
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p values is NULL; if \p
* num_values is less than \p position_size + 1; or if \p position is NULL and
* \p position_size is greater than 0
Expand All @@ -253,9 +232,10 @@ ccs_tree_get_values_at_position(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p tree is not a valid CCS tree
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p tree_ret is NULL; or if \p
* position is NULL and \p position_size is greater than 0
* @return #CCS_RESULT_ERROR_INVALID_TREE if the position does not define a
* valid position in the tree space, or if this position is undefined in a
* static tree space.
* @return #CCS_RESULT_ERROR_OUT_OF_BOUNDS if \p position does not define a
* valid position in \p tree
* @return #CCS_RESULT_ERROR_INVALID_TREE if one child node described by \p
* position is undefined in \p tree
* @remarks
* This function is thread-safe
*/
Expand Down
Loading

0 comments on commit 1e00868

Please sign in to comment.