Class: BinData::Array

Inherits:
Base
  • Object
show all
Includes:
DSLMixin, Enumerable
Defined in:
lib/bindata/array.rb

Overview

An Array is a list of data objects of the same type.

require 'bindata'

data = "\x03\x04\x05\x06\x07\x08\x09"

obj = BinData::Array.new(:type => :int8, :initial_length => 6)
obj.read(data) #=> [3, 4, 5, 6, 7, 8]

obj = BinData::Array.new(:type => :int8,
                         :read_until => lambda { index == 1 })
obj.read(data) #=> [3, 4]

obj = BinData::Array.new(:type => :int8,
                         :read_until => lambda { element >= 6 })
obj.read(data) #=> [3, 4, 5, 6]

obj = BinData::Array.new(:type => :int8,
        :read_until => lambda { array[index] + array[index - 1] == 13 })
obj.read(data) #=> [3, 4, 5, 6, 7]

obj = BinData::Array.new(:type => :int8, :read_until => :eof)
obj.read(data) #=> [3, 4, 5, 6, 7, 8, 9]

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params are:

:type

The symbol representing the data type of the array elements. If the type is to have params passed to it, then it should be provided as [type_symbol, hash_params].

:initial_length

The initial length of the array.

:read_until

While reading, elements are read until this condition is true. This is typically used to read an array until a sentinel value is found. The variables index, element and array are made available to any lambda assigned to this parameter. If the value of this parameter is the symbol :eof, then the array will read as much data from the stream as possible.

Each data object in an array has the variable index made available to any lambda evaluated as a parameter of that data object.

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLMixin

included

Methods inherited from Base

#==, #_assign, #_do_num_bytes, #_do_read, #_do_write, #_snapshot, arg_extractor, bindata_name, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_deprecation, #inspect, #new, #num_bytes, #offset, #pretty_print, #read, read, register, register_self, register_subclasses, #rel_offset, #to_binary_s, #to_s, unregister_self, #write

Methods included from CheckOrAdjustOffsetMixin

#do_read_with_adjust_offset, #do_read_with_check_offset, included

Methods included from AcceptedParametersMixin

included

Class Method Details

.sanitize_parameters!(params) ⇒ Object

:nodoc:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bindata/array.rb', line 62

def sanitize_parameters!(params) #:nodoc:
  unless params.has_parameter?(:initial_length) or
           params.has_parameter?(:read_until)
    # ensure one of :initial_length and :read_until exists
    params[:initial_length] = 0
  end

  params.warn_replacement_parameter(:read_length, :initial_length)

  params.merge!(dsl_params)

  if params.needs_sanitizing?(:type)
    el_type, el_params = params[:type]
    params[:type] = params.create_sanitized_object_prototype(el_type, el_params)
  end
end

Instance Method Details

#[](arg1, arg2 = nil) ⇒ Object Also known as: slice

Returns the element at index.



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/bindata/array.rb', line 141

def [](arg1, arg2 = nil)
  if arg1.respond_to?(:to_int) and arg2.nil?
    slice_index(arg1.to_int)
  elsif arg1.respond_to?(:to_int) and arg2.respond_to?(:to_int)
    slice_start_length(arg1.to_int, arg2.to_int)
  elsif arg1.is_a?(Range) and arg2.nil?
    slice_range(arg1)
  else
    raise TypeError, "can't convert #{arg1} into Integer" unless arg1.respond_to?(:to_int)
    raise TypeError, "can't convert #{arg2} into Integer" unless arg2.respond_to?(:to_int)
  end
end

#[]=(index, value) ⇒ Object

Sets the element at index.



176
177
178
179
# File 'lib/bindata/array.rb', line 176

def []=(index, value)
  extend_array(index)
  elements[index].assign(value)
end

#assign(array) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/bindata/array.rb', line 96

def assign(array)
  raise ArgumentError, "can't set a nil value for #{debug_name}" if array.nil?

  @element_list = to_storage_formats(array.to_ary)
end

#at(index) ⇒ Object

Returns the element at index. Unlike slice, if index is out of range the array will not be automatically extended.



171
172
173
# File 'lib/bindata/array.rb', line 171

def at(index)
  elements[index]
end

#clearObject



92
93
94
# File 'lib/bindata/array.rb', line 92

def clear
  @element_list = nil
end

#clear?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/bindata/array.rb', line 88

def clear?
  @element_list.nil? or elements.all? { |el| el.clear? }
end

#concat(array) ⇒ Object



129
130
131
132
# File 'lib/bindata/array.rb', line 129

def concat(array)
  insert(-1, *array.to_ary)
  self
end

#debug_name_of(child) ⇒ Object

:nodoc:



225
226
227
228
# File 'lib/bindata/array.rb', line 225

def debug_name_of(child) #:nodoc:
  index = find_index_of(child)
  "#{debug_name}[#{index}]"
end

#do_num_bytesObject

:nodoc:



249
250
251
# File 'lib/bindata/array.rb', line 249

def do_num_bytes #:nodoc:
  sum_num_bytes_for_all_elements
end

#do_read(io) ⇒ Object

:nodoc:



237
238
239
240
241
242
243
# File 'lib/bindata/array.rb', line 237

def do_read(io) #:nodoc:
  if has_parameter?(:initial_length)
    elements.each { |el| el.do_read(io) }
  elsif has_parameter?(:read_until)
    read_until(io)
  end
end

#do_write(io) ⇒ Object

:nodoc:



245
246
247
# File 'lib/bindata/array.rb', line 245

def do_write(io) #:nodoc:
  elements.each { |el| el.do_write(io) }
end

#eachObject



221
222
223
# File 'lib/bindata/array.rb', line 221

def each
  elements.each { |el| yield el }
end

#empty?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/bindata/array.rb', line 212

def empty?
  length.zero?
end

#find_index(obj) ⇒ Object Also known as: index



106
107
108
# File 'lib/bindata/array.rb', line 106

def find_index(obj)
  elements.index(obj)
end

#find_index_of(obj) ⇒ Object

Returns the first index of obj in self.

Uses equal? for the comparator.



114
115
116
# File 'lib/bindata/array.rb', line 114

def find_index_of(obj)
  elements.index { |el| el.equal?(obj) }
end

#first(n = nil) ⇒ Object

Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.



184
185
186
187
188
189
190
191
192
193
# File 'lib/bindata/array.rb', line 184

def first(n = nil)
  if n.nil? and empty?
    # explicitly return nil as arrays grow automatically
    nil
  elsif n.nil?
    self[0]
  else
    self[0, n]
  end
end

#initialize_instanceObject



84
85
86
# File 'lib/bindata/array.rb', line 84

def initialize_instance
  @element_list = nil
end

#initialize_shared_instanceObject



80
81
82
# File 'lib/bindata/array.rb', line 80

def initialize_shared_instance
  @element_prototype = get_parameter(:type)
end

#insert(index, *objs) ⇒ Object



134
135
136
137
138
# File 'lib/bindata/array.rb', line 134

def insert(index, *objs)
  extend_array(index - 1)
  elements.insert(index, *to_storage_formats(objs))
  self
end

#last(n = nil) ⇒ Object

Returns the last element, or the last n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.



198
199
200
201
202
203
204
205
# File 'lib/bindata/array.rb', line 198

def last(n = nil)
  if n.nil?
    self[-1]
  else
    n = length if n > length
    self[-n, n]
  end
end

#lengthObject Also known as: size



207
208
209
# File 'lib/bindata/array.rb', line 207

def length
  elements.length
end

#offset_of(child) ⇒ Object

:nodoc:



230
231
232
233
234
235
# File 'lib/bindata/array.rb', line 230

def offset_of(child) #:nodoc:
  index = find_index_of(child)
  sum = sum_num_bytes_below_index(index)

  child.do_num_bytes.is_a?(Integer) ? sum.ceil : sum.floor
end

#push(*args) ⇒ Object Also known as: <<



118
119
120
121
# File 'lib/bindata/array.rb', line 118

def push(*args)
  insert(-1, *args)
  self
end

#snapshotObject



102
103
104
# File 'lib/bindata/array.rb', line 102

def snapshot
  elements.collect { |el| el.snapshot }
end

#to_aryObject

Allow this object to be used in array context.



217
218
219
# File 'lib/bindata/array.rb', line 217

def to_ary
  collect { |el| el }
end

#unshift(*args) ⇒ Object



124
125
126
127
# File 'lib/bindata/array.rb', line 124

def unshift(*args)
  insert(0, *args)
  self
end