Class: SClust::Util::SparseLabeledVector

Inherits:
SparseVector
  • Object
show all
Defined in:
lib/sclust/util/sparse_vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SparseVector

#[]

Constructor Details

#initialize(default_value = nil) ⇒ SparseLabeledVector

Returns a new instance of SparseLabeledVector.



69
70
71
72
73
# File 'lib/sclust/util/sparse_vector.rb', line 69

def initialize(default_value=nil) 
    super(default_value) 
    @label_map = {}
    @key_map   = {}
end

Instance Attribute Details

#key_mapObject (readonly)

Map keys to the user-defined label.



64
65
66
# File 'lib/sclust/util/sparse_vector.rb', line 64

def key_map
  @key_map
end

#label_mapObject (readonly)

Map labels to the key the data is stored under.



67
68
69
# File 'lib/sclust/util/sparse_vector.rb', line 67

def label_map
  @label_map
end

Instance Method Details

#delete(key) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/sclust/util/sparse_vector.rb', line 86

def delete(key)
    if super(key)
        label = @key_map.delete(key)
        
        @label_map.delete(label) if label
    end
end

#store(key, value, label = nil) ⇒ Object

Aliased to []=, this stored the (key, value) pair as in the Hash class but accepts an optional 3rd element which will label the key. This populates values in the attributes label_map => key and key_map => label.



77
78
79
80
81
82
83
84
# File 'lib/sclust/util/sparse_vector.rb', line 77

def store(key, value, label=nil)
    super(key, value)
    
    if label
        @label_map[label] = key
        @key_map[key] = label
    end
end