summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/zipf/SparseVector.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb
index 3096412..33bb396 100644
--- a/lib/zipf/SparseVector.rb
+++ b/lib/zipf/SparseVector.rb
@@ -1,3 +1,6 @@
+require 'json'
+
+
class SparseVector < Hash
def initialize arg=nil
@@ -18,6 +21,12 @@ class SparseVector < Hash
return v
end
+ def to_h
+ h = {}
+ self.each_pair { |k,v| h[k] = v }
+ return h
+ end
+
def from_h h
h.each_pair { |k,v| self[k] = v }
end
@@ -53,6 +62,20 @@ class SparseVector < Hash
}
end
+ def to_json
+ JSON.dump self.to_h
+ end
+
+ def from_json s
+ from_h JSON.load(s)
+ end
+
+ def self.from_json s
+ v = SparseVector.new
+ v.from_json s
+ return v
+ end
+
def self.from_kv s
v = SparseVector.new
v.from_kv s