Class: GRM::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/grm.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Args

Returns a new instance of Args.



58
59
60
61
62
63
64
65
# File 'lib/grm.rb', line 58

def initialize(**args)
  @args = GRM.args_new
  @args.free = FFI['grm_args_delete']
  @references = []
  args.each do |key, value|
    push(key, value)
  end
end

Class Method Details

.try_convert(value) ⇒ Object



50
51
52
53
54
55
# File 'lib/grm.rb', line 50

def try_convert(value)
  case value
  when Hash
    new(**value)
  end
end

Instance Method Details

#addressObject



184
185
186
# File 'lib/grm.rb', line 184

def address
  @args.to_i
end

#clearObject



179
180
181
182
# File 'lib/grm.rb', line 179

def clear
  GRM.args_clear(@args)
  @references.clear
end

#push(key, value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/grm.rb', line 67

def push(key, value)
  key = key.to_s if key.is_a?(Symbol)

  # Support Numo::NArray transparently when available
  if defined?(Numo::NArray) && value.is_a?(Numo::NArray)
    shape = value.shape
    case shape.length
    when 1
      # 1D NArray: delegate to existing Array handling
      return push(key, value.to_a)
    when 2
      # 2D NArray: convert to nested Array and reuse 2D Array path
      rows, cols = shape
      nested = Array.new(rows) do |r|
        Array.new(cols) do |c|
          value[r, c]
        end
      end
      return push(key, nested)
    else
      raise ArgumentError, "Numo::NArray with dimension > 2 is not supported for key '#{key}'"
    end
  end

  case value
  when String
    GRM.args_push(@args, key, 's', :const_string, value)
  when Integer
    GRM.args_push(@args, key, 'i', :int, value)
  when Float
    GRM.args_push(@args, key, 'd', :double, value)
  when TrueClass, FalseClass
    GRM.args_push(@args, key, 'i', :int, value ? 1 : 0)
  when Args
    @references << value
    GRM.args_push(@args, key, 'a', :voidp, value.address)
    value.to_gr.free = nil
  when Array
    raise ArgumentError, "Array value for key '#{key}' cannot be empty" if value.empty?

    # Handle 2D Array (Matrix): flatten and add dimensions
    if value[0].is_a?(Array)
      rows = value.size
      cols = value[0].size
      # Validate that all rows have the same length
      unless value.all? { |row| row.size == cols }
        raise ArgumentError, "All rows in 2D array for key '#{key}' must have the same length"
      end

      # Flatten in row-major order
      flattened = value.flatten

      push(key, flattened)
      # GRM expects dims in [width, height] order (columns, rows)
      push("#{key}_dims", [cols, rows])
      return
    end

    case value[0]
    when String
      addresses = value.collect { |v| Fiddle::Pointer[v].to_i }
      @references.concat(value)
      packed = addresses.pack('J*')
      @references << packed
      GRM.args_push(@args, key, 'nS',
                    :int, value.size,
                    :voidp, packed)
    when Integer, Float
      if value.all? { |v| v.is_a?(Integer) }
        packed = value.pack('i*')
        @references << packed
        GRM.args_push(@args, key, 'nI',
                      :int, value.size,
                      :voidp, packed)
        return
      end

      packed = value.map(&:to_f).pack('d*')
      @references << packed
      GRM.args_push(@args, key, 'nD',
                    :int, value.size,
                    :voidp, packed)
    when Args
      @references.concat(value)
      packed = value.collect(&:address).pack('J*')
      @references << packed
      GRM.args_push(@args, key, 'nA',
                    :int, value.size,
                    :voidp, packed)
      value.each do |v|
        v.to_gr.free = nil
      end
    else
      vs = value.collect { |v| Args.new(**v) }
      @references.concat(vs)
      packed = vs.collect(&:address).pack('J*')
      @references << packed
      GRM.args_push(@args, key, 'nA',
                    :int, value.size,
                    :voidp, packed)
      vs.each do |v|
        v.to_gr.free = nil
      end
    end
  else
    v = Args.new(**value)
    @references << v
    GRM.args_push(@args, key, 'a', :voidp, v.address)
    v.to_gr.free = nil
  end
end

#to_grObject



188
189
190
# File 'lib/grm.rb', line 188

def to_gr
  @args
end