Skip to content

Commit

Permalink
Add core extensions to convert containers to sorted ones
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed May 9, 2024
1 parent d3a8caa commit 9acc807
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/sorted_containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require_relative "sorted_containers/sorted_array"
require_relative "sorted_containers/sorted_set"
require_relative "sorted_containers/sorted_hash"
require_relative "sorted_containers/core_extensions"
53 changes: 53 additions & 0 deletions lib/sorted_containers/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

# Array class is being extended to include methods for converting
# an Array to a SortedSet, SortedHash, and SortedArray.
class Array
# Converts the array to a SortedSet.
#
# @param load_factor [Integer] The load factor for the SortedSet.
# @return [SortedContainers::SortedSet] The new SortedSet.
def to_sorted_set(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
SortedContainers::SortedSet.new(self, load_factor: load_factor)
end

# Converts the array to a SortedHash.
#
# @param load_factor [Integer] The load factor for the SortedHash.
# @return [SortedContainers::SortedHash] The new SortedHash.
def to_sorted_h(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
SortedContainers::SortedHash.new(to_h, load_factor: load_factor)
end

# Converts the array to a SortedArray.
#
# @param load_factor [Integer] The load factor for the SortedArray.
# @return [SortedContainers::SortedArray] The new SortedArray.
def to_sorted_a(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
SortedContainers::SortedArray.new(self, load_factor: load_factor)
end
end

# Hash class is being extended to include a method for converting
# a Hash to a SortedHash.
class Hash
# Converts the hash to a SortedHash.
#
# @param load_factor [Integer] The load factor for the SortedHash.
# @return [SortedContainers::SortedHash] The new SortedHash.
def to_sorted_h(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
SortedContainers::SortedHash.new(self, load_factor: load_factor)
end
end

# Set class is being extended to include a method for converting
# a Set to a SortedSet.
class Set
# Converts the set to a SortedSet.
#
# @param load_factor [Integer] The load factor for the SortedSet.
# @return [SortedContainers::SortedSet] The new SortedSet.
def to_sorted_set(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
SortedContainers::SortedSet.new(self, load_factor: load_factor)
end
end
1 change: 1 addition & 0 deletions lib/sorted_containers/sorted_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SortedHash

# Initializes a new instance of the SortedHash class.
#
# @param hash [Hash] The initial key-value pairs for the SortedHash.
# @param load_factor [Integer] The load factor for the SortedHash.
def initialize(hash = {}, load_factor: SortedArray::DEFAULT_LOAD_FACTOR)
@hash = hash
Expand Down
52 changes: 52 additions & 0 deletions spec/core_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

RSpec.describe Array do
describe "#to_sorted_set" do
it "converts the array to a SortedSet" do
array = [3, 1, 2]
sorted_set = array.to_sorted_set
expect(sorted_set).to be_a(SortedContainers::SortedSet)
expect(sorted_set.to_a).to eq([1, 2, 3])
end
end

describe "#to_sorted_h" do
it "converts the array to a SortedHash" do
array = [["a", 1], ["b", 2], ["c", 3]]
sorted_hash = array.to_sorted_h
expect(sorted_hash).to be_a(SortedContainers::SortedHash)
expect(sorted_hash.to_a).to eq([["a", 1], ["b", 2], ["c", 3]])
end
end

describe "#to_sorted_a" do
it "converts the array to a SortedArray" do
array = [3, 1, 2]
sorted_array = array.to_sorted_a
expect(sorted_array).to be_a(SortedContainers::SortedArray)
expect(sorted_array.to_a).to eq([1, 2, 3])
end
end
end

RSpec.describe Hash do
describe "#to_sorted_h" do
it "converts the hash to a SortedHash" do
hash = { "a" => 1, "b" => 2, "c" => 3 }
sorted_hash = hash.to_sorted_h
expect(sorted_hash).to be_a(SortedContainers::SortedHash)
expect(sorted_hash.to_a).to eq([["a", 1], ["b", 2], ["c", 3]])
end
end
end

RSpec.describe Set do
describe "#to_sorted_set" do
it "converts the set to a SortedSet" do
set = Set.new([3, 1, 2])
sorted_set = set.to_sorted_set
expect(sorted_set).to be_a(SortedContainers::SortedSet)
expect(sorted_set.to_a).to eq([1, 2, 3])
end
end
end

0 comments on commit 9acc807

Please sign in to comment.