Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serialization #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# compiled output
/tmp
Gemfile.lock
/.yardoc
.rake_tasks~
*.so
Expand Down
56 changes: 56 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
PATH
remote: .
specs:
numruby (0.0.1)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.1)
coderay (1.1.3)
json (2.5.1)
method_source (1.0.0)
minitest (5.14.3)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rainbow (3.0.0)
rake (13.0.3)
rake-compiler (0.9.9)
rake
rdoc (4.3.0)
regexp_parser (2.0.3)
rexml (3.2.4)
rubocop (1.8.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.0)
parser (>= 2.7.1.5)
ruby-progressbar (1.11.0)
unicode-display_width (2.0.0)

PLATFORMS
x86_64-linux

DEPENDENCIES
bundler (>= 1.6)
json (>= 1.5.5)
minitest (~> 5.0)
numruby!
pry (~> 0.10)
rake (>= 10.3)
rake-compiler (~> 0.8)
rdoc (~> 4.0, >= 4.0.1)
rubocop (>= 0.49.0)

BUNDLED WITH
2.2.6
40 changes: 40 additions & 0 deletions lib/numruby/nmatrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ def to_a
return self.elements
end

def _dump data
[
self.dim,
self.dtype,
self.stype,
self.shape,
self.elements,
].join(":")
end

def self._load serial
data = serial.split(":")
dim = data[0].to_i
dtype = data[1].to_sym
stype = data[2].to_sym
shape = data[3..(3+dim-1)]
elements = data[(3+dim)..data.length-1]
parsed_elements = []

if dtype == :nm_bool
elements.each do |e|
parsed_elements.push(e == "true" ? true : false)
end
elsif dtype == :nm_float64
elements.each do |e|
parsed_elements.push(e.to_f)
end
else
# Convert to Integer
elements.each do |e|
parsed_elements.push(e.to_i)
end
end
parsed_shape = []
shape.each do |e|
parsed_shape.push(e.to_i)
end
self.new(parsed_shape, parsed_elements, dtype)
end

def inspect #:nodoc:
original_inspect = super()
original_inspect = original_inspect[0...original_inspect.size-1]
Expand Down
12 changes: 12 additions & 0 deletions test/nmatrix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ def test_slicing
assert_equal @m_int[0, 0..1, 0..1], @s_int
end

def test_serializing
serialized_data_float = Marshal.dump(@m)
deserialized_data_float = Marshal.load(serialized_data_float)
serialized_data_int = Marshal.dump(@m_int)
deserialized_data_int = Marshal.load(serialized_data_int)
serialized_data_bool = Marshal.dump(@b)
deserialized_data_bool = Marshal.load(serialized_data_bool)
assert_equal @m, deserialized_data_float
assert_equal @m_int, deserialized_data_int
assert_equal @b, deserialized_data_bool
end

end