anchor.diff:  %% @end
   -spec insert(pos_integer(), pos_integer(), #scarcity{}) -> scarcity().
   insert(Index, Count, Scarcity) ->
  -    #scarcity{
  -        members=Members,
  -        counters=Counters,
  -        ordering=Ordering} = Scarcity,
  -    assert_not_member(Index, Members),
  -
  -    NewMembers  = etorrent_pieceset:insert(Index, Members),
  +    #scarcity{counters=Counters, ordering=Ordering} = Scarcity,
  +    assert_not_member(Index, Counters),
  +
       NewCounters = array:set(Index, Count, Counters),
  -    IsSmaller   = fun(E) -> array:get(E, Counters) =< Count end,
  -    {Smaller, Larger} = lists:splitwith(IsSmaller, Ordering),
  -    NewOrdering = Smaller ++ [Index|Larger],
  -
  -    NewScarcity = #scarcity{
  -        members=NewMembers,
  -        counters=NewCounters,
  -        ordering=NewOrdering},
  -    NewScarcity.
  +    NewOrdering = gb_sets:insert({Count, Index}, Ordering),
  +
  +    #scarcity{counters=NewCounters, ordering=NewOrdering}.
   
   %% @doc
   %%
   %% @end
   -spec delete(pos_integer(), #scarcity{}) -> scarcity().
   delete(Index, Scarcity) ->
  -     #scarcity{
  -        members=Members,
  -        counters=Counters,
  -        ordering=Ordering} = Scarcity,
  -    assert_member(Index, Members),
  -
  -    NewMembers  = etorrent_pieceset:delete(Index, Members),
  +    #scarcity{counters=Counters, ordering=Ordering} = Scarcity,
  +    assert_member(Index, Counters),
  +
  +    Count = array:get(Index, Counters),
       NewCounters = array:reset(Index, Counters),
  -    NewOrdering = lists:delete(Index, Ordering),
  -
  -    NewScarcity = #scarcity{
  -        members=NewMembers,
  -        counters=NewCounters,
  -        ordering=NewOrdering},
  -    NewScarcity.
  +    NewOrdering = gb_sets:delete({Count, Index}, Ordering),
  +
  +    #scarcity{counters=NewCounters, ordering=NewOrdering}.
   
      
   
  
positive.code: -include_lib("eunit/include/eunit.hrl").
  -endif.
  
  -export([from_list/2,
           to_list/1,
           insert/3,
           delete/2,
           increment/2,
           decrement/2]).
  
  -type pieceset() :: etorrent_pieceset:pieceset().
  -type piecelist() :: list(pos_integer()).
  -record(scarcity, {
      members  :: pieceset(),
      counters :: array(),
      ordering :: piecelist()}).
  -opaque scarcity() :: #scarcity{}.
      
  
  %% @doc
  %%
  %% @end
  -spec from_list(pos_integer(), list({pos_integer(), pos_integer()})) -> scarcity().
  from_list(NumPieces, ScarcityList) ->
      InitMembers  = etorrent_pieceset:new(NumPieces),
      InitCounters = array:new([{size, NumPieces}, {default, 0}]),
      InitScarcity = #scarcity{
          members=InitMembers,
          counters=InitCounters,
          ordering=[]},
      lists:foldl(fun({Index, Count}, Acc) ->
          insert(Index, Count, Acc)
      end, InitScarcity, ScarcityList).
  
  %% @doc
  %%
  
negative.code: %% @end
  -spec increment(pos_integer(), #scarcity{}) -> scarcity().
  increment(Index, Scarcity) ->
      #scarcity{counters=Counters, ordering=Ordering} = Scarcity,
      assert_member(Index, Counters),
  
      Count = array:get(Index, Counters),
      NewCounters = array:set(Index, Count + 1, Counters),
      TmpOrdering = gb_sets:delete({Count, Index}, Ordering),
      NewOrdering = gb_sets:insert({Count + 1, Index}, TmpOrdering),
  
      #scarcity{counters=NewCounters, ordering=NewOrdering}.
  
  
  %% @doc
  