Method: OpenC3::StructureItem#<=>

Defined in:
lib/openc3/packets/structure_item.rb,
ext/openc3/ext/structure/structure.c

#<=>(other_item) ⇒ Object

Comparison Operator based on bit_offset. This means that StructureItems with different names or bit sizes are equal if they have the same bit offset.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/openc3/packets/structure_item.rb', line 220

def <=>(other_item)
  return nil unless other_item.kind_of?(StructureItem)

  other_bit_offset = other_item.bit_offset
  other_bit_size = other_item.bit_size

  # Handle same bit offset case
  if (@bit_offset == 0) && (other_bit_offset == 0)
    # Both bit_offsets are 0 so sort by bit_size
    # This allows derived items with bit_size of 0 to be listed first
    # Compare based on bit size then create index
    if @bit_size == other_bit_size
      if @create_index
        if @create_index <= other_item.create_index
          return -1
        else
          return 1
        end
      else
        return 0
      end
    elsif @bit_size < other_bit_size
      return -1
    else
      return 1
    end
  end

  # Handle different bit offsets
  if ((@bit_offset >= 0) && (other_bit_offset >= 0)) || ((@bit_offset < 0) && (other_bit_offset < 0))
    # Both Have Same Sign
    if @bit_offset == other_bit_offset
      if @create_index
        if @create_index <= other_item.create_index
          return -1
        else
          return 1
        end
      else
        return 0
      end
    elsif @bit_offset <= other_bit_offset
      return -1
    else
      return 1
    end
  else
    # Different Signs
    if @bit_offset == other_bit_offset
      if @create_index
        if @create_index < other_item.create_index
          return -1
        else
          return 1
        end
      else
        return 0
      end
    elsif @bit_offset < other_bit_offset
      return 1
    else
      return -1
    end
  end
end