diff --git a/lib/sorted_containers.rb b/lib/sorted_containers.rb index 976e006..51c9e2d 100644 --- a/lib/sorted_containers.rb +++ b/lib/sorted_containers.rb @@ -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" diff --git a/lib/sorted_containers/core_extensions.rb b/lib/sorted_containers/core_extensions.rb new file mode 100644 index 0000000..d75c4ec --- /dev/null +++ b/lib/sorted_containers/core_extensions.rb @@ -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 diff --git a/lib/sorted_containers/sorted_hash.rb b/lib/sorted_containers/sorted_hash.rb index 7c1682c..d2b6fb7 100644 --- a/lib/sorted_containers/sorted_hash.rb +++ b/lib/sorted_containers/sorted_hash.rb @@ -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 diff --git a/spec/core_extensions_spec.rb b/spec/core_extensions_spec.rb new file mode 100644 index 0000000..3bdb66b --- /dev/null +++ b/spec/core_extensions_spec.rb @@ -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