Coder Social home page Coder Social logo

sorted_set's Introduction

SortedSet

Hex.pm Travis

A sorted set library for Elixir. Implements the Set protocol.

Installation

Add the following to deps section of your mix.exs: {:sorted_set, "~> 1.0"}

and then mix deps.get. That's it!

Generate the documentation with mix docs.

About

Sorted sets are backed by a red-black tree, providing lookup in O(log(n)). Size is tracked automatically, resulting in O(1) performance.

Basic Usage

SortedSet implements the Set behaviour, Enumerable, and Collectable.

SortedSet.new()
|> Set.put(5)
|> Set.put(1)
|> Set.put(3)
|> Enum.reduce([], fn (element, acc) -> [element*2|acc] end)
|> Enum.reverse
# => [2, 6, 10]

Custom Comparison

Sorted Set can also take a custom :comparator function to determine ordering. The function should accept two terms and

  • return 0 if they are considered equal
  • return -1 if the first is considered less than or before the second
  • return 1 if the first is considered greater than or after the second

This function is passed on to the underlying red-black tree implementation implemetation. Otherwise, the default Erlang term comparison is used (with an extra bit to handle edgecases โ€” see note in RedBlackTree README.)

SortedSet.new([:a, :b, :c], comparator: fn (term1, term2) ->
   RedBlackTree.compare_terms(term1, term2) * -1
 end)
# => #SortedSet<[:c, :b, :a]>

sorted_set's People

Contributors

lowks avatar tyre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sorted_set's Issues

Release tags

Hi,

Not really an issue, but could you set release tags for sorted_set and red_black_tree? It would be useful to build FreeBSD packages and stay up to date.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.