Class: Cluster::Tree
- Inherits:
-
Object
- Object
- Cluster::Tree
- Defined in:
- lib/rbcluster/tree.rb,
ext/rbcluster/rbcluster.c
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #cut(nclusters) ⇒ Object
- #fetch(idx, &blk) ⇒ Object
-
#initialize(nodes) ⇒ Tree
constructor
A new instance of Tree.
- #scale ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(nodes) ⇒ Tree
Returns a new instance of Tree.
3 4 5 6 7 8 9 10 11 |
# File 'lib/rbcluster/tree.rb', line 3 def initialize(nodes) @nodes = Array(nodes) @nodes.each_with_index do |node, idx| unless node.kind_of?(Node) raise ArgumentError, "expected #{Node.class}, got #{node.class} at index #{idx}" end end end |
Instance Method Details
#[](idx) ⇒ Object
25 26 27 |
# File 'lib/rbcluster/tree.rb', line 25 def [](idx) @nodes[idx] end |
#cut(nclusters) ⇒ Object
42 43 44 |
# File 'lib/rbcluster/tree.rb', line 42 def cut(nclusters) Cluster.cuttree(@nodes, nclusters) end |
#fetch(idx, &blk) ⇒ Object
29 30 31 |
# File 'lib/rbcluster/tree.rb', line 29 def fetch(idx, &blk) @nodes.fetch(idx, &blk) end |
#scale ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rbcluster/tree.rb', line 33 def scale max = @nodes.map { |e| e.distance }.max @nodes.each do |node| node.distance = node.distance /= max end nil end |
#size ⇒ Object
13 14 15 |
# File 'lib/rbcluster/tree.rb', line 13 def size @nodes.size end |
#to_a ⇒ Object
17 18 19 |
# File 'lib/rbcluster/tree.rb', line 17 def to_a @nodes.dup end |
#to_s ⇒ Object
21 22 23 |
# File 'lib/rbcluster/tree.rb', line 21 def to_s @nodes.map { |e| "#{e}\n" }.join end |