Class: Gdsii::RecData::BitArray

Inherits:
Data
  • Object
show all
Defined in:
lib/gdsii/record/datatypes/bitarray.rb

Overview

Class for BITARRAY data type

Instance Attribute Summary collapse

Attributes inherited from Data

#record, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#[], #is_ascii?, #is_bitarray?, #is_int2?, #is_int4?, #is_no_data?, #is_real4?, #is_real8?, valid_type?

Constructor Details

#initialize(value) ⇒ BitArray

Construct an Gdsii::RecData::BitArray data object. The value is an array of 16-bit integers representing the bit array. Examples:

record = Gdsii::RecData::BitArray.new([1234])
record.value.inspect #=> [1234]

Or alternatively to initialize with an array of strings madeup of 16 1’s and 0’s, use Gdsii::RecData::BitArray.s_a_to_i_a:

arr = Gdsii::RecData::BitArray.s_a_to_i_a(["0000010011010010"])
record = Gdsii::RecData::BitArray.new(arr)
record.value.inspect #=> [1234]


30
31
32
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 30

def initialize(value)
  super(GDT_BITARRAY, value)
end

Instance Attribute Details

#valueObject

Value is an array of 16-bit integers which represent the bit array. To get a String of the bit array represented as an array of strings madeup of 16 1’s and 0’s consider using the #to_s_a or #to_s methods.



15
16
17
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 15

def value
  @value
end

Class Method Details

.read(file, byte_count) ⇒ Object

Reads a BITARRAY record from the given file and for the length of bytes given and returns a new Gdsii::RecData::BitArray object.



48
49
50
51
52
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 48

def BitArray.read(file, byte_count)
  raw = file.read(byte_count)
  data = raw.unpack("n")
  BitArray.new(data)
end

.s_a_to_i_a(value) ⇒ Object

Converts an array of strings at 16 characters in length using 1’s and 0’s to an array of respective integer values.

arr = Gdsii::RecData::BitArray.s_a_to_i_a(["0000010011010010"])
arr.inspect #=> [1234]

Also see #new for an example used in object construction.



92
93
94
95
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 92

def BitArray.s_a_to_i_a(value)
  string_arr = Data.coerce_value(value, String, :to_s)
  string_arr.map {|string| [string].pack("B16").unpack("n")[0]}
end

Instance Method Details

#byte_sizeObject

Returns the size of the record data in bytes. Each array element consumes 2 bytes.



42
43
44
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 42

def byte_size()
  @value.length * 2
end

#to_sObject

Returns a string containing string representations for each of the 16-bit integers in the value joined by spaces. Example:

record = Gdsii::RecData::BitArray.new([1234, 9876])
record.to_s.inspect #=> "0000010011010010 0010011010010100"


79
80
81
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 79

def to_s()
  to_s_a.join(' ')
end

#to_s_aObject

Returns an array containing string representations for each of the 16-bit integers in the value. Example:

record = Gdsii::RecData::BitArray.new([1234, 9876])
record.to_s_a.inspect #=> ["0000010011010010", "0010011010010100"]


68
69
70
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 68

def to_s_a()
  @value.map {|string| [string].pack("n").unpack("B16")[0]}
end

#write(file) ⇒ Object

Writes the integer values representing the bit array in this Gdsii::RecData::BitArray object to the given file as a GDSII BITARRAY record.



57
58
59
# File 'lib/gdsii/record/datatypes/bitarray.rb', line 57

def write(file)
  file.write @value.pack("n")
end