ruby_priority_queue 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e19e98409ae9c0aaec42725d65e361f3678cd81cf9bdeb221e367ae292fc1bc
4
- data.tar.gz: 1cf81d6996ca45614ac5bf21c3affad81f75a2f0db0ec9ea920131075c821d98
3
+ metadata.gz: 283341222cc9a9cd99ac44326bb67ace91206a4154f542be277823599fcd5d31
4
+ data.tar.gz: 289ee1d3a549e00e4f2f20bf62e3202a708418f178667e393df29204b97381d8
5
5
  SHA512:
6
- metadata.gz: d71ff39084ed7aeffeb817c0d81506ee67fdaa81fd7bb229d739f236390198334417c5ed491b1a0dd47248e2f129c8784b06ba904e15f9949829af91595ff5e3
7
- data.tar.gz: 83bb120de89baf7c6cdac61cb6b8092f22419b1f5a71314152071c3dff4c1c8e3c55f596589cb91c85cf1e877e51a6ae8e4a4327cb10cf58d22e93a5dbd8f28b
6
+ metadata.gz: 212eb825cfd93ff62de3d1c8632fa422cd0711ee9c502b7bbcb19348f77bceb007777ff59202375a7e2256eefccddceffb3dddb0e86ff811179b22c7a22d333a
7
+ data.tar.gz: 83f7a1340a1de4a0decf694972da9cd8b091adc3d362bb08640d6a8afaf1ccb3e2c3aed9eb2c4e4b898c4faf4ef072cd94094757796bbe356eb6ba158277ab88
data/README.md CHANGED
@@ -1,34 +1,95 @@
1
1
  # RubyPriorityQueue
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ The `RubyPriorityQueue` gem provides a simple implementation of a priority queue in Ruby. A priority queue is a data structure where each element is associated with a priority, and elements are served based on their priority.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_priority_queue`. To experiment with that code, run `bin/console` for an interactive prompt.
6
5
 
7
- ## Installation
6
+ ## Table of Contents
7
+
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Development](#development)
11
+ - [Contributing](#contributing)
12
+ - [License](#license)
8
13
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+
15
+ ## Installation
10
16
 
11
17
  Install the gem and add to the application's Gemfile by executing:
12
18
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ $ bundle add ruby_priority_queue
14
20
 
15
21
  If bundler is not being used to manage dependencies, install the gem by executing:
16
22
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ $ gem install ruby_priority_queue
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ Here's how you can use the `RubyPriorityQueue` gem:
28
+
29
+ ```ruby
30
+ require 'ruby_priority_queue'
31
+
32
+ # Create a new priority queue
33
+ pq = RubyPriorityQueue::PriorityQueue.new
34
+
35
+ # Push elements with their priorities (lower value means higher priority, if you want to use a max heap, you can negate the priority)
36
+ pq.push('task1', 1)
37
+ # You can also use keyword arguments
38
+ pq.push(item: 'task2', priority: 2)
39
+
40
+ # Pop the element with the highest priority
41
+ item, priority = pq.pop
42
+ puts "Item: #{item}, Priority: #{priority}" # Output: Item: task1, Priority: 1
43
+
44
+ # Check if the priority queue is empty
45
+ puts pq.empty? # Output: false
46
+
47
+ # Peek the item with the highest priority
48
+ item, priority = pq.peek
49
+ puts "Item: #{item}, Priority: #{priority}" # Output: Item: task2, Priority: 2
50
+
51
+ # Get the size of the priority queue
52
+ puts pq.size # Output: 1
53
+ ```
22
54
 
23
55
  ## Development
24
56
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+ To contribute to the development of this gem, follow these steps:
58
+
59
+ 1. Clone the repository:
60
+ ```bash
61
+ $ git clone https://github.com/caovanbi235/ruby_priority_queue.git
62
+ ```
63
+
64
+ 2. Install dependecies:
65
+ ```bash
66
+ $ bin/setup
67
+ ```
68
+
69
+ 3. Run tests:
70
+ ```bash
71
+ $ rake spec
72
+ ```
73
+
74
+ 4. Experiment with the code:
75
+ ```bash
76
+ $ bin/console
77
+ ```
78
+
79
+ 5. To install this gem onto your local machine, run:
80
+ ```bash
81
+ $ bundle exec rake install
82
+ ```
26
83
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+ 6. To release a new version, update the version number in `version.rb`, and then run:
85
+ ```bash
86
+ $ bundle exec rake release
87
+ ```
88
+ This will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
89
 
29
90
  ## Contributing
30
91
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_priority_queue.
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/caovanbi235/ruby_priority_queue.
32
93
 
33
94
  ## License
34
95
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyPriorityQueue
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -14,6 +14,7 @@ module RubyPriorityQueue
14
14
  # - `pop`: Removes and returns the item with the highest priority.
15
15
  # - `empty?`: Checks if the queue is empty.
16
16
  # - `peek`: Returns the item with the highest priority without removing it.
17
+ # - `size`: Returns the number of elements in the queue.
17
18
  #
18
19
  # Example usage:
19
20
  # pq = RubyPriorityQueue::PriorityQueue.new
@@ -21,55 +22,90 @@ module RubyPriorityQueue
21
22
  # pq.push("task2", 2)
22
23
  # pq.pop # => ["task1", 1]
23
24
  class PriorityQueue
25
+ # The Node class represents an element in the priority queue.
26
+ # Each node contains an item and its associated priority.
27
+ #
28
+ # The Node class includes the Comparable module to allow comparison
29
+ # between nodes based on their priority.
30
+ class Node
31
+ include Comparable
32
+ attr_accessor :item, :priority
33
+
34
+ # Initializes a new Node with the given item and priority.
35
+ #
36
+ # @param item [Object] the item to be stored in the node
37
+ # @param priority [Integer] the priority of the item
38
+ def initialize(item, priority)
39
+ @item = item
40
+ @priority = priority
41
+ end
42
+
43
+ # Compares this node with another node based on their priority.
44
+ #
45
+ # @param other [Node] the other node to compare with
46
+ # @return [Integer] -1, 0, or 1 if this node's priority is less than, equal to, or greater than the other node's priority
47
+ def <=>(other)
48
+ priority <=> other.priority
49
+ end
50
+ end
51
+
24
52
  def initialize
25
53
  @heap = []
26
54
  end
27
55
 
28
- def push(item, priority)
29
- @heap << [item, priority]
56
+ def push(item = nil, priority = nil, **kwargs)
57
+ item ||= kwargs[:item]
58
+ priority ||= kwargs[:priority]
59
+ @heap << Node.new(item, priority)
30
60
  bubble_up(@heap.size - 1)
31
61
  end
32
62
 
63
+ # @return [Array] an array containing the item and its priority
33
64
  def pop
34
65
  return if @heap.empty?
35
66
 
36
67
  swap(0, @heap.size - 1)
37
68
  min = @heap.pop
38
69
  bubble_down(0)
39
- min
70
+ [min.item, min.priority]
40
71
  end
41
72
 
42
73
  def empty?
43
74
  @heap.empty?
44
75
  end
45
76
 
77
+ # @return [Array] an array containing the item and its priority
46
78
  def peek
47
- @heap[0]
79
+ [@heap[0].item, @heap[0].priority]
80
+ end
81
+
82
+ def size
83
+ @heap.size
48
84
  end
49
85
 
50
86
  private
51
87
 
52
88
  def bubble_up(index)
53
89
  parent_index = (index - 1) / 2
54
- return if index <= 0 || @heap[parent_index][1] <= @heap[index][1]
90
+ return if index <= 0 || @heap[parent_index] <= @heap[index]
55
91
 
56
92
  swap(index, parent_index)
57
93
  bubble_up(parent_index)
58
94
  end
59
95
 
60
96
  def bubble_down(index)
61
- child_index = (index * 2) + 1
62
- return if child_index >= @heap.size
97
+ left_child_index = (index * 2) + 1
98
+ return if left_child_index >= @heap.size
63
99
 
64
- right_child_index = child_index + 1
65
- if right_child_index < @heap.size && @heap[right_child_index][1] < @heap[child_index][1]
66
- child_index = right_child_index
100
+ right_child_index = left_child_index + 1
101
+ if right_child_index < @heap.size && @heap[right_child_index] < @heap[left_child_index]
102
+ left_child_index = right_child_index
67
103
  end
68
104
 
69
- return if @heap[index][1] <= @heap[child_index][1]
105
+ return if @heap[index] <= @heap[left_child_index]
70
106
 
71
- swap(index, child_index)
72
- bubble_down(child_index)
107
+ swap(index, left_child_index)
108
+ bubble_down(left_child_index)
73
109
  end
74
110
 
75
111
  def swap(source, target)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_priority_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cao Van Bi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-19 00:00:00.000000000 Z
11
+ date: 2025-02-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A priority queue implementation in Ruby
14
14
  email: