Skip to content

Commit

Permalink
Remove *_get_num_* as they were redundant, refactor ruby meta-methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 24, 2024
1 parent fa807c4 commit e331c34
Show file tree
Hide file tree
Showing 31 changed files with 109 additions and 488 deletions.
3 changes: 1 addition & 2 deletions bindings/python/cconfigspace/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .parameter import Parameter

ccs_context_get_name = _ccs_get_function("ccs_context_get_name", [ccs_context, ct.POINTER(ct.c_char_p)])
ccs_context_get_num_parameters = _ccs_get_function("ccs_context_get_num_parameters", [ccs_context, ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter = _ccs_get_function("ccs_context_get_parameter", [ccs_context, ct.c_size_t, ct.POINTER(ccs_parameter)])
ccs_context_get_parameter_by_name = _ccs_get_function("ccs_context_get_parameter_by_name", [ccs_context, ct.c_char_p, ct.POINTER(ccs_parameter)])
ccs_context_get_parameter_index_by_name = _ccs_get_function("ccs_context_get_parameter_index_by_name", [ccs_context, ct.c_char_p, ct.POINTER(ccs_bool), ct.POINTER(ct.c_size_t)])
Expand Down Expand Up @@ -56,7 +55,7 @@ def num_parameters(self):
if hasattr(self, "_num_parameters"):
return self._num_parameters
v = ct.c_size_t(0)
res = ccs_context_get_num_parameters(self.handle, ct.byref(v))
res = ccs_context_get_parameters(self.handle, 0, None, ct.byref(v))
Error.check(res)
self._num_parameters = v.value
return self._num_parameters
Expand Down
9 changes: 3 additions & 6 deletions bindings/python/cconfigspace/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ def __init__(self, handle = None, retain = False, auto_release = True,
NormalDistribution.Int = NormalIntDistribution

ccs_create_roulette_distribution = _ccs_get_function("ccs_create_roulette_distribution", [ct.c_size_t, ct.POINTER(ccs_float), ct.POINTER(ccs_distribution)])
ccs_roulette_distribution_get_num_areas = _ccs_get_function("ccs_roulette_distribution_get_num_areas", [ccs_distribution, ct.POINTER(ct.c_size_t)])
ccs_roulette_distribution_get_areas = _ccs_get_function("ccs_roulette_distribution_get_areas", [ccs_distribution, ct.c_size_t, ct.POINTER(ccs_float), ct.POINTER(ct.c_size_t)])

class RouletteDistribution(Distribution):
Expand All @@ -374,7 +373,7 @@ def num_areas(self):
if hasattr(self, "_num_areas"):
return self._num_areas
v = ct.c_size_t()
res = ccs_roulette_distribution_get_num_areas(self.handle, ct.byref(v))
res = ccs_roulette_distribution_get_areas(self.handle, 0, None, ct.byref(v))
Error.check(res)
self._num_areas = v.value
return self._num_areas
Expand All @@ -392,7 +391,6 @@ def areas(self):
Distribution.Roulette = RouletteDistribution

ccs_create_mixture_distribution = _ccs_get_function("ccs_create_mixture_distribution", [ct.c_size_t, ct.POINTER(ccs_distribution), ct.POINTER(ccs_float), ct.POINTER(ccs_distribution)])
ccs_mixture_distribution_get_num_distributions = _ccs_get_function("ccs_mixture_distribution_get_num_distributions", [ccs_distribution, ct.POINTER(ct.c_size_t)])
ccs_mixture_distribution_get_distributions = _ccs_get_function("ccs_mixture_distribution_get_distributions", [ccs_distribution, ct.c_size_t, ct.POINTER(ccs_distribution), ct.POINTER(ct.c_size_t)])
ccs_mixture_distribution_get_weights = _ccs_get_function("ccs_mixture_distribution_get_weights", [ccs_distribution, ct.c_size_t, ct.POINTER(ccs_float), ct.POINTER(ct.c_size_t)])

Expand All @@ -416,7 +414,7 @@ def num_distributions(self):
if hasattr(self, "_num_distributions"):
return self._num_distributions
v = ct.c_size_t()
res = ccs_mixture_distribution_get_num_distributions(self.handle, ct.byref(v))
res = ccs_mixture_distribution_get_distributions(self.handle, 0, None, ct.byref(v))
Error.check(res)
self._num_distributions = v.value
return self._num_distributions
Expand Down Expand Up @@ -444,7 +442,6 @@ def distributions(self):
Distribution.Mixture = MixtureDistribution

ccs_create_multivariate_distribution = _ccs_get_function("ccs_create_multivariate_distribution", [ct.c_size_t, ct.POINTER(ccs_distribution), ct.POINTER(ccs_distribution)])
ccs_multivariate_distribution_get_num_distributions = _ccs_get_function("ccs_multivariate_distribution_get_num_distributions", [ccs_distribution, ct.POINTER(ct.c_size_t)])
ccs_multivariate_distribution_get_distributions = _ccs_get_function("ccs_multivariate_distribution_get_distributions", [ccs_distribution, ct.c_size_t, ct.POINTER(ccs_distribution), ct.POINTER(ct.c_size_t)])

class MultivariateDistribution(Distribution):
Expand All @@ -464,7 +461,7 @@ def num_distributions(self):
if hasattr(self, "_num_distributions"):
return self._num_distributions
v = ct.c_size_t()
res = ccs_multivariate_distribution_get_num_distributions(self.handle, ct.byref(v))
res = ccs_multivariate_distribution_get_distributions(self.handle, 0, None, ct.byref(v))
Error.check(res)
self._num_distributions = v.value
return self._num_distributions
Expand Down
3 changes: 1 addition & 2 deletions bindings/python/cconfigspace/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class TerminalType(CEnumeration):
ccs_create_literal = _ccs_get_function("ccs_create_literal", [DatumFix, ct.POINTER(ccs_expression)])
ccs_create_variable = _ccs_get_function("ccs_create_variable", [ccs_parameter, ct.POINTER(ccs_expression)])
ccs_expression_get_type = _ccs_get_function("ccs_expression_get_type", [ccs_expression, ct.POINTER(ExpressionType)])
ccs_expression_get_num_nodes = _ccs_get_function("ccs_expression_get_num_nodes", [ccs_expression, ct.POINTER(ct.c_size_t)])
ccs_expression_get_nodes = _ccs_get_function("ccs_expression_get_nodes", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ct.c_size_t)])
ccs_literal_get_value = _ccs_get_function("ccs_literal_get_value", [ccs_expression, ct.POINTER(Datum)])
ccs_variable_get_parameter = _ccs_get_function("ccs_variable_get_parameter", [ccs_expression, ct.POINTER(ccs_parameter)])
Expand Down Expand Up @@ -114,7 +113,7 @@ def num_nodes(self):
if hasattr(self, "_num_nodes"):
return self._num_nodes
v = ct.c_size_t(0)
res = ccs_expression_get_num_nodes(self.handle, ct.byref(v))
res = ccs_expression_get_nodes(self.handle, 0, None, ct.byref(v))
Error.check(res)
self._num_nodes = v.value
return self._num_nodes
Expand Down
56 changes: 44 additions & 12 deletions bindings/ruby/lib/cconfigspace/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ def self.from_value(v)
end
end
typedef Datum.by_value, :ccs_datum_t
class MemoryPointer
def read_ccs_datum_t
Datum::new(self).value
end

def read_array_of_ccs_datum_t(length)
length.times.collect { |i| Datum::new(self[i]).value }
end
end

attach_function :ccs_init, [], :ccs_result_t
attach_function :ccs_fini, [], :ccs_result_t
Expand Down Expand Up @@ -610,30 +619,53 @@ def self.add_handle_property(name, type, accessor, memoize: false)
src << " @#{name} ||= begin\n" if memoize
src << " ptr = MemoryPointer::new(:#{type})\n"
src << " CCS.error_check CCS.#{accessor}(@handle, ptr)\n"
src << " Object::from_handle(ptr.read_#{type})\n"
src << " h = ptr.read_#{type}\n"
src << " h.null? ? nil : Object::from_handle(h)\n"
src << " end\n" if memoize
src << "end\n"
class_eval src
end

def self.add_optional_handle_property(name, type, accessor, memoize: false)
def self.add_array_size_property(name, accessor, memoize: false)
src = ""
src << "def #{name}\n"
src << " return @#{name} if instance_variable_defined?(:@#{name})\n" if memoize
src << " @#{name} = begin\n" if memoize
src << " ptr = MemoryPointer::new(:#{type})\n"
src << " CCS.error_check CCS.#{accessor}(@handle, ptr)\n"
src << " h = ptr.read_#{type}\n"
src << " if h.null?\n"
src << " nil\n"
src << " else\n"
src << " Object::from_handle(h)\n"
src << " end\n"
src << " @#{name} ||= begin\n" if memoize
src << " ptr = MemoryPointer::new(:size_t)\n"
src << " CCS.error_check CCS.#{accessor}(@handle, 0, nil, ptr)\n"
src << " ptr.read_size_t\n"
src << " end\n" if memoize
src << "end\n"
class_eval src
end

def self.add_array_property(name, type, accessor, memoize: false)
add_array_size_property("num_#{name}", accessor, memoize: memoize)
src = ""
src << "def #{name}\n"
src << " @#{name} ||= begin\n" if memoize
src << " count = num_#{name}\n"
src << " ptr = MemoryPointer::new(:#{type}, count)\n"
src << " CCS.error_check CCS.#{accessor}(@handle, count, ptr, nil)\n"
src << " ptr.read_array_of_#{type}(count)\n"
src << " end.freeze\n" if memoize
src << "end\n"
class_eval src
end

def self.add_handle_array_property(name, type, accessor, memoize: false)
add_array_size_property("num_#{name}", accessor, memoize: memoize)
src = ""
src << "def #{name}\n"
src << " @#{name} ||= begin\n" if memoize
src << " count = num_#{name}\n"
src << " ptr = MemoryPointer::new(:#{type}, count)\n"
src << " CCS.error_check CCS.#{accessor}(@handle, count, ptr, nil)\n"
src << " ptr.read_array_of_pointer(count).map { |h| h.null? ? nil : Object::from_handle(h) }\n"
src << " end.freeze\n" if memoize
src << "end\n"
class_eval src
end

add_property :object_type, :ccs_object_type_t, :ccs_object_get_type, memoize: true
add_property :refcount, :uint32, :ccs_object_get_refcount
attr_reader :handle
Expand Down
18 changes: 1 addition & 17 deletions bindings/ruby/lib/cconfigspace/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Binding < Object
include Comparable
add_property :hash, :ccs_hash_t, :ccs_binding_hash, memoize: true
add_handle_property :context, :ccs_context_t, :ccs_binding_get_context, memoize: true
add_array_property :values, :ccs_datum_t, :ccs_binding_get_values, memoize: true

def value(parameter)
ptr = MemoryPointer::new(:ccs_datum_t)
Expand All @@ -30,23 +31,6 @@ def value(parameter)
Datum::new(ptr).value
end

def values
@values ||= begin
count = num_values
values = MemoryPointer::new(:ccs_datum_t, count)
CCS.error_check CCS.ccs_binding_get_values(@handle, count, values, nil)
count.times.collect { |i| Datum::new(values[i]).value }.freeze
end
end

def num_values
@num_values ||= begin
ptr = MemoryPointer::new(:size_t)
CCS.error_check CCS.ccs_binding_get_values(@handle, 0, nil, ptr)
ptr.read_size_t
end
end

def <=>(other)
ptr = MemoryPointer::new(:int)
CCS.error_check CCS.ccs_binding_cmp(@handle, other, ptr)
Expand Down
2 changes: 1 addition & 1 deletion bindings/ruby/lib/cconfigspace/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module CCS

class Configuration < Binding
alias configuration_space context
add_optional_handle_property :features, :ccs_features_t, :ccs_configuration_get_features, memoize: true
add_handle_property :features, :ccs_features_t, :ccs_configuration_get_features, memoize: true

def initialize(handle = nil, retain: false, auto_release: true,
configuration_space: nil, features: nil, values: nil)
Expand Down
32 changes: 3 additions & 29 deletions bindings/ruby/lib/cconfigspace/configuration_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module CCS
attach_function :ccs_configuration_space_samples, [:ccs_configuration_space_t, :ccs_distribution_space_t, :ccs_features_t, :ccs_rng_t, :size_t, :pointer], :ccs_result_t

class ConfigurationSpace < Context
add_optional_handle_property :feature_space, :ccs_feature_space_t, :ccs_configuration_space_get_feature_space, memoize: true
add_handle_property :feature_space, :ccs_feature_space_t, :ccs_configuration_space_get_feature_space, memoize: true
add_handle_property :rng, :ccs_rng_t, :ccs_configuration_space_get_rng, memoize: true
add_handle_array_property :conditions, :ccs_expression_t, :ccs_configuration_space_get_conditions, memoize: true
add_handle_array_property :forbidden_clauses, :ccs_expression_t, :ccs_configuration_space_get_forbidden_clauses, memoize: true

def initialize(handle = nil, retain: false, auto_release: true,
name: "", parameters: nil, conditions: nil, forbidden_clauses: nil, feature_space: nil, rng: nil)
Expand Down Expand Up @@ -84,17 +86,6 @@ def condition(parameter)
handle.null? ? nil : Expression.from_handle(handle)
end

def conditions
@conditions ||= begin
count = num_parameters
ptr = MemoryPointer::new(:ccs_expression_t, count)
CCS.error_check CCS.ccs_configuration_space_get_conditions(@handle, count, ptr, nil)
ptr.read_array_of_pointer(count).collect { |handle|
handle.null? ? nil : Expression.from_handle(handle)
}.freeze
end
end

def conditional_parameters
@conditional_parameters ||= begin
hps = parameters
Expand All @@ -117,23 +108,6 @@ def forbidden_clause(index)
Expression.from_handle(ptr.read_ccs_expression_t)
end

def num_forbidden_clauses
@num_forbidden_clauses ||= begin
ptr = MemoryPointer::new(:size_t)
CCS.error_check CCS.ccs_configuration_space_get_forbidden_clauses(@handle, 0, nil, ptr)
ptr.read_size_t
end
end

def forbidden_clauses
@forbidden_clauses ||= begin
count = num_forbidden_clauses
ptr = MemoryPointer::new(:ccs_expression_t, count)
CCS.error_check CCS.ccs_configuration_space_get_forbidden_clauses(@handle, count, ptr, nil)
count.times.collect { |i| Expression::from_handle(ptr[i].read_pointer) }.freeze
end
end

def check(configuration)
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_configuration_space_check_configuration(@handle, configuration.handle, ptr)
Expand Down
12 changes: 1 addition & 11 deletions bindings/ruby/lib/cconfigspace/context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module CCS
attach_function :ccs_context_get_name, [:ccs_context_t, :pointer], :ccs_result_t
attach_function :ccs_context_get_num_parameters, [:ccs_context_t, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter, [:ccs_context_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_by_name, [:ccs_context_t, :string, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_index_by_name, [:ccs_context_t, :string, :pointer, :pointer], :ccs_result_t
Expand All @@ -10,7 +9,7 @@ module CCS
attach_function :ccs_context_validate_value, [:ccs_context_t, :size_t, :ccs_datum_t, :pointer], :ccs_result_t

class Context < Object
add_property :num_parameters, :size_t, :ccs_context_get_num_parameters, memoize: true
add_handle_array_property :parameters, :ccs_parameter_t, :ccs_context_get_parameters, memoize: true

def name
@name ||= begin
Expand Down Expand Up @@ -47,15 +46,6 @@ def parameter_index(parameter)
ptr.read_size_t
end

def parameters
@parameters ||= begin
count = num_parameters
ptr = MemoryPointer::new(:ccs_parameter_t, count)
CCS.error_check CCS.ccs_context_get_parameters(@handle, count, ptr, nil)
count.times.collect { |i| Parameter.from_handle(ptr[i].read_pointer) }.freeze
end
end

def validate_value(parameter, value)
ptr = MemoryPointer::new(:ccs_datum_t)
d = Datum.from_value(value)
Expand Down
46 changes: 7 additions & 39 deletions bindings/ruby/lib/cconfigspace/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ def quantization
Distribution::Normal = NormalDistribution

attach_function :ccs_create_roulette_distribution, [:size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_roulette_distribution_get_num_areas, [:ccs_distribution_t, :pointer], :ccs_result_t
attach_function :ccs_roulette_distribution_get_areas, [:ccs_distribution_t, :size_t, :pointer, :pointer], :ccs_result_t
class RouletteDistribution < Distribution
add_property :num_areas, :size_t, :ccs_roulette_distribution_get_num_areas, memoize: true
add_array_property :areas, :ccs_float_t, :ccs_roulette_distribution_get_areas, memoize: true

def initialize(handle = nil, retain: false, auto_release: true,
areas: [])
if handle
Expand All @@ -338,24 +338,17 @@ def data_type
@data_type ||= data_types.first
end

def areas
@areas ||= begin
count = num_areas
ptr = MemoryPointer::new(:ccs_float_t, count)
CCS.error_check CCS.ccs_roulette_distribution_get_areas(@handle, count, ptr, nil)
ptr.read_array_of_ccs_float_t(count).freeze
end
end
end

Distribution::Roulette = RouletteDistribution

attach_function :ccs_create_mixture_distribution, [:size_t, :pointer, :pointer, :pointer], :ccs_result_t
attach_function :ccs_mixture_distribution_get_num_distributions, [:ccs_distribution_t, :pointer], :ccs_result_t
attach_function :ccs_mixture_distribution_get_distributions, [:ccs_distribution_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_mixture_distribution_get_weights, [:ccs_distribution_t, :size_t, :pointer, :pointer], :ccs_result_t
class MixtureDistribution < Distribution
add_property :num_distributions, :size_t, :ccs_mixture_distribution_get_num_distributions, memoize: true
add_handle_array_property :distributions, :ccs_distribution_t, :ccs_mixture_distribution_get_distributions, memoize: true
add_array_property :weights, :ccs_float_t, :ccs_mixture_distribution_get_weights, memoize: true

def initialize(handle = nil, retain: false, auto_release: true,
distributions: [], weights: nil)
if handle
Expand All @@ -376,32 +369,15 @@ def initialize(handle = nil, retain: false, auto_release: true,
end
end

def weights
@weights ||= begin
count = num_distributions
ptr = MemoryPointer::new(:ccs_float_t, count)
CCS.error_check CCS.ccs_mixture_distribution_get_weights(@handle, count, ptr, nil)
ptr.read_array_of_ccs_float_t(count).freeze
end
end

def distributions
@distributions ||= begin
count = num_distributions
ptr = MemoryPointer::new(:ccs_distribution_t, count)
CCS.error_check CCS.ccs_mixture_distribution_get_distributions(@handle, count, ptr, nil)
count.times.collect { |i| Distribution.from_handle(ptr[i].read_pointer) }.freeze
end
end
end

Distribution::Mixture = MixtureDistribution

attach_function :ccs_create_multivariate_distribution, [:size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_multivariate_distribution_get_num_distributions, [:ccs_distribution_t, :pointer], :ccs_result_t
attach_function :ccs_multivariate_distribution_get_distributions, [:ccs_distribution_t, :size_t, :pointer, :pointer], :ccs_result_t
class MultivariateDistribution < Distribution
add_property :num_distributions, :size_t, :ccs_multivariate_distribution_get_num_distributions, memoize: true
add_handle_array_property :distributions, :ccs_distribution_t, :ccs_multivariate_distribution_get_distributions, memoize: true

def initialize(handle = nil, retain: false, auto_release: true,
distributions: [])
if handle
Expand All @@ -415,14 +391,6 @@ def initialize(handle = nil, retain: false, auto_release: true,
end
end

def distributions
@distributions ||= begin
count = num_distributions
ptr = MemoryPointer::new(:ccs_distribution_t, count)
CCS.error_check CCS.ccs_multivariate_distribution_get_distributions(@handle, count, ptr, nil)
count.times.collect { |i| Distribution.from_handle(ptr[i].read_pointer) }.freeze
end
end
end

Distribution::Multivariate = MultivariateDistribution
Expand Down
Loading

0 comments on commit e331c34

Please sign in to comment.