anchor.diff:  %% @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.
  -
  -   
  +    #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
  +%%
  +%% @end
  +-spec iterator(#scarcity{}) -> term().
  +iterator(Scarcity) ->
  +    #scarcity{ordering=Ordering} = Scarcity,
  +    gb_sets:iterator(Ordering).
  +
  +
  +%% @doc
  +%%
  +%% @end
  +-spec next(term()) -> pos_integer().
  +next(Iterator) ->
  +    case gb_sets:next(Iterator) of
  +        none ->
  +            none;
  +        {{_, Index}, NewIterator} ->
  +            {Index, NewIterator}
  +    end.
  +
   
   %% @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)
  +-spec assert_member(pos_integer(), array()) -> ok.
  +assert_member(Index, Counters) ->
  +    case array:get(Index, Counters) of
  +        undefined ->
  +            error(badarg);
  +        _  ->
  +            ok
       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
  +-spec assert_not_member(pos_integer(), array()) -> ok.
  +assert_not_member(Index, Counters) ->
  +    case array:get(Index, Counters) of
  +        undefined ->
  +            ok;
  +        _ ->
  +            error(badarg)
       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)).
  +    S0 = ?mod:from_list([{0,0},{1,0},{2,0}]),
  +    ?assertEqual([0,1,2], ?mod:to_list(S0)).
   
   new_all_ordered_test() ->
  -    New = ?mod:from_list(3, [{0,0},{1,1},{2,2}]),
  -    ?assertEqual([0,1,2], ?mod:to_list(New)).
  +    S0 = ?mod:from_list([{0,0},{1,1},{2,2}]),
  +    ?assertEqual([0,1,2], ?mod:to_list(S0)).
   
   new_all_reversed_test() ->
  -    New = ?mod:from_list(3, [{0,2},{1,1},{2,0}]),
  -    ?assertEqual([2,1,0], ?mod:to_list(New)).
  +    S0 = ?mod:from_list([{0,2},{1,1},{2,0}]),
  +    ?assertEqual([2,1,0], ?mod:to_list(S0)).
  +
  +increment_test() ->
  +    S0 = ?mod:from_list([{0,0}, {1,1}, {2,2}]),
  +    S1 = ?mod:increment(0, S0),
  +    S2 = ?mod:increment(0, S1),
  +    S3 = ?mod:increment(0, S2),
  +    S4 = ?mod:increment(0, S3),
  +    ?assertEqual([1,2,0], ?mod:to_list(S4)).
  +
  +decrement_test() ->
  +    S0 = ?mod:from_list([{0,0}, {1,1}, {2,2}]),
  +    S1 = ?mod:decrement(2, S0),
  +    S2 = ?mod:decrement(2, S1),
  +    S3 = ?mod:decrement(2, S2),
  +    S4 = ?mod:decrement(2, S3),
  +    ?assertEqual([2,0,1], ?mod:to_list(S4)).
  +
  +delete_test() ->
  +    S0 = ?mod:from_list([{0,0},{1,1},{2,2}]),
  +    S1 = ?mod:delete(0, S0),
  +    S2 = ?mod:delete(1, S0),
  +    S3 = ?mod:delete(2, S0),
  +    ?assertEqual([1,2], ?mod:to_list(S1)),
  +    ?assertEqual([0,2], ?mod:to_list(S2)),
  +    ?assertEqual([0,1], ?mod:to_list(S3)).
  +
  +delete_nonmember_test() ->
  +    S0 = ?mod:from_list([{0,0},{2,2}]),
  +    ?assertError(badarg, ?mod:delete(1, S0)).
  +
  +insert_member_test() ->
  +    S0 = ?mod:from_list([{0,0},{1,1},{2,2}]),
  +    ?assertError(badarg, ?mod:insert(1, 1, S0)).
  +
  +iterator_test() ->
  +    S0 = ?mod:from_list([{0,0},{2,2}]),
  +    I0 = ?mod:iterator(S0),
  +    {E1,I1} = ?mod:next(I0),
  +    {E2,I2} = ?mod:next(I1),
  +    ?assertEqual(0, E1),
  +    ?assertEqual(2, E2),
  +    ?assertEqual(none, ?mod:next(I2)).
   
   -endif.
  
positive.code: %% @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.
  
  
  %% @doc
  
negative.code: -spec to_list(#scarcity{}) -> list(pos_integer()).
  to_list(Scarcity) ->
      #scarcity{ordering=Ordering} = Scarcity,
      [Index || {_, Index} <- gb_sets:to_list(Ordering)].
  
  
  %% @doc
  