Skip to content

Instantly share code, notes, and snippets.

@jlouis
Created July 14, 2011 12:15
Show Gist options
  • Save jlouis/1082348 to your computer and use it in GitHub Desktop.
Save jlouis/1082348 to your computer and use it in GitHub Desktop.
ETS variant of circa
-module(ecirca_ets).
-export([new/1,
set/3,
get/2,
push/2,
slice/3]).
-type res() :: {reference(), integer(), integer()}.
-spec new(pos_integer()) -> {ok, res()}.
new(Sz) ->
try
ets:new(circa_tab, [named_table, public])
catch
_:_ ->
ignore % Hack
end,
{ok, {make_ref(), 0, Sz}}.
-spec set(res(), pos_integer(), pos_integer()) -> {ok, res()} |
{error, not_found}.
set({Ref, _C, Sz} = T, I, Val) when I >= 0 andalso I < Sz ->
ets:insert(circa_tab, {{Ref, I}, Val}),
{ok, T}.
-spec get(res(), pos_integer()) -> {ok, pos_integer()} |
{error, not_found}.
get({Ref, _C, Sz}, I) when I >= 0 andalso I < Sz ->
{ok, ets:lookup_element(circa_tab, {Ref, I}, 2)}.
-spec push(res(), pos_integer()) -> {ok, res()}.
push({Ref, C, Sz}, Val) when C > Sz ->
push({Ref, 0, Sz}, Val);
push({Ref, C, Sz}, Val) ->
ets:insert(circa_tab, {{Ref, C}, Val}),
{ok, {Ref, C+1, Sz}}.
-spec slice(res(), pos_integer(), pos_integer()) -> [pos_integer()] |
{error, slice_too_big}.
slice(_Res, _Start, _End) ->
todo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment