sorted_containers 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd886e24f8bbfd92da63127cbd797e35b6df9b0329544c124bcf905cb982c571
4
- data.tar.gz: d2dd2c22556f3c68574a98b3d2d48d1e1737631f366ae402f46ac209ed15803b
3
+ metadata.gz: e7421a03c3463e6e190af04238230eca2120c195bcd711a8fc00eb0ed03ee662
4
+ data.tar.gz: 608647f3204d54ee2a0aeec7a77d3bd8050d8c14e78638b0f4edd604d61618f7
5
5
  SHA512:
6
- metadata.gz: 797036401292e34536feef71b1f16b120f6e06554d64f0a979e738a28c911cc45f819449cb9e87ac6751c92eab3fbfce662a97bb24c8ce45129188b346b6bd96
7
- data.tar.gz: 36a7f3a4ebf748e04b33923013b079e21f1b4bdeee41ca57930202f8361cc8a195be8c4f1b4b3580d3ac345a7819e520b92aee57a85bacab239e461e79bd4a49
6
+ metadata.gz: 45fdc56cd4e5593b218376240935d4ff31e3b3f010ece456e72a6334752a28a5f336852a367f8428f1967bbfee49f855ff2d7c14fb5f76af187f6bdf11ffc1ca
7
+ data.tar.gz: 405354a1b5125f4116348906a5694e66aab47d94e9aa3524335683a04c4cb96e0a0ae181ec164114a00f4fc9ccc8264c68ebf460074b45c0f8f74d2891246ef2
data/.rubocop.yml CHANGED
@@ -2,9 +2,6 @@ AllCops:
2
2
  TargetRubyVersion: 3.0
3
3
  NewCops: enable
4
4
 
5
- Metrics/MethodLength:
6
- Max: 15
7
-
8
5
  Metrics/BlockLength:
9
6
  Exclude:
10
7
  - "**/*_spec.rb"
data/README.md CHANGED
@@ -1,24 +1,141 @@
1
1
  # SortedContainers
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ 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.
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/sorted_containers`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ 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.
6
+
7
+ SortedContainers exploits the fact that modern computers are really good at shifting arrays in memory. We sacrifice theroetical time complexity for practical performance. In practice, SortedContainers is fast.
8
+
9
+ ## How it works
10
+
11
+ Computers are good at shifting arrays. For that reason, it's often faster to keep an array sorted than to use the usual tree-based data structures.
12
+
13
+ For example, if you have the array `[1, 2, 4, 5]` and want to insert the element `3`, you can shift `4, 5` to the right and insert `3` in the correct position. This is a `O(n)` operation, but in practice it's fast.
14
+
15
+ But we can do better if we have a lot of elements. We can break up the array into smaller arrays so the shifts don't have to move so many elements. For example, if you have the array `[[1,2,4], [5,6,7]]` and you want to insert the element `3`, you can insert `3` into the first array to get `[[1,2,3,4], [5,6,7]]` and only the element `4` has to be shifted.
16
+
17
+ This often outperforms the more common tree-based data structures like red-black trees with `O(log n)` insertions and deletions. In practice, the `O(n)` insertions and deletions of SortedContainers are faster.
18
+
19
+ How big the subarrays are is a trade-off. You can modify how big you want to subarrays by setting the `load_factor`. The default is set to DEFAULT_LOAD_FACTOR = 1000. The subarray is split when its size is `2*load_factor`. There is no perfect value. The ideal value will depend on your use case and may require some experimentation.
20
+
21
+ ## Benchmarks
22
+
23
+ Performance comparison against [SortedSet](https://github.com/knu/sorted_set) a C extension red-black tree implementation. Every test was run 5 times and the average was taken.
24
+
25
+ You can see that SortedContainers has compariable performance for add and delete, and much better performance for iteration, initialization, and include.
26
+
27
+ Note: I do not know why initialization is faster for 4 million than 3 million elements. This was consistant across multiple runs.
28
+
29
+ - MacBook Pro (16-inch, 2019)
30
+ - 2.6 GHz 6-Core Intel Core i7, 16 GB 2667 MHz DDR4
31
+ - Ruby 3.2.2
32
+ - SortedContainers 0.1.0
33
+ - SortedSet 1.0.3
34
+ ### Results (Lower is better)
35
+ <img src="benchmark/initialize_performance_comparison.png" width="50%">
36
+ <img src="benchmark/add_performance_comparison.png" width="50%">
37
+ <img src="benchmark/delete_performance_comparison.png" width="50%">
38
+ <img src="benchmark/iteration_performance_comparison.png" width="50%">
39
+ <img src="benchmark/include_performance_comparison.png" width="50%">
6
40
 
7
41
  ## Installation
8
42
 
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.
43
+ Add this line to your application's Gemfile:
10
44
 
11
- Install the gem and add to the application's Gemfile by executing:
45
+ ```ruby
46
+ gem 'sorted_containers'
47
+ ```
12
48
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
49
+ And then execute:
14
50
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
51
+ ```bash
52
+ bundle install
53
+ ```
16
54
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
55
+ Or install it yourself as:
56
+
57
+ ```bash
58
+ gem install sorted_containers
59
+ ```
18
60
 
19
61
  ## Usage
20
62
 
21
- TODO: Write usage instructions here
63
+ require 'sorted_containers'
64
+
65
+ # Create a new SortedArray
66
+ list = SortedContainers::SortedArray.new
67
+
68
+ # Add elements to the list
69
+ list << 3
70
+ list << 1
71
+ list << 2
72
+
73
+ # Access elements by index
74
+ puts list[0] # => 1
75
+ puts list[1] # => 2
76
+ puts list[2] # => 3
77
+
78
+ # Access elements by index
79
+ puts list.first # => 1
80
+ puts list.last # => 3
81
+
82
+ # Remove elements from the list
83
+ list.delete(2)
84
+
85
+ # Iterate over the list
86
+ list.each do |element|
87
+ puts element
88
+ end
89
+
90
+ # Create a new SortedSet
91
+ set = SortedContainers::SortedSet.new
92
+
93
+ # Add elements to the set
94
+ set << 3
95
+ set << 1
96
+ set << 2
97
+
98
+ # Access elements by index
99
+ puts set[0] # => 1
100
+ puts set[1] # => 2
101
+ puts set[2] # => 3
102
+
103
+ # Access elements by index
104
+ puts set.first # => 1
105
+ puts set.last # => 3
106
+
107
+ # Remove elements from the set
108
+ set.delete(2)
109
+
110
+ # Iterate over the set
111
+ set.each do |element|
112
+ puts element
113
+ end
114
+
115
+ # Create a new SortedHash
116
+ dict = SortedContainers::SortedHash.new
117
+
118
+ # Add elements to the dict
119
+ dict[3] = 'three'
120
+ dict[1] = 'one'
121
+ dict[2] = 'two'
122
+
123
+ # Access elements by key
124
+ puts dict[1] # => 'one'
125
+ puts dict[2] # => 'two'
126
+ puts dict[3] # => 'three'
127
+
128
+ # Access elements by index
129
+ puts dict.first # => [1, 'one']
130
+ puts dict.last # => [3, 'three']
131
+
132
+ # Remove elements from the dict
133
+ dict.delete(2)
134
+
135
+ # Iterate over the dict
136
+ dict.each do |key, value|
137
+ puts "#{key}: #{value}"
138
+ end
22
139
 
23
140
  ## Development
24
141
 
@@ -28,7 +145,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
145
 
29
146
  ## Contributing
30
147
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sorted_containers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sorted_containers/blob/main/CODE_OF_CONDUCT.md).
148
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GarrisonJ/sorted_containers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/GarrisonJ/sorted_containers/blob/main/CODE_OF_CONDUCT.md).
32
149
 
33
150
  ## License
34
151
 
@@ -36,4 +153,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
153
 
37
154
  ## Code of Conduct
38
155
 
39
- Everyone interacting in the SortedContainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sorted_containers/blob/main/CODE_OF_CONDUCT.md).
156
+ Everyone interacting in the SortedContainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/GarrisonJ/sorted_containers/blob/main/CODE_OF_CONDUCT.md).