Giter VIP home page Giter VIP logo

hallux's Introduction

Hallux

build status Hex.pm Hex.pm Hex.pm

A Finger Tree implemenation for Elixir.

Currently, a random access structure (Hallux.Seq) and an interval map (Hallux.IntervalMap) is available for use.

Hallux.Seq

Supports efficient insertion from both left and right ends and random access.

Examples

iex> Hallux.Seq.new
#HalluxSeq<[]>
iex> seq = 1..10 |> Hallux.Seq.new
#HalluxSeq<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>
iex> Hallux.Seq.cons(seq, 0)
#HalluxSeq<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>
iex> Hallux.Seq.snoc(seq, 11)
#HalluxSeq<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]>
iex> Enum.at(seq, 5)
6
iex> Enum.at(seq, 0)
1

Hallux.IntervalMap

A map of closed intervals can be used to find an interval that overlaps with a given interval in O(log(n)), and all m of them in O(m log(n/m)) time.

Examples

iex(1)> im = Enum.into([{{1, 2}, :a}, {{4, 10}, :b}, {{9, 15}, :c}], Hallux.IntervalMap.new())
#HalluxIMap<[{{1, 2}, :a}, {{4, 10}, :b}, {{9, 15}, :c}]>
iex(2)> Hallux.IntervalMap.interval_
interval_match/2     interval_search/2
iex(2)> Hallux.IntervalMap.interval_match(im, {8, 9})
[{{4, 10}, :b}, {{9, 15}, :c}]

Hallux.OrderedMap

An ordered sequence that is also a hashmap on values.

  • Items consist of a value and an order key. Values are hashed.
  • The sequence is always sorted by the order, new items are inserted at their order position.
  • Values can be retrieved in O(log(n)) if addressed by order key.
  • All keys to an item can be retrieved in O(m*log(n)), where m is the number of occurrences of that value. (Currently, hash collisions are not checked.)

Examples

iex(1)> om1 = Hallux.OrderedMap.new([b: 10, c: 20, b: 15, c: 7])
#HalluxOrderedMap<[b: 10, b: 15, c: 20, c: 7]>
iex(2)> om2 = Hallux.OrderedMap.insert(om1, :a, 50)
#HalluxOrderedMap<[a: 50, b: 10, b: 15, c: 20, c: 7]>
iex(3)> Hallux.OrderedMap.get(om2, :b)
#HalluxOrderedMap<[b: 10, b: 15]>
iex(4)> Hallux.OrderedMap.split(om2, :b)
{#HalluxOrderedMap<[a: 50]>, #HalluxOrderedMap<[b: 10, b: 15]>,
 #HalluxOrderedMap<[c: 20, c: 7]>}

Installation

Add hallux to your list of dependencies in mix.exs:

def deps do
  [
    {:hallux, "~> 1.2.0"}
  ]
end

Documentation can be generated with ExDoc. Docs can be found at https://hexdocs.pm/hallux.

References

hallux's People

Contributors

lokilow avatar mynomoto avatar thalesmg avatar turion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hallux's Issues

Add basic benchmarking

In the light of #16 it would be helpful to have a basic benchmark where one can manually, qualitatively, assess what the typical runtimes of basic functions like cons, view_l etc. are.

Generic Enumerable and Collectable implementations

The Enumerable and Collectable protocols can be implemented for Hallux.Internal.FingerTree, as far as I understand. For some operations (like count on Seq) there are more efficient specific functions in the special cases, but sometimes it would be nice to reuse generic functions when implementing a fingertree with a custom monoid.

Is there no O(1) head function?

After reading the original article by Ross & Paterson, I came across the part where they talk about how the performance differs slightly for strict languages as compared to lazy languages. One situation is the head function. In a lazy language, one simply defines head in terms of view_l. But this only has O(1) complexity because the computation of the tail is lazy. In a strict language such as elixir, view_l (

def view_l(%Empty{}), do: nil
) builds up the tail completely, taking O(n), and then in the case of head drops it again.

There are two ways out of this:

  1. Manual laziness annotations using closures for the :m field in the %Deep{} struct
  2. An extra head function that doesn't compute the tail

Option 2. adds some duplicated code and only patches this particular function, but option 1. requires changes in the whole Fingertree module.

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.