From 3589fd85deba24579567ff80764c6c87feb38f59 Mon Sep 17 00:00:00 2001 From: oldme Date: Mon, 20 May 2024 20:49:37 +0800 Subject: [PATCH] enhance: use bytes.Buffer instead of string concatenation --- maps/linkedhashmap/linkedhashmap.go | 13 ++++++++----- maps/treemap/treemap.go | 13 ++++++++----- trees/avltree/avltree.go | 23 +++++++++++++---------- trees/redblacktree/redblacktree.go | 23 +++++++++++++---------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/maps/linkedhashmap/linkedhashmap.go b/maps/linkedhashmap/linkedhashmap.go index bf5ef4c3..adb32fce 100644 --- a/maps/linkedhashmap/linkedhashmap.go +++ b/maps/linkedhashmap/linkedhashmap.go @@ -12,6 +12,7 @@ package linkedhashmap import ( + "bytes" "fmt" "strings" @@ -98,11 +99,13 @@ func (m *Map[K, V]) Clear() { // String returns a string representation of container func (m *Map[K, V]) String() string { - str := "LinkedHashMap\nmap[" - it := m.Iterator() + var ( + buf bytes.Buffer + it = m.Iterator() + ) + buf.WriteString("LinkedHashMap\nmap[") for it.Next() { - str += fmt.Sprintf("%v:%v ", it.Key(), it.Value()) + buf.WriteString(fmt.Sprintf("%v:%v ", it.Key(), it.Value())) } - return strings.TrimRight(str, " ") + "]" - + return strings.TrimRight(buf.String(), " ") + "]" } diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index f3766622..e60afd12 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -12,6 +12,7 @@ package treemap import ( + "bytes" "cmp" "fmt" "strings" @@ -137,11 +138,13 @@ func (m *Map[K, V]) Ceiling(key K) (foundKey K, foundValue V, ok bool) { // String returns a string representation of container func (m *Map[K, V]) String() string { - str := "TreeMap\nmap[" - it := m.Iterator() + var ( + buf bytes.Buffer + it = m.Iterator() + ) + buf.WriteString("TreeMap\nmap[") for it.Next() { - str += fmt.Sprintf("%v:%v ", it.Key(), it.Value()) + buf.WriteString(fmt.Sprintf("%v:%v ", it.Key(), it.Value())) } - return strings.TrimRight(str, " ") + "]" - + return strings.TrimRight(buf.String(), " ") + "]" } diff --git a/trees/avltree/avltree.go b/trees/avltree/avltree.go index 81b9f3f8..f05edfab 100644 --- a/trees/avltree/avltree.go +++ b/trees/avltree/avltree.go @@ -10,6 +10,7 @@ package avltree import ( + "bytes" "cmp" "fmt" @@ -211,11 +212,12 @@ func (tree *Tree[K, V]) Clear() { // String returns a string representation of container func (tree *Tree[K, V]) String() string { - str := "AVLTree\n" + var buf bytes.Buffer + buf.WriteString("AVLTree\n") if !tree.Empty() { - output(tree.Root, "", true, &str) + output(&buf, tree.Root, "", true) } - return str + return buf.String() } func (n *Node[K, V]) String() string { @@ -442,7 +444,7 @@ func (n *Node[K, V]) walk1(a int) *Node[K, V] { return p } -func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, str *string) { +func output[K comparable, V any](buf *bytes.Buffer, node *Node[K, V], prefix string, isTail bool) { if node.Children[1] != nil { newPrefix := prefix if isTail { @@ -450,15 +452,16 @@ func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, s } else { newPrefix += " " } - output(node.Children[1], newPrefix, false, str) + output(buf, node.Children[1], newPrefix, false) } - *str += prefix + buf.WriteString(prefix) if isTail { - *str += "└── " + buf.WriteString("└── ") } else { - *str += "┌── " + buf.WriteString("┌── ") } - *str += node.String() + "\n" + buf.WriteString(node.String() + "\n") + if node.Children[0] != nil { newPrefix := prefix if isTail { @@ -466,6 +469,6 @@ func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, s } else { newPrefix += "│ " } - output(node.Children[0], newPrefix, true, str) + output(buf, node.Children[0], newPrefix, true) } } diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index ec53532d..2a1fc64f 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -12,6 +12,7 @@ package redblacktree import ( + "bytes" "cmp" "fmt" @@ -281,18 +282,19 @@ func (tree *Tree[K, V]) Clear() { // String returns a string representation of container func (tree *Tree[K, V]) String() string { - str := "RedBlackTree\n" + var buf bytes.Buffer + buf.WriteString("RedBlackTree\n") if !tree.Empty() { - output(tree.Root, "", true, &str) + output(&buf, tree.Root, "", true) } - return str + return buf.String() } func (node *Node[K, V]) String() string { return fmt.Sprintf("%v", node.Key) } -func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, str *string) { +func output[K comparable, V any](buf *bytes.Buffer, node *Node[K, V], prefix string, isTail bool) { if node.Right != nil { newPrefix := prefix if isTail { @@ -300,15 +302,16 @@ func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, s } else { newPrefix += " " } - output(node.Right, newPrefix, false, str) + output(buf, node.Right, newPrefix, false) } - *str += prefix + buf.WriteString(prefix) if isTail { - *str += "└── " + buf.WriteString("└── ") } else { - *str += "┌── " + buf.WriteString("┌── ") } - *str += node.String() + "\n" + buf.WriteString(node.String() + "\n") + if node.Left != nil { newPrefix := prefix if isTail { @@ -316,7 +319,7 @@ func output[K comparable, V any](node *Node[K, V], prefix string, isTail bool, s } else { newPrefix += "│ " } - output(node.Left, newPrefix, true, str) + output(buf, node.Left, newPrefix, true) } }