From dbfb2ab0e4e15d7503464aabb3971a38d8bde0e1 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Tue, 22 Sep 2020 17:24:48 -0400 Subject: [PATCH] bulk label --- go/src/miller/mappers/label.go | 10 +-------- go/src/miller/types/mlrmap_accessors.go | 30 +++++++++++++++++++++++++ go/todo.txt | 3 +-- go/u/try-verbs.out | 20 ++++++++--------- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/go/src/miller/mappers/label.go b/go/src/miller/mappers/label.go index 813ff9515..56701a4f1 100644 --- a/go/src/miller/mappers/label.go +++ b/go/src/miller/mappers/label.go @@ -109,15 +109,7 @@ func (this *MapperLabel) Map( ) { inrec := inrecAndContext.Record if inrec != nil { // not end of record stream - - oldNames := inrec.GetKeys() - numOld := len(oldNames) - numNew := len(this.newNames) - numToRename := lib.IntMin2(numOld, numNew) - - for i := 0; i < numToRename; i++ { - inrec.Rename(&oldNames[i], &this.newNames[i]) - } + inrec.Label(this.newNames) } outputChannel <- inrecAndContext // end-of-stream marker } diff --git a/go/src/miller/types/mlrmap_accessors.go b/go/src/miller/types/mlrmap_accessors.go index 4af5b47f6..4cfb4dcd2 100644 --- a/go/src/miller/types/mlrmap_accessors.go +++ b/go/src/miller/types/mlrmap_accessors.go @@ -317,6 +317,36 @@ func (this *Mlrmap) Rename(oldKey *string, newKey *string) bool { return true } +// ---------------------------------------------------------------- +func (this *Mlrmap) Label(newNames []string) { + that := NewMlrmapAsRecord() + + i := 0 + numNewNames := len(newNames) + for { + if i >= numNewNames { + break + } + pe := this.pop() + if pe == nil { + break + } + // Old record will be GC'ed: just move pointers + that.PutReference(&newNames[i], pe.Value) + i++ + } + + for { + pe := this.pop() + if pe == nil { + break + } + that.PutReference(pe.Key, pe.Value) + } + + *this = *that +} + // ================================================================ // PRIVATE METHODS diff --git a/go/todo.txt b/go/todo.txt index e54acc0cd..4669f9e3c 100644 --- a/go/todo.txt +++ b/go/todo.txt @@ -17,8 +17,6 @@ TOP OF LIST: - default to absent (no hoisting) - no scope-walks needed for for-loops -* rename: bulk-edit for b,i,x vs x,i,b cases - ! filter > bare-boolean LR-1 conflicts wut ? erase BNF distinction between BaseLvalue and MlrvalOrFunction @@ -164,3 +162,4 @@ NITS/NON-IMMEDIATE: * mlrdoc: there should have always been a >>> and >>>=, and now there are ! systime printing out in scientific notation :( ! head early-out -- cite the relevant issue & fix the right way ... +* relnotes: label b,i,x vs x,i,b change diff --git a/go/u/try-verbs.out b/go/u/try-verbs.out index 196fc347b..8e4858c0e 100644 --- a/go/u/try-verbs.out +++ b/go/u/try-verbs.out @@ -259,19 +259,19 @@ eks wye 4 0.38139939387114097 0.13418874328430463 ---------------------------------------------------------------- mlr --opprint label b,i,x u/s.dkvp -x y -pan 0.7268028627434533 -eks 0.5221511083334797 -wye 0.33831852551664776 -eks 0.13418874328430463 +b i x y +pan pan 0.3467901443380824 0.7268028627434533 +eks pan 0.7586799647899636 0.5221511083334797 +wye wye 0.20460330576630303 0.33831852551664776 +eks wye 0.38139939387114097 0.13418874328430463 ---------------------------------------------------------------- mlr --opprint label x,i,b u/s.dkvp -b x y -pan pan 0.7268028627434533 -pan eks 0.5221511083334797 -wye wye 0.33831852551664776 -wye eks 0.13418874328430463 +x i b y +0.3467901443380824 pan 1 0.7268028627434533 +0.7586799647899636 pan 2 0.5221511083334797 +0.20460330576630303 wye 3 0.33831852551664776 +0.38139939387114097 wye 4 0.13418874328430463 ---------------------------------------------------------------- mlr --opprint --from u/s.dkvp sort -f nonesuch