Class: CharTree::Node

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

Instance Method Summary collapse

Constructor Details

#initialize(id = 0, up = 0, up_position = 0, complete = 0, down = 0, down_position = 0) ⇒ Node

Returns a new instance of Node.



13
14
15
16
17
18
19
20
21
# File 'lib/char_tree/node.rb', line 13

def initialize(id = 0, up = 0, up_position = 0, complete = 0, down = 0, down_position = 0)
  @current 				= 0
  @id							= id
@up   					= up
@up_position  	= up_position
 @complete 			= [complete]*CHARS
@down 					= [down]*CHARS
@down_position 	= [down_position]*CHARS
end

Instance Method Details

#chrObject



125
126
127
# File 'lib/char_tree/node.rb', line 125

def chr
	chr_for(@current)
end

#chr_for(position) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/char_tree/node.rb', line 129

def chr_for(position)
  return '8' if position == MAX - 3 # Ch
  return '1' if position == MAX - 2 # Ll
  return '4' if position == MAX - 1 # Rr
  return 'Ñ' if position == MAX
  return (65+position).chr
end

#completeObject



145
146
147
# File 'lib/char_tree/node.rb', line 145

def complete
	@complete[@current] = 1
end

#complete?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/char_tree/node.rb', line 153

def complete?
	@complete[@current] > 0
end

#currentObject



61
62
63
# File 'lib/char_tree/node.rb', line 61

def current
 	@current
end

#downObject



177
178
179
# File 'lib/char_tree/node.rb', line 177

def down
	@down[@current]
end

#down=(down) ⇒ Object



173
174
175
# File 'lib/char_tree/node.rb', line 173

def down=(down)
	@down[@current] = down
end

#down?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/char_tree/node.rb', line 181

def down?
	@down[@current] > 0
end

#down_positionObject



189
190
191
# File 'lib/char_tree/node.rb', line 189

def down_position
	@down_position[@current]
end

#down_position=(down_position) ⇒ Object



185
186
187
# File 'lib/char_tree/node.rb', line 185

def down_position=(down_position)
	@down_position[@current] = down_position
end

#firstObject



27
28
29
# File 'lib/char_tree/node.rb', line 27

def first
  @current = 0
end

#first_charObject



89
90
91
92
93
94
95
96
# File 'lib/char_tree/node.rb', line 89

def first_char
  previous_current = @current
  self.first
  return true if down? || complete?
  return true if next_char
  @current = previous_current
  return false
end

#from_b(binary) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/char_tree/node.rb', line 203

def from_b(binary)
	@id       			= binary[BYTES_L*0..BYTES_L*1+BYTES_C*0].unpack("L")[0]
			@up   					= binary[BYTES_L*1..BYTES_L*2+BYTES_C*0].unpack("L")[0]
			@up_position  	= binary[BYTES_L*2..BYTES_L*2+BYTES_C*1].unpack("C")[0]
@complete 			= array_from_b :L, BYTES_L, binary[(BYTES_L*2+BYTES_C+CHARS*BYTES_L*0+CHARS*BYTES_C*0)..(BYTES_L*2+BYTES_C+CHARS*BYTES_L*1+CHARS*BYTES_C*0-1)]
			@down 					= array_from_b :L, BYTES_L, binary[(BYTES_L*2+BYTES_C+CHARS*BYTES_L*1+CHARS*BYTES_C*0)..(BYTES_L*2+BYTES_C+CHARS*BYTES_L*2+CHARS*BYTES_C*0-1)]
			@down_position 	= array_from_b :C, BYTES_C, binary[(BYTES_L*2+BYTES_C+CHARS*BYTES_L*2+CHARS*BYTES_C*0)..(BYTES_L*2+BYTES_C+CHARS*BYTES_L*2+CHARS*BYTES_C*1-1)]
end

#go(position) ⇒ Object



117
118
119
# File 'lib/char_tree/node.rb', line 117

def go(position)
  @current = position if position <= MAX
end

#go_for(char) ⇒ Object



121
122
123
# File 'lib/char_tree/node.rb', line 121

def go_for(char)
  @current = position_for(char)
end

#idObject



141
142
143
# File 'lib/char_tree/node.rb', line 141

def id
	@id
end

#id=(id) ⇒ Object



137
138
139
# File 'lib/char_tree/node.rb', line 137

def id=(id)
	@id = up
end

#lastObject



31
32
33
# File 'lib/char_tree/node.rb', line 31

def last
  @current = MAX
end

#last_charObject



98
99
100
101
102
103
104
105
# File 'lib/char_tree/node.rb', line 98

def last_char
  previous_current = @current
  self.last
  return true if down? || complete?
  return true if previous_char
  @current = previous_current
  return false
end

#nextObject



35
36
37
38
39
40
41
42
# File 'lib/char_tree/node.rb', line 35

def next
  @current += 1
  if @current > MAX
    @current = MAX
    return false
  end
  return true
end

#next?Boolean

Returns:

  • (Boolean)


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

def next?
 	@current < MAX
end

#next_charObject



69
70
71
72
73
74
75
76
77
# File 'lib/char_tree/node.rb', line 69

def next_char
  previous_current = @current
  return false unless next?
  self.next
  return true if down? || complete?
  return true if next_char
  @current = previous_current
  return false
end

#not_completeObject



149
150
151
# File 'lib/char_tree/node.rb', line 149

def not_complete
	@complete[@current] = 0
end

#positionObject



65
66
67
# File 'lib/char_tree/node.rb', line 65

def position
 	@current
end

#position_for(char) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/char_tree/node.rb', line 107

def position_for(char)
   c = char.capitalize[0]
   return MAX - 3  if c == '8' # Ch
   return MAX - 2  if c == '1' # Ll
   return MAX - 1  if c == '4' # Rr
   p = c.ord - 65
   p = MAX if p > MAX
   p
end

#previousObject



48
49
50
51
52
53
54
55
# File 'lib/char_tree/node.rb', line 48

def previous
  @current -= 1
  if @current < 0
    @current = 0
    return false
  end
  return true
end

#previous?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/char_tree/node.rb', line 57

def previous?
 	@current > 0
end

#previous_charObject



79
80
81
82
83
84
85
86
87
# File 'lib/char_tree/node.rb', line 79

def previous_char
  previous_current = @current
  return false unless previous?
  self.previous
  return true if down? || complete?
  return true if previous_char
  @current = previous_current
  return false
end

#sizeObject



23
24
25
# File 'lib/char_tree/node.rb', line 23

def size
  SIZE
end

#to_bObject



193
194
195
196
197
198
199
200
201
# File 'lib/char_tree/node.rb', line 193

def to_b
	d  = [@id].pack("L")
  d += [@up].pack("L")
  d += [@up_position].pack("C")
  d += array_to_b :L, @complete
  d += array_to_b :L, @down
  d += array_to_b :C, @down_position
  d
end

#upObject



161
162
163
# File 'lib/char_tree/node.rb', line 161

def up
	@up
end

#up=(up) ⇒ Object



157
158
159
# File 'lib/char_tree/node.rb', line 157

def up=(up)
	@up = up
end

#up_positionObject



169
170
171
# File 'lib/char_tree/node.rb', line 169

def up_position
	@up_position
end

#up_position=(up_position) ⇒ Object



165
166
167
# File 'lib/char_tree/node.rb', line 165

def up_position=(up_position)
	@up_position = up_position
end