@@ -303,6 +303,52 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
303303 return MapRange! (This, IterType.both)(cast (Unqual! (This)* ) &this );
304304 }
305305
306+ /**
307+ * Support for $(D foreach(key, value; aa) { ... }) syntax;
308+ */
309+ int opApply (int delegate (in ref K, ref V) del)
310+ {
311+ int result = 0 ;
312+ foreach (ref bucket; buckets)
313+ foreach (ref node; bucket[])
314+ if ((result = del(* cast (K* )&node.key, * cast (V* )&node.value)) != 0 )
315+ return result;
316+ return result;
317+ }
318+
319+ // / ditto
320+ int opApply (int delegate (in ref K, in ref V) del) const
321+ {
322+ int result = 0 ;
323+ foreach (const ref bucket; buckets)
324+ foreach (const ref node; bucket[])
325+ if ((result = del(* cast (K* )&node.key, * cast (V* )&node.value)) != 0 )
326+ return result;
327+ return result;
328+ }
329+
330+ // / ditto
331+ int opApply (int delegate (ref V) del)
332+ {
333+ int result = 0 ;
334+ foreach (ref bucket; buckets)
335+ foreach (ref node; bucket[])
336+ if ((result = del(* cast (V* )&node.value)) != 0 )
337+ return result;
338+ return result;
339+ }
340+
341+ // / ditto
342+ int opApply (int delegate (in ref V) del) const
343+ {
344+ int result = 0 ;
345+ foreach (const ref bucket; buckets)
346+ foreach (const ref node; bucket[])
347+ if ((result = del(* cast (V* )&node.value)) != 0 )
348+ return result;
349+ return result;
350+ }
351+
306352 mixin AllocatorState! Allocator;
307353
308354private :
@@ -694,3 +740,42 @@ version(emsi_containers_unittest) unittest
694740 HashMap! (S, S, Mallocator, (S s) { return s.v; }, false , false ) aa;
695741 static assert (aa.Node.sizeof == 2 * S.sizeof);
696742}
743+
744+ version (emsi_containers_unittest) unittest
745+ {
746+ auto hm = HashMap! (string , int )(16 );
747+
748+ foreach (v; hm) {}
749+ foreach (ref v; hm) {}
750+ foreach (int v; hm) {}
751+ foreach (ref int v; hm) {}
752+ foreach (const ref int v; hm) {}
753+
754+ foreach (k, v; hm) {}
755+ foreach (k, ref v; hm) {}
756+ foreach (k, int v; hm) {}
757+ foreach (k, ref int v; hm) {}
758+ foreach (k, const ref int v; hm) {}
759+
760+ foreach (ref k, v; hm) {}
761+ foreach (ref k, ref v; hm) {}
762+ foreach (ref k, int v; hm) {}
763+ foreach (ref k, ref int v; hm) {}
764+ foreach (ref k, const ref int v; hm) {}
765+
766+ foreach (const string k, v; hm) {}
767+ foreach (const string k, ref v; hm) {}
768+ foreach (const string k, int v; hm) {}
769+ foreach (const string k, ref int v; hm) {}
770+ foreach (const string k, const ref int v; hm) {}
771+
772+ foreach (const ref string k, v; hm) {}
773+ foreach (const ref string k, ref v; hm) {}
774+ foreach (const ref string k, int v; hm) {}
775+ foreach (const ref string k, ref int v; hm) {}
776+ foreach (const ref string k, const ref int v; hm) {}
777+
778+ hm[" a" ] = 1 ;
779+ foreach (k, ref v; hm) { v++ ; }
780+ assert (hm[" a" ] == 2 );
781+ }
0 commit comments