anchor.diff:      Start = timestamp() - Sz,
       take_since(Buf2, Start, reverse(Buf1)).
   
  -fold(_Fun, _Acc, #slide{size = Sz}) when Sz == 0 ->
  +fold(_TS, _Fun, _Acc, #slide{size = Sz}) when Sz == 0 ->
       [];
  -fold(Fun, Acc, #slide{size = Sz, buf1 = Buf1, buf2 = Buf2}) ->
  -    Start = timestamp() - Sz,
  +fold(TS, Fun, Acc, #slide{size = Sz, buf1 = Buf1, buf2 = Buf2}) ->
  +    Start = TS - Sz,
       lists:foldr(
         Fun, lists:foldl(Fun, Acc, take_since(Buf2, Start, [])), Buf1).
  +
  +fold(Fun, Acc, Slide) ->
  +    fold(timestamp(), Fun, Acc, Slide).
   
   take_since([{TS,_} = H|T], Start, Acc) when TS >= Start ->
       take_since(T, Start, [H|Acc]);
  
positive.code:     %% Don't reverse; already the wanted order.
      Acc.
  
  test() ->
      S = new(1000),
      S1 = lists:foldl(
  	   fun({K,V}, Acc) ->
  		   Acc1 = add_element({K,V}, Acc),
  		   Acc1
  	   end, S, [{K, V} || K <- lists:seq(1,100),
  			      V <- lists:seq(1,100)] ++
  	       [{K,V} || K <- [3,4],
  			 V <- [b,c]]),
      timer:tc(?MODULE, build_histogram, [S1]).
  
  build_histogram(S) ->
      on_pdict(
        fun() ->
  	      fold(fun({_T, {K, _V}}, Acc) ->
  			   pd_incr(K), Acc
  		   end, ok, S),
  	      get()
        end).
  
  on_pdict(F) when is_function(F, 0) ->
      Prev = erase(),
      try F()
      after
  	erase(),
  	[put(K,V) || {K,V} <- Prev]
      end.
  
  pd_incr(K) ->
      case get(K) of
  	undefined ->
  	    put(K, 1);
  	N ->
  	    put(K, N+1)
      end.
  
negative.code: 	 add_element/2,
  	 add_element/3,
  	 to_list/1,
  	 fold/3,
  	fold/4]).
  
  
  -export([test/0]).
  
  -compile(export_all).
  
  