tria 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tria.rb +25 -9
- metadata +1 -1
data/lib/tria.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
=begin
|
2
2
|
Tria is a multi-dimensional array based tree format, with a parent, child, and descendants
|
3
|
-
for each branch of the tree.
|
3
|
+
for each branch of the tree. Tria was written by Solomon Wise.
|
4
4
|
=end
|
5
|
-
# Base class for the tree.
|
6
5
|
class Tria
|
6
|
+
|
7
|
+
attr_reader :branches
|
7
8
|
# Initializes a new tree
|
8
|
-
def initialize(init_parent, child)
|
9
|
-
@
|
9
|
+
def initialize(init_parent, child, descendant)
|
10
|
+
@branches = 0
|
11
|
+
@tree = [[init_parent, child, descendant]]
|
12
|
+
@branches += 1
|
10
13
|
end
|
11
14
|
# Adds a new branch
|
12
|
-
def add_branch(init_parent, child)
|
13
|
-
@tree.push([init_parent, child])
|
15
|
+
def add_branch(init_parent, child, descendant)
|
16
|
+
@tree.push([init_parent, child, descendant])
|
17
|
+
@branches += 1
|
14
18
|
end
|
15
19
|
# Adds a new descendant
|
16
20
|
def add_descendant(branch, descendant)
|
@@ -32,15 +36,19 @@ class Tria
|
|
32
36
|
def descendants branch
|
33
37
|
@tree[branch - 1][2..-1]
|
34
38
|
end
|
35
|
-
# Returns the parent of the
|
39
|
+
# Returns the parent of the previous sibling
|
36
40
|
def before_sibling_parent branch
|
37
41
|
@tree[branch - 2][0]
|
38
42
|
end
|
39
|
-
# Returns the child of the
|
43
|
+
# Returns the child of the previous sibling
|
40
44
|
def before_sibling_child branch
|
41
45
|
@tree[branch - 2][1]
|
42
46
|
end
|
43
|
-
# Returns
|
47
|
+
# Returns a certain descendant of the previous sibling
|
48
|
+
def next_sibling_descendant branch, number
|
49
|
+
@tree[branch - 2][number + 1]
|
50
|
+
end
|
51
|
+
# Returns the descendants of the previous sibling
|
44
52
|
def before_sibling_descendants branch
|
45
53
|
@tree[branch - 2][2..-1]
|
46
54
|
end
|
@@ -52,6 +60,10 @@ class Tria
|
|
52
60
|
def next_sibling_child branch
|
53
61
|
@tree[branch][1]
|
54
62
|
end
|
63
|
+
# Returns a certain descendant of the next sibling
|
64
|
+
def next_sibling_descendant branch, number
|
65
|
+
@tree[branch][number + 1]
|
66
|
+
end
|
55
67
|
# Returns the next sibling's descendants
|
56
68
|
def next_sibling_descendants branch
|
57
69
|
@tree[branch][2..-1]
|
@@ -68,5 +80,9 @@ class Tria
|
|
68
80
|
def change_descendant branch, number, descendant
|
69
81
|
@tree[branch - 1][number + 1] = descendant
|
70
82
|
end
|
83
|
+
# Returns a string representation
|
84
|
+
def to_s
|
85
|
+
return "Tria-(MemAddress:#{self.object_id})"
|
86
|
+
end
|
71
87
|
|
72
88
|
end
|