-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell_store.erl
46 lines (37 loc) · 1.22 KB
/
cell_store.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-module(cell_store).
-compile(export_all).
% This module provides a simple interface to an ETS table for storing/retrieving the location of
% living cells.
-define(TABLE, cell_store).
% Set up an empty cell store
init() ->
case ets:info(?TABLE) of
undefined ->
ets:new(?TABLE, [public, named_table]);
_ ->
ets:delete_all_objects(?TABLE)
end.
% Set the state of a cell ('true' for occupied, 'false' otherwise)
set_cell(Pos, true) ->
% Wrap the coordinates in a tuple so that the index is on the tuple {X,Y} rather
% than just a single coordinate
ets:insert(?TABLE, {Pos});
set_cell(Pos, false) ->
ets:delete(?TABLE, Pos).
% Get the value of a cell
get_cell(Pos) ->
ets:lookup(?TABLE, Pos) =/= [].
% Get a list of all occupied cells
all_cells() ->
[C || {C} <- ets:tab2list(?TABLE)].
% Save the current state to a persistant store
save() ->
{ok, Dets} = dets:open_file(?TABLE, []),
ets:to_dets(?TABLE, Dets), % to_dets clears the dets table first, so no need to do it explicitly
dets:close(Dets).
% Load a state saved with save/0
load() ->
{ok, Dets} = dets:open_file(?TABLE, []),
ets:delete_all_objects(?TABLE), % ets:from_dets doesn't empty the table - do it ourselves
ets:from_dets(?TABLE, Dets),
dets:close(Dets).