module main; import std.algorithm; import std.stdio; import std.range; Foo[uint] foo; auto sorted(T)(T t) { sort(t); return t; } void main() { add( new Foo(1) ); add( new Foo(2) ); add( new Foo(3) ); add( new Foo(4) ); writeln("length: ", foo.length); foreach (key; retro(sorted(foo.keys))) { if(key >= 2 && key <= 3) { remove(foo[key]); continue; } writeln("k: ", key); } writeln("length: ", foo.length); } void add(Foo f) { foo[f.id] = f; } void remove(Foo f) { foo.remove(f.id); } class Foo { uint id; this(uint id) { this.id = id; } }