tria 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tria.rb +72 -0
  2. metadata +48 -0
@@ -0,0 +1,72 @@
1
+ =begin
2
+ Tria is a multi-dimensional array based tree format, with a parent, child, and descendants
3
+ for each branch of the tree.
4
+ =end
5
+ # Base class for the tree.
6
+ class Tria
7
+ # Initializes a new tree
8
+ def initialize(init_parent, child)
9
+ @tree = [[init_parent, child]]
10
+ end
11
+ # Adds a new branch
12
+ def add_branch(init_parent, child)
13
+ @tree.push([init_parent, child])
14
+ end
15
+ # Adds a new descendant
16
+ def add_descendant(branch, descendant)
17
+ @tree[branch - 1].push(descendant)
18
+ end
19
+ # Returns the parent of a branch
20
+ def parent branch
21
+ @tree[branch - 1][0]
22
+ end
23
+ # Returns the child of a branch
24
+ def child branch
25
+ @tree[branch - 1][1]
26
+ end
27
+ # Returns a certain descendant of a branch
28
+ def descendant branch, number
29
+ @tree[branch - 1][number + 1]
30
+ end
31
+ # Returns all descendants of a branch
32
+ def descendants branch
33
+ @tree[branch - 1][2..-1]
34
+ end
35
+ # Returns the parent of the left sibling
36
+ def before_sibling_parent branch
37
+ @tree[branch - 2][0]
38
+ end
39
+ # Returns the child of the left sibling
40
+ def before_sibling_child branch
41
+ @tree[branch - 2][1]
42
+ end
43
+ # Returns the descendants of the left sibling
44
+ def before_sibling_descendants branch
45
+ @tree[branch - 2][2..-1]
46
+ end
47
+ # Returns the next sibling's parent
48
+ def next_sibling_parent branch
49
+ @tree[branch][0]
50
+ end
51
+ # Returns the next sibling's child
52
+ def next_sibling_child branch
53
+ @tree[branch][1]
54
+ end
55
+ # Returns the next sibling's descendants
56
+ def next_sibling_descendants branch
57
+ @tree[branch][2..-1]
58
+ end
59
+ # Changes the parent of a branch
60
+ def change_parent branch, parent
61
+ @tree[branch - 1][0] = parent
62
+ end
63
+ # Changes the child of a branch
64
+ def change_child branch, child
65
+ @tree[branch - 1][1] = child
66
+ end
67
+ # Changes the descendant of a branch
68
+ def change_descendant branch, number, descendant
69
+ @tree[branch - 1][number + 1] = descendant
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tria
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Solomon Wise
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Tria is a multi-dimensional array based tree format, with a parent,
15
+ child, and descendants
16
+
17
+ for each branch of the tree.'
18
+ email: slmnwise@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/tria.rb
24
+ homepage:
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Tria
48
+ test_files: []