From 55ad9621b687ee1ad767ffa546d45c4e7f9ba245 Mon Sep 17 00:00:00 2001 From: Mahrud Sayrafi Date: Sat, 25 May 2024 01:56:13 -0500 Subject: [PATCH 1/3] Revert "Refactor dedupSymbols to allow more than two duplicates" This reverts commit 6b3e0bd256f3164a67e8d5ddd4e8c3bfc5a98104. --- M2/Macaulay2/m2/monoids.m2 | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/M2/Macaulay2/m2/monoids.m2 b/M2/Macaulay2/m2/monoids.m2 index aea587f6b50..e3d85493237 100644 --- a/M2/Macaulay2/m2/monoids.m2 +++ b/M2/Macaulay2/m2/monoids.m2 @@ -474,22 +474,14 @@ checkSymbol = sym -> if instance(sym, Symbol) or lookup(symbol <-, class sym) =! -- turns {x, y, z, y} into {x, y_0, z, y_1} -- adding 'toString' in a few places will eliminate more duplications -- but makes creating temporary rings in functions more difficult. -dedupSymbols = varlist -> ( - while repeats varlist > 0 do ( - counts := tally varlist; - data := hashTable apply(keys counts, - var -> ( - var, - new MutableList from { - 0, - if counts#var > 1 then ( - makeVars(counts#var, var)) - else {var}})); - varlist = apply(varlist, var -> ( - newvar := data#var#1#(data#var#0); - data#var#0 += 1; - newvar))); - varlist) +dedupSymbols = varlist -> if 0 == repeats varlist then varlist else while 0 < repeats varlist do ( + mapping := hashTable toList pairs varlist; + -- TODO: applyPairs with collision handler, and accepting MutableHashTable + counter := new MutableHashTable from applyKeys(mapping, + i -> varlist#i, dups -> makeVars(#dups, first dups)); + varlist = values applyValues(mapping, var -> if instance(counter#(name := var), List) + then first(counter#name#0, counter#name = drop(counter#name, 1)) else var); + if 0 == repeats varlist then break varlist else varlist) -- also used in AssociativeAlgebras.m2 findSymbols = varlist -> dedupSymbols toList apply(pairs listSplice varlist, From 4b87a04371ad281884814450ffd934627fd9ae51 Mon Sep 17 00:00:00 2001 From: Mahrud Sayrafi Date: Sat, 25 May 2024 01:59:39 -0500 Subject: [PATCH 2/3] added simple fix for #3261 --- M2/Macaulay2/m2/monoids.m2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/M2/Macaulay2/m2/monoids.m2 b/M2/Macaulay2/m2/monoids.m2 index e3d85493237..d3a3579d98b 100644 --- a/M2/Macaulay2/m2/monoids.m2 +++ b/M2/Macaulay2/m2/monoids.m2 @@ -478,7 +478,7 @@ dedupSymbols = varlist -> if 0 == repeats varlist then varlist else while 0 < re mapping := hashTable toList pairs varlist; -- TODO: applyPairs with collision handler, and accepting MutableHashTable counter := new MutableHashTable from applyKeys(mapping, - i -> varlist#i, dups -> makeVars(#dups, first dups)); + i -> varlist#i, dups -> makeVars(#flatten dups, last dups)); varlist = values applyValues(mapping, var -> if instance(counter#(name := var), List) then first(counter#name#0, counter#name = drop(counter#name, 1)) else var); if 0 == repeats varlist then break varlist else varlist) From 2dba31778bbff0d9fb18ad874672e18f857c3880 Mon Sep 17 00:00:00 2001 From: Mahrud Sayrafi Date: Sat, 25 May 2024 11:57:04 -0500 Subject: [PATCH 3/3] made dedupSymbols more efficient --- M2/Macaulay2/m2/monoids.m2 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/M2/Macaulay2/m2/monoids.m2 b/M2/Macaulay2/m2/monoids.m2 index d3a3579d98b..58996855caa 100644 --- a/M2/Macaulay2/m2/monoids.m2 +++ b/M2/Macaulay2/m2/monoids.m2 @@ -471,16 +471,17 @@ makeVars = (n, var) -> toList( -- TODO: why is (symbol <-, T) and not (symbol <-, T, Thing) the right method sequence for assignment? checkSymbol = sym -> if instance(sym, Symbol) or lookup(symbol <-, class sym) =!= null then sym else error() +-- TODO: the compiled function remove should return the removed value +remove' = (L, i) -> (x := L#i; remove(L, i); x) + -- turns {x, y, z, y} into {x, y_0, z, y_1} -- adding 'toString' in a few places will eliminate more duplications -- but makes creating temporary rings in functions more difficult. dedupSymbols = varlist -> if 0 == repeats varlist then varlist else while 0 < repeats varlist do ( mapping := hashTable toList pairs varlist; - -- TODO: applyPairs with collision handler, and accepting MutableHashTable - counter := new MutableHashTable from applyKeys(mapping, - i -> varlist#i, dups -> makeVars(#flatten dups, last dups)); - varlist = values applyValues(mapping, var -> if instance(counter#(name := var), List) - then first(counter#name#0, counter#name = drop(counter#name, 1)) else var); + counter := applyPairs(tally varlist, (name, count) -> + name => new MutableList from if count == 1 then {name} else makeVars(count, name)); + varlist = apply(varlist, var -> remove'(counter#var, 0)); if 0 == repeats varlist then break varlist else varlist) -- also used in AssociativeAlgebras.m2