Skip to content

Commit

Permalink
Add sorted_set class
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 16, 2024
1 parent 65cc9ae commit 8122eff
Show file tree
Hide file tree
Showing 2 changed files with 66 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 @@ -2,6 +2,7 @@

require_relative "sorted_containers/version"
require_relative "sorted_containers/sorted_list"
require_relative "sorted_containers/sorted_set"

module SortedContainers
class Error < StandardError; end
Expand Down
65 changes: 65 additions & 0 deletions lib/sorted_containers/sorted_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require "set"
require_relative "sorted_list"

# A module that provides sorted container data structures.
module SortedContainers
# The SortedSet class is a sorted set implementation.
class SortedSet
# Initializes a new instance of the SortedSet class.
def initialize
@set = Set.new
@list = SortedContainers::SortedList.new
end

# Adds an item to the sorted set.
#
# @param item [Object] The item to be added.
def add(item)
return if @set.include?(item)

@set.add(item)
@list.add(item)
end

# Adds an item to the sorted set using the `<<` operator.
#
# @param item [Object] The item to be added.
def <<(item)
add(item)
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)
end

# Returns the number of items in the sorted set.
#
# @return [Integer] The number of items.
def size
@list.size
end

# Checks if an item is included in the sorted set.
#
# @param item [Object] The item to be checked.
# @return [Boolean] `true` if the item is included, `false` otherwise.
def include?(item)
@set.include?(item)
end

# Iterates over each item in the sorted set.
#
# @yield [item] Gives each item to the block.
def each(&block)
@list.each(&block)
end
end
end

0 comments on commit 8122eff

Please sign in to comment.