sorted_containers 0.1.1 → 1.1.0
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 +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +4 -1
- data/README.md +30 -12
- data/lib/sorted_containers/core_extensions.rb +56 -0
- data/lib/sorted_containers/sorted_array.rb +986 -209
- data/lib/sorted_containers/sorted_hash.rb +461 -53
- data/lib/sorted_containers/sorted_set.rb +310 -71
- data/lib/sorted_containers/version.rb +1 -1
- data/lib/sorted_containers.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79ae63c0d79d80ddc1ae621638b598673c2aca5732277582dedcf58c58be89c4
|
4
|
+
data.tar.gz: d6b9be454ce395905449ec323a115e89a9319237dc62a96b70568e061a7a6906
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4faf2986fae2b98c40f8bc58797b437e023c342504bbe0fdcc364b0dd922708030b63bfb979e256794b6f9b9c345d0b0eafb5835561e21b1c1076ef28893a500
|
7
|
+
data.tar.gz: a80d3cbfe66ca19bf137b835684d4162c9ac665641fe70e6e1a2ce1c86a825ad69f0180e48aa65a642b016f133cb9c922aa6e9d5c9da2b8c3c5123646b8948b5
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,30 +1,48 @@
|
|
1
1
|
# SortedContainers
|
2
2
|
|
3
|
-
|
3
|
+
[Documentation](https://www.rubydoc.info/gems/sorted_containers/0.1.1)
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/sorted_containers)
|
6
|
+
|
7
|
+
SortedContainers is a fast implementation of sorted arrays, sets, and hashes in pure Ruby.
|
8
|
+
|
9
|
+
- SortedArray, SortedSet, and SortedHash
|
10
|
+
- Pure Ruby
|
11
|
+
- Fast Performance
|
12
|
+
- (almost) Identical API to Array, Set, and Hash
|
13
|
+
- No dependencies
|
14
|
+
- Benchmarks
|
15
|
+
- Unit Tested
|
16
|
+
|
17
|
+
This library is based on the [sortedcontainers](https://grantjenks.com/docs/sortedcontainers/) Python library by Grant Jenks.
|
4
18
|
|
5
19
|
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
20
|
|
7
|
-
SortedContainers exploits the fact that modern computers are
|
21
|
+
SortedContainers exploits the fact that modern computers are good at shifting arrays in memory. We sacrifice theoretical time complexity for practical performance. In practice, SortedContainers is fast.
|
8
22
|
|
9
23
|
## How it works
|
10
24
|
|
11
|
-
|
25
|
+
Modern 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
26
|
|
13
|
-
For example, if you have the array `[1,
|
27
|
+
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
28
|
|
15
|
-
|
29
|
+
You also save memory by not having to store pointers to children nodes, and you benefit from the cache locality of arrays. When you iterate over a sorted array, you are more likely to access elements that are close together in memory.
|
16
30
|
|
17
|
-
|
31
|
+
But we can do better if we have a lot of elements. We can break up the array so fewer elements have to be moved when a new element is inserted. 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.
|
18
32
|
|
19
|
-
|
33
|
+
This often outperforms the more common tree-based data structures like red-black trees with `O(log n)` insertions, deletions, and lookups. We sacrifice theoretical time complexity for practical performance.
|
34
|
+
|
35
|
+
The size of the subarrays 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.
|
36
|
+
|
37
|
+
SortedSet and SortedHash are implemented using a SortedArray to keep track of the order, and then also use a standard Set and Hash for quick lookups.
|
20
38
|
|
21
39
|
## 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
40
|
|
25
|
-
|
41
|
+
[SortedSet](https://github.com/knu/sorted_set) is a C extension red-black tree implementation. It is the fastest Ruby implementation of a sorted set that I could find. I used it as a benchmark to compare the performance of SortedContainers.
|
26
42
|
|
27
|
-
|
43
|
+
Every test was run 5 times and the average was taken.
|
44
|
+
|
45
|
+
You can see that SortedContainers has comparable performance for add and delete, and much better performance for iteration, initialization, and include.
|
28
46
|
|
29
47
|
- MacBook Pro (16-inch, 2019)
|
30
48
|
- 2.6 GHz 6-Core Intel Core i7, 16 GB 2667 MHz DDR4
|
@@ -59,7 +77,7 @@ bundle install
|
|
59
77
|
```
|
60
78
|
|
61
79
|
Or install it yourself as:
|
62
|
-
|
80
|
+
|
63
81
|
```bash
|
64
82
|
gem install sorted_containers
|
65
83
|
```
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Array class is being extended to include methods for converting
|
4
|
+
# an Array to a SortedSet, SortedHash, and SortedArray.
|
5
|
+
class Array
|
6
|
+
# Converts the array to a SortedSet.
|
7
|
+
#
|
8
|
+
# @param load_factor [Integer] The load factor for the SortedSet.
|
9
|
+
# @return [SortedContainers::SortedSet] The new SortedSet.
|
10
|
+
def to_sorted_set(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
|
11
|
+
SortedContainers::SortedSet.new(self, load_factor: load_factor)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Converts the array to a SortedHash.
|
15
|
+
#
|
16
|
+
# @param load_factor [Integer] The load factor for the SortedHash.
|
17
|
+
# @return [SortedContainers::SortedHash] The new SortedHash.
|
18
|
+
def to_sorted_h(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
|
19
|
+
hash = SortedContainers::SortedHash.new(load_factor: load_factor)
|
20
|
+
hash.merge!(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Converts the array to a SortedArray.
|
24
|
+
#
|
25
|
+
# @param load_factor [Integer] The load factor for the SortedArray.
|
26
|
+
# @return [SortedContainers::SortedArray] The new SortedArray.
|
27
|
+
def to_sorted_a(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
|
28
|
+
SortedContainers::SortedArray.new(self, load_factor: load_factor)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Hash class is being extended to include a method for converting
|
33
|
+
# a Hash to a SortedHash.
|
34
|
+
class Hash
|
35
|
+
# Converts the hash to a SortedHash.
|
36
|
+
#
|
37
|
+
# @param load_factor [Integer] The load factor for the SortedHash.
|
38
|
+
# @return [SortedContainers::SortedHash] The new SortedHash.
|
39
|
+
def to_sorted_h(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
|
40
|
+
hash = SortedContainers::SortedHash.new(load_factor: load_factor)
|
41
|
+
hash.merge!(self)
|
42
|
+
hash
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Set class is being extended to include a method for converting
|
47
|
+
# a Set to a SortedSet.
|
48
|
+
class Set
|
49
|
+
# Converts the set to a SortedSet.
|
50
|
+
#
|
51
|
+
# @param load_factor [Integer] The load factor for the SortedSet.
|
52
|
+
# @return [SortedContainers::SortedSet] The new SortedSet.
|
53
|
+
def to_sorted_set(load_factor: SortedContainers::SortedArray::DEFAULT_LOAD_FACTOR)
|
54
|
+
SortedContainers::SortedSet.new(self, load_factor: load_factor)
|
55
|
+
end
|
56
|
+
end
|