summaryrefslogtreecommitdiff
path: root/lib/util.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2020-07-07 21:39:10 +0200
committerhref <href@random.sh>2020-07-07 21:39:51 +0200
commitd6ee134a5957e299c3ad59011df320b3c41e6e61 (patch)
tree29567e6635466f8a3415a935b3cc8a777019f5bc /lib/util.ex
parentbleh (diff)
pouet
Diffstat (limited to 'lib/util.ex')
-rw-r--r--lib/util.ex34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/util.ex b/lib/util.ex
index f515936..22d1034 100644
--- a/lib/util.ex
+++ b/lib/util.ex
@@ -10,4 +10,38 @@ defmodule Util do
|> Float.parse()
end
+ def ets_mutate_select_each(ets, table, spec, fun) do
+ ets.safe_fixtable(table, true)
+ first = ets.select(table, spec, 1)
+ do_ets_mutate_select_each(ets, table, fun, first)
+ after
+ ets.safe_fixtable(table, false)
+ end
+
+ defp do_ets_mutate_select_each(_, _, _, :'$end_of_table') do
+ :ok
+ end
+
+ defp do_ets_mutate_select_each(ets, table, fun, {objs, continuation}) do
+ for obj <- objs, do: fun.(table, obj)
+ do_ets_mutate_select_each(ets, table, fun, ets.select(continuation))
+ end
+
+
+ def ets_mutate_each(ets, table, fun) do
+ ets.safe_fixtable(table, true)
+ first = ets.first(table)
+ do_ets_mutate_each(ets, table, fun, first)
+ after
+ ets.safe_fixtable(table, false)
+ end
+
+ defp do_ets_mutate_each(ets, table, fun, key) do
+ case ets.lookup(table, key) do
+ [elem] -> fun.(table, elem)
+ _ -> nil
+ end
+ do_ets_mutate_each(ets, table, fun, ets.next(table, key))
+ end
+
end