-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add core extensions to convert containers to sorted ones
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |