Skip to content

Commit

Permalink
Rename dict to hash to match Ruby class name
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 18, 2024
1 parent 941a886 commit e9dcbd6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SortedContainers is a _fast_ implementation of sorted lists, sets, and dictionaries in pure Ruby. It is based on the [sortedcontainers](https://grantjenks.com/docs/sortedcontainers/) Python library by Grant Jenks.

SortedContainers provides three main classes: `SortedArray`, `SortedSet`, and `SortedDict`. Each class is a drop-in replacement for the corresponding Ruby class, but with the added benefit of maintaining the elements in sorted order.
SortedContainers provides three main classes: `SortedArray`, `SortedSet`, and `SortedHash`. Each class is a drop-in replacement for the corresponding Ruby class, but with the added benefit of maintaining the elements in sorted order.

SortedContainers exploits the fact that modern computers are really good at shifting elements around in memory. We sacrifice theroetical time complexity for practical performance. In practice, SortedContainers is _fast_.

Expand Down Expand Up @@ -93,8 +93,8 @@ set.each do |element|
puts element
end

# Create a new SortedDict
dict = SortedContainers::SortedDict.new
# Create a new SortedHash
dict = SortedContainers::SortedHash.new

# Add elements to the dict
dict[3] = 'three'
Expand Down
2 changes: 1 addition & 1 deletion lib/sorted_containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "sorted_containers/version"
require_relative "sorted_containers/sorted_array"
require_relative "sorted_containers/sorted_set"
require_relative "sorted_containers/sorted_dict"
require_relative "sorted_containers/sorted_hash"

module SortedContainers
class Error < StandardError; end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

# The SortedContainers module provides data structures for sorted collections.
module SortedContainers
# The SortedDict class represents a sorted dictionary.
class SortedDict
# The SortedHash class represents a sorted hash.
class SortedHash
include Enumerable

# Initializes a new instance of the SortedDict class.
# Initializes a new instance of the SortedHash class.
#
# @param load_factor [Integer] The load factor for the SortedDict.
# @param load_factor [Integer] The load factor for the SortedHash.
def initialize(load_factor: SortedArray::DEFAULT_LOAD_FACTOR)
@dictionary = {}
@hash = {}
@sorted_array = SortedArray.new(load_factor: load_factor)
end

Expand All @@ -21,7 +21,7 @@ def initialize(load_factor: SortedArray::DEFAULT_LOAD_FACTOR)
# @param key [Object] The key to retrieve the value for.
# @return [Object] The value associated with the key, or nil if the key is not found.
def [](key)
@dictionary[key]
@hash[key]
end

# Associates the specified value with the specified key.
Expand All @@ -31,45 +31,45 @@ def [](key)
# @param value [Object] The value to be associated with the key.
# @return [Object] The value that was associated with the key.
def []=(key, value)
@sorted_array.delete(key) if @dictionary.key?(key)
@sorted_array.delete(key) if @hash.key?(key)
@sorted_array.add(key)
@dictionary[key] = value
@hash[key] = value
end

# Deletes the key-value pair associated with the specified key.
#
# @param key [Object] The key to delete.
# @return [void]
def delete(key)
return unless @dictionary.key?(key)
return unless @hash.key?(key)

@dictionary.delete(key)
@hash.delete(key)
@sorted_array.delete(key)
end

# Returns an array of all the keys in the SortedDict.
# Returns an array of all the keys in the SortedHash.
#
# @return [Array] An array of all the keys.
def keys
@sorted_array.to_a
end

# Returns an array of all the values in the SortedDict.
# Returns an array of all the values in the SortedHash.
#
# @return [Array] An array of all the values.
def values
@sorted_array.to_a.map { |key| @dictionary[key] }
@sorted_array.to_a.map { |key| @hash[key] }
end

# Iterates over each key-value pair in the SortedDict.
# Iterates over each key-value pair in the SortedHash.
#
# @yield [key, value] The block to be executed for each key-value pair.
# @yieldparam key [Object] The key of the current key-value pair.
# @yieldparam value [Object] The value of the current key-value pair.
# @return [void]
def each(&block)
@sorted_array.each do |key|
value = @dictionary[key]
value = @hash[key]
block.call(key, value)
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/sorted_dict_spec.rb → spec/sorted_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# frozen_string_literal: true

RSpec.describe SortedContainers::SortedDict do
RSpec.describe SortedContainers::SortedHash do
it "should return the value for the given key" do
dict = SortedContainers::SortedDict.new
dict = SortedContainers::SortedHash.new
dict[:a] = 1
dict[:b] = 2
dict[:c] = 3
expect(dict[:b]).to eq(2)
end

it "should return the keys in the dictionary" do
dict = SortedContainers::SortedDict.new
it "should return the keys in the hash" do
dict = SortedContainers::SortedHash.new
dict[:a] = 1
dict[:b] = 2
dict[:c] = 3
expect(dict.keys).to eq(%i[a b c])
end

it "should return the values in the dictionary" do
dict = SortedContainers::SortedDict.new
it "should return the values in the hash" do
dict = SortedContainers::SortedHash.new
dict[:a] = 1
dict[:b] = 2
dict[:c] = 3
expect(dict.values).to eq([1, 2, 3])
end

it "should iterate over each key-value pair in the dictionary" do
dict = SortedContainers::SortedDict.new
it "should iterate over each key-value pair in the hash" do
dict = SortedContainers::SortedHash.new
dict[:a] = 1
dict[:b] = 2
dict[:c] = 3
Expand All @@ -35,7 +35,7 @@
end

it "should remove the value for the given key" do
dict = SortedContainers::SortedDict.new
dict = SortedContainers::SortedHash.new
dict[:a] = 1
dict[:b] = 2
dict[:c] = 3
Expand Down

0 comments on commit e9dcbd6

Please sign in to comment.