anchor.diff:  %% @end
   -spec increment(pos_integer(), #scarcity{}) -> scarcity().
   increment(Index, Scarcity) ->
  -    #scarcity{
  -        members=Members,
  -        counters=Counters,
  -        ordering=Ordering} = Scarcity,
  -    assert_member(Index, Members),
  -
  -    PrevCount = array:get(Index, Counters),
  -    NewCounters = array:set(Index, PrevCount + 1, Counters),
  -    NewOrdering = reorder(Index, NewCounters, Ordering),
  -
  -    NewScarcity = #scarcity{
  -        counters=NewCounters,
  -        ordering=NewOrdering},
  -    NewScarcity.
  +    #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
  
positive.code: %% @end
  -spec decrement(pos_integer(), #scarcity{}) -> scarcity().
  decrement(Index, Scarcity) ->
       #scarcity{
          members=Members,
          counters=Counters,
          ordering=Ordering} = Scarcity,
      assert_member(Index, Members),
  
      PrevCount = array:get(Index, Counters),
      NewCounters = array:set(Index, PrevCount - 1, Counters),
      NewOrdering = reorder(Index, NewCounters, Ordering),
  
      NewScarcity = #scarcity{
          counters=NewCounters,
          ordering=NewOrdering},
      NewScarcity.
  
     
  
  %% @doc  
  %%
  %% @end
  -spec assert_member(pos_integer(), pieceset()) -> ok.
  assert_member(Index, Pieceset) ->
      case etorrent_pieceset:is_member(Index, Pieceset) of
          true  -> ok;
          false -> error(badarg)
      end.
  
  %% @doc
  %%
  %% @end
  -spec assert_not_member(pos_integer(), pieceset()) -> ok.
  assert_not_member(Index, Pieceset) ->
      case etorrent_pieceset:is_member(Index, Pieceset) of
          true  -> error(badarg);
          false -> ok
      end.
  
  
  %% @doc
  %%
  %% @end
  -spec reorder(pos_integer(), array(), piecelist()) -> piecelist().
  %% Changed piece occurs at the middle of the list
  reorder(Changed, Counters, [Smaller,Changed,Larger|T]=Original) ->
      SmallerHas = array:get(Smaller, Counters),
      ChangedHas = array:get(Changed, Counters),
      LargerHas  = array:get(Larger,  Counters),
      if  ChangedHas < SmallerHas ->
              [Changed,Smaller,Larger|T];
          ChangedHas > LargerHas ->
              [Smaller,Larger,Changed|T];
          true ->
              Original
      end;
  %% Changed piece occurs at the beginning of the list
  reorder(Changed, Counters, [Changed,Larger|T]=Original) ->
      ChangedHas = array:get(Changed, Counters),
      LargerHas  = array:get(Larger,  Counters),
      if  ChangedHas > LargerHas ->
              [Larger,Changed|T];
          true ->
              Original
      end;
  %% Changed piece occurs at the end of the list
  reorder(Changed, Counters, [Smaller,Changed|T]=Original) ->
      ChangedHas = array:get(Changed, Counters),
      SmallerHas = array:get(Smaller, Counters),
      if  ChangedHas < SmallerHas ->
              [ChangedHas,SmallerHas|T];
          true ->
              Original
      end;
  %% Not there yet, order of head is unaffected
  reorder(Changed, Counters, [H|T]) ->
      [H|reorder(Changed, Counters, T)].
  
  -ifdef(TEST).
  -define(mod, ?MODULE).
  -define(pset, etorrent_pieceset).
  
  new_all_unordered_test() ->
      New = ?mod:from_list(3, [{0,0},{1,0},{2,0}]),
      ?assertEqual([0,1,2], ?mod:to_list(New)).
  
  new_all_ordered_test() ->
      New = ?mod:from_list(3, [{0,0},{1,1},{2,2}]),
      ?assertEqual([0,1,2], ?mod:to_list(New)).
  
  new_all_reversed_test() ->
      New = ?mod:from_list(3, [{0,2},{1,1},{2,0}]),
      ?assertEqual([2,1,0], ?mod:to_list(New)).
  
  -endif.
  
negative.code: -spec to_list(#scarcity{}) -> list(pos_integer()).
  to_list(Scarcity) ->
      #scarcity{ordering=Ordering} = Scarcity,
      [Index || {_, Index} <- gb_sets:to_list(Ordering)].
  
  
  %% @doc
  