github.com/spider-gazelle/bisect

Library for maintaining sorted Arrays

12 stars
2 dependents
License: MIT

Nothing has been indexed for 1.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  bisect:
    github: spider-gazelle/bisect
    version: ~> 1.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 1.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Crystal Lang Bisect

Provides helpers for dealing with sorted Arrays. It uses binary search to reduce the number of comparisons.

Build Status

Usage

There are two primary functions that you need to know about Bisect.insort and Bisect.bisect.

Bisect.insort adds a new element to the Array, but keeps the Array sorted:

require "bisect"
a = [1, 2, 4]
Bisect.insort(a, 3)
a == [1, 2, 3, 4]

Bisect.bisect gives you the index at which the element would have been inserted:

require "bisect"
a = ['a', 'b', 'd']
Bisect.bisect(a, 'c') == 2

If there are equal elements in the Array then insort will insert the element after the last equal element. Similarly bisect will return the index one higher than the last equal element. If you'd like to add new elements before equal elements, use insort_left and bisect_left. If you need to be explicit then insort_right and bisect_right are aliases for insort and bisect.

The above methods are useful for finding insertion points but can be tricky or awkward to use for common searching tasks. Hence the following methods focus on searching for specific values:

Bisect.find_gt(a, x) return left most value greater than x otherwise nil if no value greater than x exists:

scores = [33, 70, 77, 89, 89, 90, 99, 100]
Bisect.find_gt(scores, 65)  #=> 70
Bisect.find_gt(scores, 70)  #=> 77
Bisect.find_gt(scores, 101) #=> nil

Bisect.find_ge(a, x) returns left most value greater than or equal to x otherwise nil if no value greater than or equal to x exists:

Bisect.find_ge(scores, 65)  #=> 70
Bisect.find_ge(scores, 70)  #=> 70
Bisect.find_ge(scores, 101) #=> nil

Bisect.find_lt returns rightmost value less than x otherwise nil if no value less than x exists:

Bisect.find_lt(scores, 65)  #=> 33
Bisect.find_lt(scores, 70)  #=> 33
Bisect.find_lt(scores, 32)  #=> nil

Bisect.find_le returns rightmost value less than or equal to x otherwise nil if no value less than or equal to x exists:

Bisect.find_le(scores, 65)  #=> 33
Bisect.find_le(scores, 70)  #=> 70
Bisect.find_le(scores, 32)  #=> nil

Core ext

These methods are also available directly on Arrays

require "bisect/ext"
a = [1, 2, 4]
a.insort(3)
a == [1, 2, 3, 4]

scores = [33, 70, 77, 89, 89, 90, 99, 100]
scores.find_gt(70) #=> 77
scores.find_ge(70) #=> 70
scores.find_lt(70) #=> 33
scores.find_le(70) #=> 70