@@ -111,6 +111,34 @@ struct StringMap(T, U = uint)
111111 assert (map.findPos(" key" ) == 0 );
112112 }
113113
114+ // /
115+ string toString () const
116+ {
117+ import mir.format: stringBuf;
118+ stringBuf buffer;
119+ toString(buffer);
120+ return buffer.data.idup;
121+ }
122+
123+ // /ditto
124+ void toString (W)(scope ref W w) const
125+ {
126+ bool next;
127+ w.put(' [' );
128+ import mir.format: printEscaped, EscapeFormat, print;
129+ foreach (i, ref value; values )
130+ {
131+ if (next)
132+ w.put(` , ` );
133+ next = true ;
134+ w.put(' \" ' );
135+ printEscaped! (char , EscapeFormat.ion)(w, keys [i]);
136+ w.put(` ": ` );
137+ print(w, value);
138+ }
139+ w.put(' ]' );
140+ }
141+
114142 /+ +
115143 Constructs an associative array using keys and values.
116144 Params:
@@ -704,7 +732,7 @@ private struct StructImpl(T, U = uint)
704732 this ()(string [] keys , T[] values ) @trusted pure nothrow
705733 {
706734 import mir.array.allocation: array;
707- import mir.ndslice.sorting: sort ;
735+ import mir.ndslice.sorting: makeIndex ;
708736 import mir.ndslice.topology: iota, indexed;
709737 import mir.string_table: smallerStringFirst;
710738
@@ -714,13 +742,9 @@ private struct StructImpl(T, U = uint)
714742 _length = keys .length;
715743 _keys = keys .ptr;
716744 _values = values .ptr;
717- _indices = keys .length.iota ! U.array .ptr;
745+ _indices = keys .makeIndex ! (U, smallerStringFirst) .ptr;
718746 auto sortedKeys = _keys.indexed(indices);
719- sortedKeys.sort! smallerStringFirst;
720- size_t maxKeyLength;
721- foreach (ref key; keys )
722- if (key.length > maxKeyLength)
723- maxKeyLength = key.length;
747+ size_t maxKeyLength = sortedKeys[$ - 1 ].length;
724748 _lengthTable = new U[maxKeyLength + 2 ];
725749
726750 size_t ski;
0 commit comments