Class: Dawg::MemoryDawg

Inherits:
Object
  • Object
show all
Includes:
Finder, Serialization
Defined in:
lib/dawg/dawg/memory_dawg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finder

#get_childs, #lookup, #query, #set_the_node

Methods included from Serialization

#load_bigint, #load_bool, #load_char, #load_int, #write_bigint, #write_bool, #write_char, #write_int

Constructor Details

#initialize(slice) ⇒ MemoryDawg

Returns a new instance of MemoryDawg.



7
8
9
10
11
12
# File 'lib/dawg/dawg/memory_dawg.rb', line 7

def initialize(slice)
  @slice = slice
  @node_count = get_node_count
  @edge_count = get_edge_count
  set_the_node(root)
end

Instance Attribute Details

#edge_countObject

Returns the value of attribute edge_count.



5
6
7
# File 'lib/dawg/dawg/memory_dawg.rb', line 5

def edge_count
  @edge_count
end

#node_countObject

Returns the value of attribute node_count.



5
6
7
# File 'lib/dawg/dawg/memory_dawg.rb', line 5

def node_count
  @node_count
end

#sliceObject

Returns the value of attribute slice.



5
6
7
# File 'lib/dawg/dawg/memory_dawg.rb', line 5

def slice
  @slice
end

Class Method Details

.load(filename) ⇒ Object



61
62
63
64
65
66
# File 'lib/dawg/dawg/memory_dawg.rb', line 61

def self.load(filename)
  File.open(filename) do |f|
    slice = StringIO.new(f.read)
    return MemoryDawg.new(slice)
  end
end

Instance Method Details

#each_edge(index, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dawg/dawg/memory_dawg.rb', line 44

def each_edge(index, &block)
  pos = NODE_START + (NODE_SIZE * index)
  io = make_io(pos, 8)
  edges_pos = load_int(io)
  edge_count = load_int(io)
  edge_start = NODE_START + NODE_SIZE * @node_count
  position =  edge_start + edges_pos
  io = make_io(position, edge_count * EDGE_SIZE)

  edge_count.times do
    hash = load_bigint(io)
    char = load_char(io)
    node_index = load_int(io)
    yield char, node_index
  end
end

#get_edge_countObject



29
30
31
# File 'lib/dawg/dawg/memory_dawg.rb', line 29

def get_edge_count
  load_int(make_io(4,4))
end

#get_node_by_index(index) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/dawg/dawg/memory_dawg.rb', line 33

def get_node_by_index(index)
  pos = NODE_START + (NODE_SIZE * index)
  io = make_io(pos, NODE_SIZE)
  edges_pos = load_int(io)
  edge_count = load_int(io)
  id = load_int(io)
  final = load_bool(io)
  hash = load_bigint(io)
  MemoryNode.new(self, index, edge_count, final, hash, edges_pos)
end

#get_node_countObject



25
26
27
# File 'lib/dawg/dawg/memory_dawg.rb', line 25

def get_node_count
  load_int(make_io(0,4))
end

#make_io(start, size) ⇒ Object



20
21
22
23
# File 'lib/dawg/dawg/memory_dawg.rb', line 20

def make_io(start, size)
  @slice.pos = start
  StringIO.new(@slice.read(size))
end

#rootObject



16
17
18
# File 'lib/dawg/dawg/memory_dawg.rb', line 16

def root
  @root = get_node_by_index(@node_count - 1)
end