@@ -1229,14 +1229,41 @@ pub fn ArrayHashMapUnmanaged(
1229
1229
/// Sorts the entries and then rebuilds the index.
1230
1230
/// `sort_ctx` must have this method:
1231
1231
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
1232
+ /// Uses a stable sorting algorithm.
1232
1233
pub inline fn sort (self : * Self , sort_ctx : anytype ) void {
1233
1234
if (@sizeOf (ByIndexContext ) != 0 )
1234
1235
@compileError ("Cannot infer context " ++ @typeName (Context ) ++ ", call sortContext instead." );
1235
- return self . sortContext ( sort_ctx , undefined );
1236
+ return sortContextInternal ( self , .stable , sort_ctx , undefined );
1236
1237
}
1237
1238
1238
- pub fn sortContext (self : * Self , sort_ctx : anytype , ctx : Context ) void {
1239
- self .entries .sort (sort_ctx );
1239
+ /// Sorts the entries and then rebuilds the index.
1240
+ /// `sort_ctx` must have this method:
1241
+ /// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
1242
+ /// Uses an unstable sorting algorithm.
1243
+ pub inline fn sortUnstable (self : * Self , sort_ctx : anytype ) void {
1244
+ if (@sizeOf (ByIndexContext ) != 0 )
1245
+ @compileError ("Cannot infer context " ++ @typeName (Context ) ++ ", call sortUnstableContext instead." );
1246
+ return self .sortContextInternal (.unstable , sort_ctx , undefined );
1247
+ }
1248
+
1249
+ pub inline fn sortContext (self : * Self , sort_ctx : anytype , ctx : Context ) void {
1250
+ return sortContextInternal (self , .stable , sort_ctx , ctx );
1251
+ }
1252
+
1253
+ pub inline fn sortUnstableContext (self : * Self , sort_ctx : anytype , ctx : Context ) void {
1254
+ return sortContextInternal (self , .unstable , sort_ctx , ctx );
1255
+ }
1256
+
1257
+ fn sortContextInternal (
1258
+ self : * Self ,
1259
+ comptime mode : std.sort.Mode ,
1260
+ sort_ctx : anytype ,
1261
+ ctx : Context ,
1262
+ ) void {
1263
+ switch (mode ) {
1264
+ .stable = > self .entries .sort (sort_ctx ),
1265
+ .unstable = > self .entries .sortUnstable (sort_ctx ),
1266
+ }
1240
1267
const header = self .index_header orelse return ;
1241
1268
header .reset ();
1242
1269
self .insertAllEntriesIntoNewHeader (if (store_hash ) {} else ctx , header );
0 commit comments