Sha256: 0373b280ffac7f3207d1d7d28bc76072d43c760191ca686f358fc47b21f3d144

Contents?: true

Size: 731 Bytes

Versions: 1

Compression:

Stored size: 731 Bytes

Contents

# encoding: UTF-8

module TrieFile
  class Node
    CHILD_FIELDS_LENGTH = 6
    HEADER_FIELD_LENGTH = 4

    attr_reader :children
    attr_accessor :value, :byte_pos

    def initialize(value = nil)
      @value = value
      @children = {}
      @byte_pos = 0
    end

    def has_child?(char)
      children.include?(char)
    end

    def child_at(char)
      children[char]
    end

    def add_child(char, node)
      @children[char] = node
    end

    def bytesize
      # add some constants here
      HEADER_FIELD_LENGTH + (children.size * CHILD_FIELDS_LENGTH) + value_bytesize
    end

    def value_bytesize
      value ? value.bytesize : 0
    end

    def value_bytes
      value ? value.bytes : []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trie-file-1.0.0 lib/trie-file/node.rb