Class: Evoasm::Kernel::IO
- Inherits:
-
FFI::AutoPointer
- Object
- FFI::AutoPointer
- Evoasm::Kernel::IO
- Includes:
- Enumerable
- Defined in:
- lib/evoasm/kernel/io.rb
Overview
Represents one or multiple input/output tuples.
Constant Summary
- MAX_ARITY =
Maximum arity for tuples
8
Instance Method Summary collapse
-
#[](tuple_index) ⇒ Array
Returns the tuple at tuple_index.
-
#arity ⇒ Integer
Arity of tuples.
- #each {|Array| ... } ⇒ Object
-
#initialize(tuples, types = nil) ⇒ IO
constructor
A new instance of IO.
-
#length ⇒ Integer
The number of input/output values.
-
#size ⇒ Integer
The number of input/output tuples.
-
#to_a ⇒ Object
Converts to array of tuples.
Constructor Details
#initialize(tuples, types = nil) ⇒ IO
Returns a new instance of IO
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 |
# File 'lib/evoasm/kernel/io.rb', line 19 def initialize(tuples, types = nil) if tuples.is_a?(FFI::Pointer) super(tuples) else tuples = canonicalize_tuples tuples, types types = check_types tuples, types arity = types.size if arity > MAX_ARITY raise ArgumentError, "maximum arity exceeded (#{arity} > #{MAX_ARITY})" end kernel_io_val_type_enum_type = Libevoasm.enum_type :kernel_io_val_type types_array = FFI::MemoryPointer.new :int, arity ffi_types = types.map {|t| kernel_io_val_type_enum_type[t]} types_array.write_array_of :int, ffi_types ptr = Libevoasm.kernel_io_alloc Libevoasm.kernel_io_init ptr, arity, tuples.size, types_array super(ptr) load! tuples, arity, types end end |
Instance Method Details
#[](tuple_index) ⇒ Array
Returns the tuple at tuple_index
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/evoasm/kernel/io.rb', line 77 def [](tuple_index) if arity > 1 Array.new(arity) do |value_index| read_write_value(tuple_index, value_index) end else read_write_value(tuple_index, 0) end end |
#arity ⇒ Integer
Returns arity of tuples
46 47 48 |
# File 'lib/evoasm/kernel/io.rb', line 46 def arity Libevoasm.kernel_io_get_arity self end |
#each {|Array| ... } ⇒ Object
51 52 53 54 55 56 |
# File 'lib/evoasm/kernel/io.rb', line 51 def each return enum_for(:each) unless block_given? size.times do |tuple_index| yield self[tuple_index] end end |
#length ⇒ Integer
Returns the number of input/output values
66 67 68 |
# File 'lib/evoasm/kernel/io.rb', line 66 def length Libevoasm.kernel_io_get_n_vals self end |
#size ⇒ Integer
Returns the number of input/output tuples
71 72 73 |
# File 'lib/evoasm/kernel/io.rb', line 71 def size Libevoasm.kernel_io_get_n_tuples self end |
#to_a ⇒ Object
Converts to array of tuples
59 60 61 62 63 |
# File 'lib/evoasm/kernel/io.rb', line 59 def to_a Array.new(size) do |tuple_index| self[tuple_index] end end |