Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: use bytes.Buffer instead of String Concatenation #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions maps/linkedhashmap/linkedhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package linkedhashmap

import (
"bytes"
"fmt"
"strings"

Expand Down Expand Up @@ -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(), " ") + "]"
}
13 changes: 8 additions & 5 deletions maps/treemap/treemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package treemap

import (
"bytes"
"cmp"
"fmt"
"strings"
Expand Down Expand Up @@ -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(), " ") + "]"
}
23 changes: 13 additions & 10 deletions trees/avltree/avltree.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package avltree

import (
"bytes"
"cmp"
"fmt"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -442,30 +444,31 @@ 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 {
newPrefix += "│ "
} 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 {
newPrefix += " "
} else {
newPrefix += "│ "
}
output(node.Children[0], newPrefix, true, str)
output(buf, node.Children[0], newPrefix, true)
}
}
23 changes: 13 additions & 10 deletions trees/redblacktree/redblacktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package redblacktree

import (
"bytes"
"cmp"
"fmt"

Expand Down Expand Up @@ -281,42 +282,44 @@ 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 {
newPrefix += "│ "
} 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 {
newPrefix += " "
} else {
newPrefix += "│ "
}
output(node.Left, newPrefix, true, str)
output(buf, node.Left, newPrefix, true)
}
}

Expand Down