为什么Elixir的MapSet在32个元素之后变得无序?

时间:2016-07-15 00:53:40

标签: set elixir

iex> MapSet.new(1..32) |> Enum.to_list
[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]

iex> MapSet.new(1..33) |> Enum.to_list
[11, 26, 15, 20, 17, 25, 13, 8, 7, 1, 32, 3, 6, 2, 33, 10, 9, 19, 14, 5, 18, 31,
 22, 29, 21, 27, 24, 30, 23, 28, 16, 4, 12]

这里是Elixir 1.3中的implementation

def new(enumerable) do
  map =
    enumerable
    |> Enum.to_list
    |> do_new([])

  %MapSet{map: map}
end

defp do_new([], acc) do
  acc
  |> :lists.reverse
  |> :maps.from_list
end

defp do_new([item | rest], acc) do
  do_new(rest, [{item, true} | acc])
end

尽管订单在MapSet中并不重要,但仍然想知道为什么MapSet在32个元素之后变得无序?

1 个答案:

答案 0 :(得分:15)

这不是MapSet特有的,但普通MapMapSet使用Map)会发生同样的事情:

iex(1)> for i <- Enum.shuffle(1..32), into: %{}, do: {i, i}
%{1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9,
  10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16,
  17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23,
  24 => 24, 25 => 25, 26 => 26, 27 => 27, 28 => 28, 29 => 29, 30 => 30,
  31 => 31, 32 => 32}
iex(2)> for i <- Enum.shuffle(1..33), into: %{}, do: {i, i}
%{11 => 11, 26 => 26, 15 => 15, 20 => 20, 17 => 17, 25 => 25, 13 => 13, 8 => 8,
  7 => 7, 1 => 1, 32 => 32, 3 => 3, 6 => 6, 2 => 2, 33 => 33, 10 => 10, 9 => 9,
  19 => 19, 14 => 14, 5 => 5, 18 => 18, 31 => 31, 22 => 22, 29 => 29, 21 => 21,
  27 => 27, 24 => 24, 30 => 30, 23 => 23, 28 => 28, 16 => 16, 4 => 4, 12 => 12}

这是因为(最有可能是优化)Erlang将大小最高为MAP_SMALL_MAP_LIMIT的地图存储为sorted by key array。只有在大小大于MAP_SMALL_MAP_LIMIT之后,Erlang才会切换到将数据存储在Hash Array Mapped Trie like data structure中。在非调试模式Erlang中,MAP_SMALL_MAP_LIMITdefined to be 32,因此所有长度最多为32的地图都应按排序顺序打印。请注意,就我所知,这是一个实现细节,您不应该依赖此行为;它们可能会在未来更改常量的值,或者如果它的性能更高,则可以切换到完全不同的算法。