Class: Upl::TermVector
- Includes:
- Enumerable
- Defined in:
- lib/upl/term_vector.rb
Overview
Create a c-array of terms using PL_new_term_refs. Methods on this class will return the term_t pointers wrapped in Term objects. If you want access to the underlying term_t pointers, use terms + idx, or the term_t method of the Term objects.
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Class Method Summary collapse
-
.[](*args) ⇒ Object
args must all be convertible to term_t Fiddle::Pointers, via term_t_of.
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #[]=(idx, value) ⇒ Object
- #each ⇒ Object
- #each_t ⇒ Object
- #first ⇒ Object
-
#initialize(size, &blk) ⇒ TermVector
constructor
similar to Array.new, but each value yielded from blk will be converted to term_t using term_t_of.
- #last ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(size, &blk) ⇒ TermVector
similar to Array.new, but each value yielded from blk will be converted to term_t using term_t_of
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/upl/term_vector.rb', line 21 def initialize size, &blk @size = Integer size @terms = Extern.PL_new_term_refs @size if block_given? @size.times do |idx| termable = (yield idx) || Variable.new term_t = Inter.term_t_of termable # TODO not sure if Extern::PL_put_term should be available as a possibility here? rv = Extern::PL_unify @terms+idx, term_t rv == 1 or raise "can't set index #{idx} of term_vector to #{termable}" end end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
36 37 38 |
# File 'lib/upl/term_vector.rb', line 36 def size @size end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
36 37 38 |
# File 'lib/upl/term_vector.rb', line 36 def terms @terms end |
Class Method Details
.[](*args) ⇒ Object
args must all be convertible to term_t Fiddle::Pointers, via term_t_of.
nil values are defaulted to Variable.new, but beware passing in the wrong number of arguments.
13 14 15 16 17 |
# File 'lib/upl/term_vector.rb', line 13 def self.[]( *args ) new args.size do |idx| args[idx] end end |
Instance Method Details
#[](idx) ⇒ Object
53 54 55 56 |
# File 'lib/upl/term_vector.rb', line 53 def [](idx) raise IndexError unless idx < @size Term.new @terms+idx end |
#[]=(idx, value) ⇒ Object
58 59 60 61 |
# File 'lib/upl/term_vector.rb', line 58 def []=(idx, value) raise IndexError unless idx < @size Extern::PL_put_term @terms + idx, (Inter.term_t_of value) end |
#each ⇒ Object
43 44 45 46 |
# File 'lib/upl/term_vector.rb', line 43 def each return enum_for :each unless block_given? size.times.each do |idx| yield Term.new @terms+idx end end |
#each_t ⇒ Object
38 39 40 41 |
# File 'lib/upl/term_vector.rb', line 38 def each_t return enum_for :each_t unless block_given? size.times.each do |idx| yield @terms+idx end end |
#to_a ⇒ Object
63 64 65 |
# File 'lib/upl/term_vector.rb', line 63 def to_a size.times.map{|idx| @terms+idx} end |