lib/daru/vector.rb in daru-0.0.2.2 vs lib/daru/vector.rb in daru-0.0.2.3
- old
+ new
@@ -1,45 +1,5 @@
-class Array
- def daru_vector
- Daru::Vector.new self
- end
-
- alias_method :dv, :daru_vector
-end
-
-class Range
- def daru_vector
- Daru::Vector.new self
- end
-
- alias_method :dv, :daru_vector
-end
-
-class Hash
- def daru_vector
- Daru::Vector.new self.values[0], self.keys[0]
- end
-
- alias_method :dv, :daru_vector
-end
-
-class NMatrix
- def daru_vector
- Daru::Vector.new self
- end
-
- alias_method :dv, :daru_vector
-end
-
-class MDArray
- def daru_vector
- Daru::Vector.new self
- end
-
- alias_method :dv, :daru_vector
-end
-
module Daru
class Vector
include Enumerable
def each(&block)
@@ -77,15 +37,17 @@
def []=(index, value)
@vector[index] = value
end
def ==(other)
- other.vector == @vector and other.name == @name
+ other.vector == @vector and other.name == @name and other.size == @size
end
def <<(element)
@vector << element
+
+ @size += 1
end
def to_json
self.to_a.to_json
end
@@ -102,10 +64,16 @@
def first lim=1
lim == 1 ? @vector.first : @vector.first(lim)
end
+ def delete index
+ @vector[index] = nil
+ @vector.compact!
+ @size -= 1
+ end
+
def to_html threshold=15
html = '<table><tr><th>' + @name.to_s + '</th></tr>>'
@vector.to_a.each_with_index do |el,i|
next if threshold < i and i < @arr.length-1
@@ -115,9 +83,19 @@
html += '</table>'
end
def dup
- Daru::Vector.new @vector.dup, @name.dup
+ Daru::Vector.new @vector.dup, @name
+ end
+
+ def daru_vector
+ self
+ end
+
+ alias_method :dv, :daru_vector
+
+ def compact!
+ @vector.compact!
end
end
end
\ No newline at end of file