Class: Upl::TermVector

Inherits:
Object show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#sizeObject (readonly)

Returns the value of attribute size.



36
37
38
# File 'lib/upl/term_vector.rb', line 36

def size
  @size
end

#termsObject (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

Raises:

  • (IndexError)


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

Raises:

  • (IndexError)


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

#eachObject



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_tObject



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

#firstObject



50
# File 'lib/upl/term_vector.rb', line 50

def first; Term.new @terms+0; end

#lastObject



51
# File 'lib/upl/term_vector.rb', line 51

def last; Term.new @terms+(size-1); end

#to_aObject



63
64
65
# File 'lib/upl/term_vector.rb', line 63

def to_a
  size.times.map{|idx| @terms+idx}
end