LimitedMapSet

View Source

LimitedMapSet is an extension of Elixir’s MapSet that keeps insertion order (FIFO) and automatically evicts the oldest elements when the limit is reached.

Examples

  • Creating a limited set

    iex> s = LimitedMapSet.new(3)
    #LimitedMapSet<limit: 3, size: 0>
    
    iex> s = LimitedMapSet.put(s, :a)
    iex> s = LimitedMapSet.put(s, :b)
    iex> s = LimitedMapSet.put(s, :c)
    iex> LimitedMapSet.to_list(s)
    [:a, :b, :c]
  • Automatic eviction when full

    iex> s = LimitedMapSet.put(s, :d)
    iex> LimitedMapSet.to_list(s)
    [:b, :c, :d]   # :a was evicted
  • Membership and deletion

    iex> LimitedMapSet.member?(s, :b)
    true
    
    iex> s = LimitedMapSet.delete(s, :b)
    iex> LimitedMapSet.member?(s, :b)
    false
  • Creating from a list

    iex> s = LimitedMapSet.new([1, 2, 3, 4], 3)
    iex> LimitedMapSet.to_list(s)
    [2, 3, 4]
  • Checking size

    iex> LimitedMapSet.size(s)
    3
  • Using Enum

    iex> s = LimitedMapSet.new([:a, :b, :c], 3)
    iex> Enum.member?(s, :b)
    true
    iex> Enum.to_list(s)
    [:a, :b, :c]

Add to your mix.exs:

def deps do
  [
    {:limited_map_set, "~> 0.1.0"}
  ]
end