-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to improve the performance of the ordered map without generics. Apply the same approach already used for V2 in commit b46f20e.
- Loading branch information
1 parent
b46f20e
commit 1e43e19
Showing
5 changed files
with
117 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package orderedmap | ||
|
||
// Element is an element of a null terminated (non circular) intrusive doubly linked list that contains the key of the correspondent element in the ordered map too. | ||
type Element struct { | ||
// Next and previous pointers in the doubly-linked list of elements. | ||
// To simplify the implementation, internally a list l is implemented | ||
// as a ring, such that &l.root is both the next element of the last | ||
// list element (l.Back()) and the previous element of the first list | ||
// element (l.Front()). | ||
next, prev *Element | ||
|
||
// The key that corresponds to this element in the ordered map. | ||
Key interface{} | ||
|
||
// The value stored with this element. | ||
Value interface{} | ||
} | ||
|
||
// Next returns the next list element or nil. | ||
func (e *Element) Next() *Element { | ||
return e.next | ||
} | ||
|
||
// Prev returns the previous list element or nil. | ||
func (e *Element) Prev() *Element { | ||
return e.prev | ||
} | ||
|
||
// list represents a null terminated (non circular) intrusive doubly linked list. | ||
// The list is immediately usable after instantiation without the need of a dedicated initialization. | ||
type list struct { | ||
root Element // list head and tail | ||
} | ||
|
||
func (l *list) IsEmpty() bool { | ||
return l.root.next == nil | ||
} | ||
|
||
// Front returns the first element of list l or nil if the list is empty. | ||
func (l *list) Front() *Element { | ||
return l.root.next | ||
} | ||
|
||
// Back returns the last element of list l or nil if the list is empty. | ||
func (l *list) Back() *Element { | ||
return l.root.prev | ||
} | ||
|
||
// Remove removes e from its list | ||
func (l *list) Remove(e *Element) { | ||
if e.prev == nil { | ||
l.root.next = e.next | ||
} else { | ||
e.prev.next = e.next | ||
} | ||
if e.next == nil { | ||
l.root.prev = e.prev | ||
} else { | ||
e.next.prev = e.prev | ||
} | ||
e.next = nil // avoid memory leaks | ||
e.prev = nil // avoid memory leaks | ||
} | ||
|
||
// PushFront inserts a new element e with value v at the front of list l and returns e. | ||
func (l *list) PushFront(key interface{}, value interface{}) *Element { | ||
e := &Element{Key: key, Value: value} | ||
if l.root.next == nil { | ||
// It's the first element | ||
l.root.next = e | ||
l.root.prev = e | ||
return e | ||
} | ||
|
||
e.next = l.root.next | ||
l.root.next.prev = e | ||
l.root.next = e | ||
return e | ||
} | ||
|
||
// PushBack inserts a new element e with value v at the back of list l and returns e. | ||
func (l *list) PushBack(key interface{}, value interface{}) *Element { | ||
e := &Element{Key: key, Value: value} | ||
if l.root.prev == nil { | ||
// It's the first element | ||
l.root.next = e | ||
l.root.prev = e | ||
return e | ||
} | ||
|
||
e.prev = l.root.prev | ||
l.root.prev.next = e | ||
l.root.prev = e | ||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters