From e7bb1a43df9565c8be45a05ff150f19e7124174b Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 5 Dec 2014 10:50:22 -0800 Subject: [PATCH] sorting: faster sort-keys and sort-values for hashtables. --- core/sorting/sorting.factor | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/sorting/sorting.factor b/core/sorting/sorting.factor index 32ebe7f7bf..52c8656008 100644 --- a/core/sorting/sorting.factor +++ b/core/sorting/sorting.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors arrays assocs growable.private kernel math -math.order sequences sequences.private vectors ; +USING: accessors arrays assocs growable.private hashtables +kernel kernel.private math math.order sequences +sequences.private vectors ; IN: sorting ! Optimized merge-sort: @@ -159,6 +160,9 @@ M: object sort-keys >alist sort-keys ; M: sequence sort-keys 0 check-bounds [ first-unsafe ] sort-with ; +M: hashtable sort-keys + >alist [ { array } declare first-unsafe ] sort-with ; + GENERIC: sort-values ( obj -- sortedseq ) M: object sort-values >alist sort-values ; @@ -166,4 +170,7 @@ M: object sort-values >alist sort-values ; M: sequence sort-values 1 check-bounds [ second-unsafe ] sort-with ; +M: hashtable sort-values + >alist [ { array } declare second-unsafe ] sort-with ; + : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;