Permalink
Browse files

Rename Dict -> HashDict and encapsulate it in a record

  • Loading branch information...
1 parent bf28eb0 commit 5e92c8e8db79e0d169e0362a75634db4f8c3957f @alco committed Apr 18, 2012
Showing with 148 additions and 147 deletions.
  1. +18 −18 lib/dict/common.ex
  2. +39 −17 lib/dict/dict.ex
  3. +1 −1 lib/dict/orddict.ex
  4. +0 −21 lib/enum.ex
  5. +48 −48 test/elixir/dict_test.exs
  6. +42 −42 test/elixir/enum_test.exs
View
@@ -1,4 +1,4 @@
-defprotocol PDict, [
+defprotocol Dict, [
# @doc """
# Returns a list containing all dict's keys.
#
@@ -7,7 +7,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.keys [a: 1, b: 2] #=> [:a,:b]
+# Dict.keys [a: 1, b: 2] #=> [:a,:b]
#
# """
keys(dict),
@@ -17,7 +17,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.values [a: 1, b: 2] #=> [1,2]
+# Dict.values [a: 1, b: 2] #=> [1,2]
#
# """
values(dict),
@@ -27,7 +27,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.size [a: 1, b: 2] #=> 2
+# Dict.size [a: 1, b: 2] #=> 2
#
# """
size(dict),
@@ -37,8 +37,8 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.has_key?([a:, 1], :a) #=> true
-# PDict.has_key?([a:, 1], :b) #=> false
+# Dict.has_key?([a:, 1], :a) #=> true
+# Dict.has_key?([a:, 1], :b) #=> false
# """
has_key?(dict, key),
@@ -48,9 +48,9 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.get [a: 1], :a #=> 1
-# PDict.get [a: 1], :b #=> nil
-# PDict.get [a: 1], :b, 3 #=> 3
+# Dict.get [a: 1], :a #=> 1
+# Dict.get [a: 1], :b #=> nil
+# Dict.get [a: 1], :b, 3 #=> 3
# """
get(dict, key),
get(dict, key, default),
@@ -61,9 +61,9 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.put [a: 1, b: 2], :a, 3
+# Dict.put [a: 1, b: 2], :a, 3
# #=> [a: 3, b: 2]
-# PDict.put [a: 1, b: 2], {:c, 3}
+# Dict.put [a: 1, b: 2], {:c, 3}
# #=> [a: 1, b: 2, c: 3]
# """
put(dict, pair),
@@ -75,8 +75,8 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.delete [a: 1, b: 2], :a #=> [b: 2]
-# PDict.delete [b: 2], :a #=> [b: 2]
+# Dict.delete [a: 1, b: 2], :a #=> [b: 2]
+# Dict.delete [b: 2], :a #=> [b: 2]
# """
delete(dict, key),
@@ -86,7 +86,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.merge [a: 1, b: 2], [a: 3, d: 4]
+# Dict.merge [a: 1, b: 2], [a: 3, d: 4]
# #=> [a:3, b:2, d: 4]
# """
merge(dict1, dict2),
@@ -97,7 +97,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.merge [a: 1, b: 2], [a: 3, d: 4], fn(_k, v1, v2) ->
+# Dict.merge [a: 1, b: 2], [a: 3, d: 4], fn(_k, v1, v2) ->
# v1 + v2
# end
# #=> [a: 4, b: 2, d: 4]
@@ -110,7 +110,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.update [a: 1, b: 2], :a, fn(val) -> -val end
+# Dict.update [a: 1, b: 2], :a, fn(val) -> -val end
# #=> [a: -1, b: 2]
#
# """
@@ -123,7 +123,7 @@ defprotocol PDict, [
#
# ## Examples
#
-# PDict.update [a: 1, b: 2], :c, 3, fn(val) -> -val end
+# Dict.update [a: 1, b: 2], :c, 3, fn(val) -> -val end
# #=> [a: 1, b: 2, c: 3]
#
# """
@@ -146,7 +146,7 @@ defmodule Dict.Common do
defmacro __using__(module, impl_ref) do
# This is an optimization trick that allows us to reference the
# implementation module directly
- ref = Module.concat(PDict, impl_ref)
+ ref = Module.concat(Dict, impl_ref)
quote do
@doc """
Creates a new empty dict.
View
@@ -1,24 +1,26 @@
-defimpl PDict, for: Tuple do
+defrecord HashDict.Record, data: nil
+
+defimpl Dict, for: HashDict.Record do
def keys(dict) do
- :dict.fetch_keys dict
+ :dict.fetch_keys dict.data
end
def values(dict) do
:dict.fold fn(_key, value, acc) ->
[value|acc]
- end, [], dict
+ end, [], dict.data
end
def size(dict) do
- :dict.size dict
+ :dict.size dict.data
end
def has_key?(dict, key) do
- :dict.is_key key, dict
+ :dict.is_key key, dict.data
end
def get(dict, key, default // nil) do
- case :dict.find(key, dict) do
+ case :dict.find(key, dict.data) do
match: {:ok, value}
value
match: :error
@@ -27,42 +29,62 @@ defimpl PDict, for: Tuple do
end
def put(dict, key, value) do
- :dict.store key, value, dict
+ dict.update_data(:dict.store key, value, &1)
end
def put(dict, {key, value}) do
- :dict.store key, value, dict
+ dict.update_data(:dict.store key, value, &1)
end
def delete(dict, key) do
- :dict.erase key, dict
+ dict.update_data(:dict.erase key, &1)
end
def merge(d1, d2) do
- :dict.merge fn(_k, _v1, v2) -> v2 end, d1, d2
+ d1.update_data(:dict.merge fn(_k, _v1, v2) -> v2 end, &1, d2.data)
end
def merge(d1, d2, fun) do
- :dict.merge fun, d1, d2
+ d1.update_data(:dict.merge fun, &1, d2.data)
end
def update(dict, key, fun) do
- :dict.update key, fun, dict
+ dict.update_data(:dict.update key, fun, &1)
end
def update(dict, key, initial, fun) do
- :dict.update key, fun, initial, dict
+ dict.update_data(:dict.update key, fun, initial, &1)
end
def empty(_) do
- :dict.new
+ HashDict.Record.new(data: :dict.new)
end
def to_list(dict) do
- :dict.to_list dict
+ :dict.to_list dict.data
end
end
-defmodule Dict do
- use Dict.Common, :Tuple
+defmodule HashDict do
+ use Dict.Common, :"HashDict.Record"
+end
+
+defimpl Enum.Iterator, for: HashDict.Record do
+ import Enum.Iterator.List, only: [iterate: 1]
+
+ def iterator(dict) do
+ { iterate(&1), iterate({ to_list(dict), fn(pairs, do: extend(Dict.HashDict.Record.empty(dict), pairs)) }) }
+ end
+
+ def ordered_iterator(_) do
+ raise ArgumentError, message: "Dict does not support ordering"
+ end
+
+ def to_list(dict), do: Dict.HashDict.Record.to_list(dict)
+
+ defp extend(dict, pairs) do
+ List.foldl pairs, dict, fn(pair, dict) ->
+ Dict.HashDict.Record.put dict, pair
+ end
+ end
end
View
@@ -1,5 +1,5 @@
# We're assuming that the List contains sorted pairs of key-value entries
-defimpl PDict, for: List do
+defimpl Dict, for: List do
def keys(dict) do
:orddict.fetch_keys dict
end
View
@@ -869,24 +869,3 @@ defimpl Enum.Iterator, for: List do
{ :stop, ctor, nil}
end
end
-
-# An implementation for Dict
-defimpl Enum.Iterator, for: Tuple do
- import Enum.Iterator.List, only: [iterate: 1]
-
- def iterator(dict) do
- { iterate(&1), iterate({ to_list(dict), fn(pairs, do: extend(PDict.empty(dict), pairs)) }) }
- end
-
- def ordered_iterator(_) do
- raise ArgumentError, message: "Dict does not support ordering"
- end
-
- def to_list(dict), do: PDict.to_list(dict)
-
- defp extend(dict, pairs) do
- List.foldl pairs, dict, fn(pair, dict) ->
- PDict.put dict, pair
- end
- end
-end
Oops, something went wrong.

0 comments on commit 5e92c8e

Please sign in to comment.