Class: Tria

Inherits:
Object
  • Object
show all
Defined in:
lib/tria.rb

Overview

Tria is a multi-dimensional array based tree format, with a parent, child, and descendants for each branch of the tree. Tria was written by Solomon Wise.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_parent, child, descendant) ⇒ Tria

Initializes a new tree



9
10
11
12
13
# File 'lib/tria.rb', line 9

def initialize(init_parent, child, descendant)
  @branches = 0
  @tree = [[init_parent, child, descendant]]
  @branches += 1
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



7
8
9
# File 'lib/tria.rb', line 7

def branches
  @branches
end

Instance Method Details

#add_branch(init_parent, child, descendant) ⇒ Object

Adds a new branch



15
16
17
18
# File 'lib/tria.rb', line 15

def add_branch(init_parent, child, descendant)
  @tree.push([init_parent, child, descendant])
  @branches += 1
end

#add_descendant(branch, descendant) ⇒ Object

Adds a new descendant



20
21
22
# File 'lib/tria.rb', line 20

def add_descendant(branch, descendant)
  @tree[branch - 1].push(descendant)
end

#before_sibling_child(branch) ⇒ Object

Returns the child of the previous sibling



44
45
46
# File 'lib/tria.rb', line 44

def before_sibling_child branch
  @tree[branch - 2][1]
end

#before_sibling_descendants(branch) ⇒ Object

Returns the descendants of the previous sibling



52
53
54
# File 'lib/tria.rb', line 52

def before_sibling_descendants branch
  @tree[branch - 2][2..-1]
end

#before_sibling_parent(branch) ⇒ Object

Returns the parent of the previous sibling



40
41
42
# File 'lib/tria.rb', line 40

def before_sibling_parent branch
  @tree[branch - 2][0]
end

#change_child(branch, child) ⇒ Object

Changes the child of a branch



76
77
78
# File 'lib/tria.rb', line 76

def change_child branch, child
  @tree[branch - 1][1] = child
end

#change_descendant(branch, number, descendant) ⇒ Object

Changes the descendant of a branch



80
81
82
# File 'lib/tria.rb', line 80

def change_descendant branch, number, descendant
  @tree[branch - 1][number + 1] = descendant
end

#change_parent(branch, parent) ⇒ Object

Changes the parent of a branch



72
73
74
# File 'lib/tria.rb', line 72

def change_parent branch, parent
  @tree[branch - 1][0] = parent
end

#child(branch) ⇒ Object

Returns the child of a branch



28
29
30
# File 'lib/tria.rb', line 28

def child branch
  @tree[branch - 1][1]
end

#descendant(branch, number) ⇒ Object

Returns a certain descendant of a branch



32
33
34
# File 'lib/tria.rb', line 32

def descendant branch, number
  @tree[branch - 1][number + 1]
end

#descendants(branch) ⇒ Object

Returns all descendants of a branch



36
37
38
# File 'lib/tria.rb', line 36

def descendants branch
  @tree[branch - 1][2..-1]
end

#next_sibling_child(branch) ⇒ Object

Returns the next sibling’s child



60
61
62
# File 'lib/tria.rb', line 60

def next_sibling_child branch
  @tree[branch][1]
end

#next_sibling_descendant(branch, number) ⇒ Object

Returns a certain descendant of the next sibling



48
49
50
# File 'lib/tria.rb', line 48

def next_sibling_descendant branch, number
  @tree[branch - 2][number + 1]
end

#next_sibling_descendants(branch) ⇒ Object

Returns the next sibling’s descendants



68
69
70
# File 'lib/tria.rb', line 68

def next_sibling_descendants branch
  @tree[branch][2..-1]
end

#next_sibling_parent(branch) ⇒ Object

Returns the next sibling’s parent



56
57
58
# File 'lib/tria.rb', line 56

def next_sibling_parent branch
  @tree[branch][0]
end

#parent(branch) ⇒ Object

Returns the parent of a branch



24
25
26
# File 'lib/tria.rb', line 24

def parent branch
  @tree[branch - 1][0]
end

#to_sObject

Returns a string representation



84
85
86
# File 'lib/tria.rb', line 84

def to_s
  return "Tria-(MemAddress:#{self.object_id})"
end