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.



74
75
76
77
78
79
80
81
# File 'lib/grm.rb', line 74

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



66
67
68
69
70
71
# File 'lib/grm.rb', line 66

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

Instance Method Details

#addressObject



182
183
184
# File 'lib/grm.rb', line 182

def address
  @args.to_i
end

#clearObject



177
178
179
180
# File 'lib/grm.rb', line 177

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

#push(key, value) ⇒ Object



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
# File 'lib/grm.rb', line 83

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 Args
    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 }
      GRM.args_push(@args, key, 'nS',
                    :int, value.size,
                    :voidp, addresses.pack('J*'))
    when Integer
      GRM.args_push(@args, key, 'nI',
                    :int, value.size,
                    :voidp, value.pack('i*'))
    when Float
      GRM.args_push(@args, key, 'nD',
                    :int, value.size,
                    :voidp, value.pack('d*'))
    when Args
      GRM.args_push(@args, key, 'nA',
                    :int, value.size,
                    :voidp, value.collect(&:address).pack('J*'))
      value.each do |v|
        v.to_gr.free = nil
      end
    else
      vs = value.collect { |v| Args.new(**v) }
      @references.concat(vs)
      GRM.args_push(@args, key, 'nA',
                    :int, value.size,
                    :voidp, vs.collect(&:address).pack('J*'))
      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



186
187
188
# File 'lib/grm.rb', line 186

def to_gr
  @args
end