Skip to content

Commit

Permalink
Work on API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 18, 2024
1 parent 4b3ae25 commit d755467
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/sorted_containers/sorted_dict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class SortedDict
include Enumerable

# Initializes a new instance of the SortedDict class.
def initialize
def initialize(load_factor: SortedList::DEFAULT_LOAD_FACTOR)
@dictionary = {}
@sorted_list = SortedList.new
@sorted_list = SortedList.new(load_factor: load_factor)
end

# Retrieves the value associated with the specified key.
Expand All @@ -27,7 +27,7 @@ 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_list.remove(key) if @dictionary.key?(key)
@sorted_list.delete(key) if @dictionary.key?(key)
@sorted_list.add(key)
@dictionary[key] = value
end
Expand All @@ -40,7 +40,7 @@ def delete(key)
return unless @dictionary.key?(key)

@dictionary.delete(key)
@sorted_list.remove(key)
@sorted_list.delete(key)
end

# Returns an array of all the keys in the SortedDict.
Expand Down
24 changes: 21 additions & 3 deletions lib/sorted_containers/sorted_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class SortedList
# Initializes a new SortedList object.
#
# @param iterable [Enumerable] An optional iterable object to initialize the list with.
def initialize(iterable = [])
def initialize(iterable = [], load_factor: DEFAULT_LOAD_FACTOR)
@lists = []
@maxes = []
@load_factor = DEFAULT_LOAD_FACTOR
@load_factor = load_factor
@size = 0
update(iterable)
end
Expand Down Expand Up @@ -45,7 +45,7 @@ def <<(value)
add(value)
end

def remove(value)
def delete(value)
i = bisect_left(@maxes, value)
raise "Value not found: #{value}" if i == @maxes.size

Expand All @@ -69,6 +69,24 @@ def [](index)
end
end

# Retrieves the last value in the sorted list.
#
# @return [Object] The last value in the list.
def last
raise "List is empty" if @size.zero?

@lists.last.last
end

# Retrieves the first value in the sorted list.
#
# @return [Object] The first value in the list.
def first
raise "List is empty" if @size.zero?

@lists.first.first
end

# Deletes the value at the specified index.
#
# @param index [Integer] The index of the value to delete.
Expand Down
27 changes: 24 additions & 3 deletions lib/sorted_containers/sorted_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class SortedSet
# Initializes a new instance of the SortedSet class.
#
# @param iterable [Array] The initial elements of the sorted set.
def initialize(iterable = [])
def initialize(iterable = [], load_factor: SortedList::DEFAULT_LOAD_FACTOR)
@set = Set.new(iterable)
@list = SortedContainers::SortedList.new(iterable)
@list = SortedContainers::SortedList.new(iterable, load_factor: load_factor)
end

# Adds an item to the sorted set.
Expand All @@ -34,14 +34,35 @@ def <<(item)
add(item)
end

# Retrieves the item at the specified index.
#
# @param index [Integer] The index of the item to retrieve.
def [](index)
@list[index]
end

# Retrieves the first item in the sorted set.
#
# @return [Object] The first item.
def first
@list.first
end

# Retrieves the last item in the sorted set.
#
# @return [Object] The last item.
def last
@list.last
end

# Removes an item from the sorted set.
#
# @param item [Object] The item to be removed.
def delete(item)
return unless @set.include?(item)

@set.delete(item)
@list.remove(item)
@list.delete(item)
end

# Returns the number of items in the sorted set.
Expand Down
14 changes: 12 additions & 2 deletions spec/sorted_containers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
expect(set.to_a).to eq([1, 2, 4, 5])
end

it "should return the value indexed by the given index" do
set = SortedContainers::SortedSet.new([1, 2, 3, 4, 5])
expect(set[2]).to eq(3)
end

it "should return the number of elements in the set" do
set = SortedContainers::SortedSet.new([1, 2, 3, 4, 5])
expect(set.size).to eq(5)
Expand Down Expand Up @@ -102,14 +107,19 @@
expect(list.to_a).to eq([1, 2, 4, 5, 7])
end

it "should set the load factor to the provided value" do
list = SortedContainers::SortedList.new([], load_factor: 100)
expect(list.instance_variable_get(:@load_factor)).to eq(100)
end

it "should return true if the value is in the list" do
list = SortedContainers::SortedList.new([1, 2, 3, 4, 5])
expect(list.contains(3)).to be true
end

it "should remove the value from the list" do
it "should delete the value from the list" do
list = SortedContainers::SortedList.new([1, 2, 3, 4, 5])
list.remove(3)
list.delete(3)
expect(list.to_a).to eq([1, 2, 4, 5])
end

Expand Down

0 comments on commit d755467

Please sign in to comment.