Replaced tab by four spaces
This commit is contained in:
parent
68e9bbcd0f
commit
7e69d4949e
1 changed files with 59 additions and 59 deletions
|
@ -41,7 +41,7 @@ it is going to be called on.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
add(ModuleToUse, Key, Value, DictData) ->
|
add(ModuleToUse, Key, Value, DictData) ->
|
||||||
ModuleToUse:add(Key, Value, DictData).
|
ModuleToUse:add(Key, Value, DictData).
|
||||||
```
|
```
|
||||||
|
|
||||||
This works, and you can vary how you want to pass the data. For
|
This works, and you can vary how you want to pass the data. For
|
||||||
|
@ -51,7 +51,7 @@ bit cleaner.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
add(Key, Value, {ModuleToUse, DictData}) ->
|
add(Key, Value, {ModuleToUse, DictData}) ->
|
||||||
ModuleToUse:add(Key, Value, DictData).
|
ModuleToUse:add(Key, Value, DictData).
|
||||||
```
|
```
|
||||||
|
|
||||||
Either way, there are a few problems with this approach. One of the
|
Either way, there are a few problems with this approach. One of the
|
||||||
|
@ -80,7 +80,7 @@ So what we actually want to do is something mole like this:
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
add(Key, Value, DictData) ->
|
add(Key, Value, DictData) ->
|
||||||
dictionary:add(Key, Value, DictData).
|
dictionary:add(Key, Value, DictData).
|
||||||
```
|
```
|
||||||
|
|
||||||
Doing this we retain the locality. We can easily look up the
|
Doing this we retain the locality. We can easily look up the
|
||||||
|
@ -107,18 +107,18 @@ Behaviour for dictionaries. That Behaviour looks like this:
|
||||||
-export([behaviour_info/1]).
|
-export([behaviour_info/1]).
|
||||||
|
|
||||||
behaviour_info(callbacks) ->
|
behaviour_info(callbacks) ->
|
||||||
[{new, 0},
|
[{new, 0},
|
||||||
{has_key, 2},
|
{has_key, 2},
|
||||||
{get, 2},
|
{get, 2},
|
||||||
{add, 3},
|
{add, 3},
|
||||||
{remove, 2},
|
{remove, 2},
|
||||||
{has_value, 2},
|
{has_value, 2},
|
||||||
{size, 1},
|
{size, 1},
|
||||||
{to_list, 1},
|
{to_list, 1},
|
||||||
{from_list, 1},
|
{from_list, 1},
|
||||||
{keys, 1}];
|
{keys, 1}];
|
||||||
behaviour_info(_) ->
|
behaviour_info(_) ->
|
||||||
undefined.
|
undefined.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ behaviour, starting with `new/1`.
|
||||||
%% @param ModuleName The module name.
|
%% @param ModuleName The module name.
|
||||||
-spec new(module()) -> dictionary(_K, _V).
|
-spec new(module()) -> dictionary(_K, _V).
|
||||||
new(ModuleName) when is_atom(ModuleName) ->
|
new(ModuleName) when is_atom(ModuleName) ->
|
||||||
#dict_t{callback = ModuleName, data = ModuleName:new()}.
|
#dict_t{callback = ModuleName, data = ModuleName:new()}.
|
||||||
```
|
```
|
||||||
|
|
||||||
This code creates a new dictionary for us. Or to be more specific it
|
This code creates a new dictionary for us. Or to be more specific it
|
||||||
|
@ -163,7 +163,7 @@ adding a value.
|
||||||
%% @param Value the value to add
|
%% @param Value the value to add
|
||||||
-spec add(key(K), value(V), dictionary(K, V)) -> dictionary(K, V).
|
-spec add(key(K), value(V), dictionary(K, V)) -> dictionary(K, V).
|
||||||
add(Key, Value, #dict_t{callback = Mod, data = Data} = Dict) ->
|
add(Key, Value, #dict_t{callback = Mod, data = Data} = Dict) ->
|
||||||
Dict#dict_t{data = Mod:add(Key, Value, Data)}.
|
Dict#dict_t{data = Mod:add(Key, Value, Data)}.
|
||||||
```
|
```
|
||||||
|
|
||||||
There are two key things here.
|
There are two key things here.
|
||||||
|
@ -189,7 +189,7 @@ of the dictionary Signature.
|
||||||
%% @throws not_found when the key does not exist
|
%% @throws not_found when the key does not exist
|
||||||
-spec get(key(K), dictionary(K, V)) -> value(V).
|
-spec get(key(K), dictionary(K, V)) -> value(V).
|
||||||
get(Key, #dict_t{callback = Mod, data = Data}) ->
|
get(Key, #dict_t{callback = Mod, data = Data}) ->
|
||||||
Mod:get(Key, Data).
|
Mod:get(Key, Data).
|
||||||
```
|
```
|
||||||
|
|
||||||
In this case, you can see a very similar approach to deconstructing
|
In this case, you can see a very similar approach to deconstructing
|
||||||
|
@ -246,14 +246,14 @@ translation needs to be done. We do that in the `ec_dict:get/2` function.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
-spec get(ec_dictionary:key(K), Object::dictionary(K, V)) ->
|
-spec get(ec_dictionary:key(K), Object::dictionary(K, V)) ->
|
||||||
ec_dictionary:value(V).
|
ec_dictionary:value(V).
|
||||||
get(Key, Data) ->
|
get(Key, Data) ->
|
||||||
case dict:find(Key, Data) of
|
case dict:find(Key, Data) of
|
||||||
{ok, Value} ->
|
{ok, Value} ->
|
||||||
Value;
|
Value;
|
||||||
error ->
|
error ->
|
||||||
throw(not_found)
|
throw(not_found)
|
||||||
end.
|
end.
|
||||||
```
|
```
|
||||||
|
|
||||||
So the `ec_dict` module's purpose for existence is to help the
|
So the `ec_dict` module's purpose for existence is to help the
|
||||||
|
@ -278,10 +278,10 @@ want to test. The first we want to time is the Signature Wrapper, so
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
create_dict() ->
|
create_dict() ->
|
||||||
lists:foldl(fun(El, Dict) ->
|
lists:foldl(fun(El, Dict) ->
|
||||||
dict:store(El, El, Dict)
|
dict:store(El, El, Dict)
|
||||||
end, dict:new(),
|
end, dict:new(),
|
||||||
lists:seq(1,100)).
|
lists:seq(1,100)).
|
||||||
```
|
```
|
||||||
|
|
||||||
The only thing we do here is create a sequence of numbers 1 to 100,
|
The only thing we do here is create a sequence of numbers 1 to 100,
|
||||||
|
@ -295,11 +295,11 @@ dictionary `ec_dict`.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
create_dictionary(Type) ->
|
create_dictionary(Type) ->
|
||||||
lists:foldl(fun(El, Dict) ->
|
lists:foldl(fun(El, Dict) ->
|
||||||
ec_dictionary:add(El, El, Dict)
|
ec_dictionary:add(El, El, Dict)
|
||||||
end,
|
end,
|
||||||
ec_dictionary:new(Type),
|
ec_dictionary:new(Type),
|
||||||
lists:seq(1,100)).
|
lists:seq(1,100)).
|
||||||
```
|
```
|
||||||
|
|
||||||
Here we actually create everything using the Signature. So we don't
|
Here we actually create everything using the Signature. So we don't
|
||||||
|
@ -315,14 +315,14 @@ the `add` function.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
time_direct_vs_signature_dict() ->
|
time_direct_vs_signature_dict() ->
|
||||||
io:format("Timing dict~n"),
|
io:format("Timing dict~n"),
|
||||||
Dict = create_dict(),
|
Dict = create_dict(),
|
||||||
test_avg(fun() ->
|
test_avg(fun() ->
|
||||||
dict:size(dict:store(some_key, some_value, Dict))
|
dict:size(dict:store(some_key, some_value, Dict))
|
||||||
end,
|
end,
|
||||||
1000000),
|
1000000),
|
||||||
io:format("Timing ec_dict implementation of ec_dictionary~n"),
|
io:format("Timing ec_dict implementation of ec_dictionary~n"),
|
||||||
time_dict_type(ec_dict).
|
time_dict_type(ec_dict).
|
||||||
```
|
```
|
||||||
|
|
||||||
The `test_avg` function runs the provided function the number of times
|
The `test_avg` function runs the provided function the number of times
|
||||||
|
@ -337,12 +337,12 @@ implementations. Lets take a look at the `time_dict_type` function.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
time_dict_type(Type) ->
|
time_dict_type(Type) ->
|
||||||
io:format("Testing ~p~n", [Type]),
|
io:format("Testing ~p~n", [Type]),
|
||||||
Dict = create_dictionary(Type),
|
Dict = create_dictionary(Type),
|
||||||
test_avg(fun() ->
|
test_avg(fun() ->
|
||||||
ec_dictionary:size(ec_dictionary:add(some_key, some_value, Dict))
|
ec_dictionary:size(ec_dictionary:add(some_key, some_value, Dict))
|
||||||
end,
|
end,
|
||||||
1000000).
|
1000000).
|
||||||
```
|
```
|
||||||
|
|
||||||
As you can see we take the type as an argument (we need it for `dict`
|
As you can see we take the type as an argument (we need it for `dict`
|
||||||
|
@ -389,10 +389,10 @@ to create a creation helper just like we did for dict.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
create_rbdict() ->
|
create_rbdict() ->
|
||||||
lists:foldl(fun(El, Dict) ->
|
lists:foldl(fun(El, Dict) ->
|
||||||
ec_rbdict:add(El, El, Dict)
|
ec_rbdict:add(El, El, Dict)
|
||||||
end, ec_rbdict:new(),
|
end, ec_rbdict:new(),
|
||||||
lists:seq(1,100)).
|
lists:seq(1,100)).
|
||||||
```
|
```
|
||||||
|
|
||||||
This is exactly the same as `create_dict` with the exception that dict
|
This is exactly the same as `create_dict` with the exception that dict
|
||||||
|
@ -404,14 +404,14 @@ implementation, but we don't for the `ec_dictionary` test.
|
||||||
|
|
||||||
```erlang
|
```erlang
|
||||||
time_direct_vs_signature_rbdict() ->
|
time_direct_vs_signature_rbdict() ->
|
||||||
io:format("Timing rbdict~n"),
|
io:format("Timing rbdict~n"),
|
||||||
Dict = create_rbdict(),
|
Dict = create_rbdict(),
|
||||||
test_avg(fun() ->
|
test_avg(fun() ->
|
||||||
ec_rbdict:size(ec_rbdict:add(some_key, some_value, Dict))
|
ec_rbdict:size(ec_rbdict:add(some_key, some_value, Dict))
|
||||||
end,
|
end,
|
||||||
1000000),
|
1000000),
|
||||||
io:format("Timing ec_dict implementation of ec_dictionary~n"),
|
io:format("Timing ec_dict implementation of ec_dictionary~n"),
|
||||||
time_dict_type(ec_rbdict).
|
time_dict_type(ec_rbdict).
|
||||||
```
|
```
|
||||||
|
|
||||||
And there we have our test. What do the results look like?
|
And there we have our test. What do the results look like?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue