From f6493508672e33a90f0a62a420a9c3143b2a7a71 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Wed, 16 Nov 2022 11:20:54 +0100 Subject: [PATCH] use map instead of iterating a list --- pkg/pools/communities.go | 8 ++------ pkg/pools/lists.go | 39 ++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/pkg/pools/communities.go b/pkg/pools/communities.go index daa71a6..15b1153 100644 --- a/pkg/pools/communities.go +++ b/pkg/pools/communities.go @@ -20,12 +20,8 @@ type CommunitiesPool struct { // of BGP communities. func NewCommunitiesPool() *CommunitiesPool { return &CommunitiesPool{ - communitiesRoot: &Node{ - ptr: []int{}, - }, - root: &Node{ - ptr: []api.Community{}, - }, + communitiesRoot: NewNode([]int{}), + root: NewNode([]api.Community{}), } } diff --git a/pkg/pools/lists.go b/pkg/pools/lists.go index 897b125..549e82b 100644 --- a/pkg/pools/lists.go +++ b/pkg/pools/lists.go @@ -6,33 +6,30 @@ import ( // Node is a node in the tree. type Node struct { - children []*Node - value int + children map[int]*Node counter int ptr interface{} } +// NewNode creates a new tree node +func NewNode(ptr interface{}) *Node { + return &Node{ + children: map[int]*Node{}, + ptr: ptr, + } +} + // Internally acquire list by traversing the tree and // creating nodes if required. func (n *Node) traverse(list interface{}, tail []int) interface{} { - head := tail[0] + value := tail[0] tail = tail[1:] // Seek for value in children - var child *Node - for _, c := range n.children { - if c.value == head { - child = c - } - } - if child == nil { - // Insert child - child = &Node{ - children: []*Node{}, - value: head, - ptr: nil, - } - n.children = append(n.children, child) + child, ok := n.children[value] + if !ok { + child = NewNode(nil) + n.children[value] = child } // Set list ptr if required @@ -58,9 +55,7 @@ type IntListPool struct { // NewIntListPool creates a new int list pool func NewIntListPool() *IntListPool { return &IntListPool{ - root: &Node{ - ptr: []int{}, - }, + root: NewNode([]int{}), } } @@ -90,9 +85,7 @@ func NewStringListPool() *StringListPool { return &StringListPool{ head: 1, values: map[string]int{}, - root: &Node{ - ptr: []string{}, - }, + root: NewNode([]string{}), } }