Skip to content

Commit

Permalink
Change char max to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed May 16, 2023
1 parent 8574e79 commit 641c18d
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 170 deletions.
24 changes: 8 additions & 16 deletions lib/rambling/trie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true

%w(
comparable compressible compressor configuration container enumerable
inspectable invalid_operation readers serializers stringifyable nodes
version
comparable compressible compressor configuration container enumerable inspectable invalid_operation
readers serializers stringifyable nodes version
).each do |file|
require File.join('rambling', 'trie', file)
end
Expand Down Expand Up @@ -41,15 +40,12 @@ def create filepath = nil, reader = nil
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
# previous formats. You can also define your own.
# @param [String] filepath the file to load the words from.
# @param [Serializer, nil] serializer the object responsible of loading
# the trie from disk
# @param [Serializer, nil] serializer the object responsible of loading the trie from disk.
# @return [Container] the trie just loaded.
# @yield [Container] the trie just loaded.
# @see Rambling::Trie::Serializers Serializers.
# @note Use of
# {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load
# Marshal.load} is generally discouraged. Only use the +.marshal+
# format with trusted input.
# @note Use of # {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load} is generally
# discouraged. Only use the +.marshal+ format with trusted input.
def load filepath, serializer = nil
serializer ||= serializers.resolve filepath
root = serializer.load filepath
Expand All @@ -64,10 +60,8 @@ def load filepath, serializer = nil
# previous formats. You can also define your own.
# @param [Container] trie the trie to dump into disk.
# @param [String] filepath the file to dump to serialized trie into.
# @param [Serializers::Serializer, nil] serializer the object responsible
# for trie serialization.
# @param [Serializers::Serializer, nil] serializer the object responsible for trie serialization.
# @return [void]
# serializing and dumping the trie into disk.
# @see Serializers Serializers.
def dump trie, filepath, serializer = nil
serializer ||= serializers.resolve filepath
Expand All @@ -76,10 +70,8 @@ def dump trie, filepath, serializer = nil
end

# Provides configuration properties for the +Rambling::Trie+ gem.
# @return [Configuration::Properties] the configured properties of the
# gem.
# @yield [Configuration::Properties] the configured properties of the
# gem.
# @return [Configuration::Properties] the configured properties of the gem.
# @yield [Configuration::Properties] the configured properties of the gem.
def config
yield properties if block_given?
properties
Expand Down
3 changes: 1 addition & 2 deletions lib/rambling/trie/comparable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module Comparable
# Compares two nodes.
# @param [Nodes::Node] other the node to compare against.
# @return [Boolean] +true+ if the nodes' {Nodes::Node#letter #letter} and
# {Nodes::Node#children_tree #children_tree} are equal, +false+
# otherwise.
# {Nodes::Node#children_tree #children_tree} are equal, +false+ otherwise.
def == other
letter == other.letter &&
terminal? == other.terminal? &&
Expand Down
6 changes: 2 additions & 4 deletions lib/rambling/trie/compressible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ module Rambling
module Trie
# Provides the compressible behavior for the trie data structure.
module Compressible
# Indicates if the current {Rambling::Trie::Nodes::Node Node} can be
# compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes
# with one child, +false+ otherwise.
# Indicates if the current {Rambling::Trie::Nodes::Node Node} can be compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes with one child, +false+ otherwise.
def compressible?
!(root? || terminal?) && 1 == children_tree.size
end
Expand Down
14 changes: 2 additions & 12 deletions lib/rambling/trie/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,11 @@ def compress_child_and_merge node
def merge node, other
letter = node.letter.to_s << other.letter.to_s

new_compressed_node(
letter.to_sym,
node.parent,
other.children_tree,
other.terminal?,
)
new_compressed_node letter.to_sym, node.parent, other.children_tree, other.terminal?
end

def compress_children_and_copy node
new_compressed_node(
node.letter,
node.parent,
compress_children(node.children_tree),
node.terminal?,
)
new_compressed_node node.letter, node.parent, compress_children(node.children_tree), node.terminal?
end

def compress_children tree
Expand Down
15 changes: 4 additions & 11 deletions lib/rambling/trie/configuration/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ module Configuration
# Provides configurable properties for Rambling::Trie.
class Properties
# The configured {Readers Readers}.
# @return [ProviderCollection<Readers::Reader>] the mapping of
# configured {Readers Readers}.
# @return [ProviderCollection<Readers::Reader>] the mapping of configured {Readers Readers}.
attr_reader :readers

# The configured {Serializers Serializers}.
# @return [ProviderCollection<Serializers::Serializer>] the mapping of
# configured {Serializers Serializers}.
# @return [ProviderCollection<Serializers::Serializer>] the mapping of configured {Serializers Serializers}.
attr_reader :serializers

# The configured {Compressor Compressor}.
# @return [Compressor] the configured compressor.
attr_accessor :compressor

# The configured +root_builder+, which returns a {Nodes::Node Node}
# when called.
# The configured +root_builder+, which returns a {Nodes::Node Node} when called.
# @return [Proc<Nodes::Node>] the configured +root_builder+.
attr_accessor :root_builder

Expand Down Expand Up @@ -50,11 +47,7 @@ def reset

def reset_readers
plain_text_reader = Rambling::Trie::Readers::PlainText.new

@readers = Rambling::Trie::Configuration::ProviderCollection.new(
:reader,
txt: plain_text_reader,
)
@readers = Rambling::Trie::Configuration::ProviderCollection.new :reader, txt: plain_text_reader
end

def reset_serializers
Expand Down
30 changes: 10 additions & 20 deletions lib/rambling/trie/configuration/provider_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ class ProviderCollection
# Sets the default provider. Needs to be one of the configured
# providers.
# @param [TProvider] provider the provider to use as default.
# @raise [ArgumentError] when the given provider is not in the
# provider collection.
# @raise [ArgumentError] when the given provider is not in the provider collection.
# @note If no providers have been configured, +nil+ will be assigned.
# @return [TProvider, nil] the default provider to use when a provider
# cannot be resolved in {ProviderCollection#resolve #resolve}.
# @return [TProvider, nil] the default provider to use when a provider cannot be resolved in
# {ProviderCollection#resolve #resolve}.
attr_reader :default

# Creates a new provider collection.
Expand All @@ -36,35 +35,28 @@ def initialize name, providers = {}, default = nil
end

# Adds a new provider to the provider collection.
# @param [Symbol] extension the extension that the provider will
# correspond to.
# @param [TProvider] provider the provider to add to the provider
# collection.
# @param [Symbol] extension the extension that the provider will correspond to.
# @param [TProvider] provider the provider to add to the provider collection.
# @return [TProvider] the provider just added.
def add extension, provider
providers[extension] = provider
end

def default= provider
unless contains? provider
raise ArgumentError,
"default #{name} should be part of configured #{name}s"
end
raise ArgumentError, "default #{name} should be part of configured #{name}s" unless contains? provider

@default = provider
end

# List of configured providers.
# @return [Hash<Symbol, TProvider>] the mapping of extensions to their
# corresponding providers.
# @return [Hash<Symbol, TProvider>] the mapping of extensions to their corresponding providers.
def providers
@providers ||= {}
end

# Resolves the provider from a filepath based on the file extension.
# @param [String] filepath the filepath to resolve into a provider.
# @return [TProvider, nil] the provider for the given file's extension.
# {#default} if not found.
# @return [TProvider, nil] the provider for the given file's extension. {#default} if not found.
def resolve filepath
providers[file_format filepath] || default
end
Expand All @@ -88,8 +80,7 @@ def formats
# Get provider corresponding to a given format.
# @param [Symbol] format the format to search for in the collection.
# @return [TProvider] the provider corresponding to that format.
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D
# Hash#[]
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D Hash#[]
def [] format
providers[format]
end
Expand All @@ -113,8 +104,7 @@ def file_format filepath
end

def contains? provider
provider.nil? ||
(providers.any? && provider_instances.include?(provider))
provider.nil? || (providers.any? && provider_instances.include?(provider))
end

alias_method :provider_instances, :values
Expand Down
52 changes: 20 additions & 32 deletions lib/rambling/trie/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,19 @@ def concat words
words.map { |word| add word }
end

# Compresses the existing trie using redundant node elimination. Marks
# the trie as compressed. Does nothing if the trie has already been
# compressed.
# Compresses the existing trie using redundant node elimination.
# Marks the trie as compressed.
# Does nothing if the trie has already been compressed.
# @return [self]
# @note This method replaces the root {Nodes::Raw Raw} node with a
# {Nodes::Compressed Compressed} version of it.
# @note This method replaces the root {Nodes::Raw Raw} node with a {Nodes::Compressed Compressed} version of it.
def compress!
self.root = compress_root unless root.compressed?
self
end

# Compresses the existing trie using redundant node elimination. Returns
# a new trie with the compressed root.
# @return [Container] A new {Container} with the {Nodes::Compressed
# Compressed} root node or self if the trie has already been
# compressed.
# Compresses the existing trie using redundant node elimination. Returns a new trie with the compressed root.
# @return [Container] A new {Container} with the {Nodes::Compressed Compressed} root node
# or self if the trie has already been compressed.
def compress
return self if root.compressed?

Expand All @@ -65,45 +62,40 @@ def compress

# Checks if a path for a word or partial word exists in the trie.
# @param [String] word the word or partial word to look for in the trie.
# @return [Boolean] +true+ if the word or partial word is found, +false+
# otherwise.
# @return [Boolean] +true+ if the word or partial word is found, +false+ otherwise.
# @see Nodes::Node#partial_word?
def partial_word? word = ''
root.partial_word? word.chars
end

# Checks if a whole word exists in the trie.
# @param [String] word the word to look for in the trie.
# @return [Boolean] +true+ only if the word is found and the last
# character corresponds to a terminal node, +false+ otherwise.
# @return [Boolean] +true+ only if the word is found and the last character corresponds to a terminal node,
# +false+ otherwise.
# @see Nodes::Node#word?
def word? word = ''
root.word? word.chars
end

# Returns all words that start with the specified characters.
# @param [String] word the word to look for in the trie.
# @return [Array<String>] all the words contained in the trie that start
# with the specified characters.
# @return [Array<String>] all the words contained in the trie that start with the specified characters.
# @see Nodes::Node#scan
def scan word = ''
root.scan(word.chars).to_a
end

# Returns all words within a string that match a word contained in the
# trie.
# Returns all words within a string that match a word contained in the trie.
# @param [String] phrase the string to look for matching words in.
# @return [Enumerator<String>] all the words in the given string that
# match a word in the trie.
# @return [Enumerator<String>] all the words in the given string that match a word in the trie.
# @yield [String] each word found in phrase.
def words_within phrase
words_within_root(phrase).to_a
end

# Checks if there are any valid words in a given string.
# @param [String] phrase the string to look for matching words in.
# @return [Boolean] +true+ if any word within phrase is contained in the
# trie, +false+ otherwise.
# @return [Boolean] +true+ if any word within phrase is contained in the trie, +false+ otherwise.
# @see Container#words_within
def words_within? phrase
words_within_root(phrase).any?
Expand Down Expand Up @@ -141,33 +133,29 @@ def [] letter
end

# Root node's child nodes.
# @return [Array<Nodes::Node>] the array of children nodes contained in
# the root node.
# @return [Array<Nodes::Node>] the array of children nodes contained in the root node.
# @see Nodes::Node#children
def children
root.children
end

# Root node's children tree.
# @return [Hash<Symbol, Nodes::Node>] the children tree hash contained in
# the root node, consisting of +:letter => node+.
# @return [Hash<Symbol, Nodes::Node>] the children tree hash contained in the root node, consisting of
# +:letter => node+.
# @see Nodes::Node#children_tree
def children_tree
root.children_tree
end

# Indicates if the root {Nodes::Node Node} can be
# compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal}
# nodes with one child, +false+ otherwise.
# Indicates if the root {Nodes::Node Node} can be compressed or not.
# @return [Boolean] +true+ for non-{Nodes::Node#terminal? terminal} nodes with one child, +false+ otherwise.
def compressed?
root.compressed?
end

# Array of words contained in the root {Nodes::Node Node}.
# @return [Array<String>] all words contained in this trie.
# @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-to_a
# Enumerable#to_a
# @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-to_a Enumerable#to_a
def to_a
root.to_a
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rambling/trie/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module Enumerable
include ::Enumerable

# Returns number of words contained in the trie
# @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-count
# Enumerable#count
# @see https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-count Enumerable#count
alias_method :size, :count

# Iterates over the words contained in the trie.
Expand Down
3 changes: 1 addition & 2 deletions lib/rambling/trie/nodes/compressed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Compressed < Rambling::Trie::Nodes::Node
# @raise [InvalidOperation] if the trie is already compressed.
# @return [void]
def add _
raise Rambling::Trie::InvalidOperation,
'Cannot add word to compressed trie'
raise Rambling::Trie::InvalidOperation, 'Cannot add word to compressed trie'
end

# Always return +true+ for a compressed node.
Expand Down
3 changes: 1 addition & 2 deletions lib/rambling/trie/nodes/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
module Rambling
module Trie
module Nodes
# A representation of a missing node in the trie data structure. Returned
# when a node is not found.
# A representation of a missing node in the trie data structure. Returned when a node is not found.
class Missing < Rambling::Trie::Nodes::Node
end
end
Expand Down
Loading

0 comments on commit 641c18d

Please sign in to comment.