func (i *Iterator) Prev() *Element {
	if i.state == LeftEndReachedState || i.current == nil {
		panic("no next element found in iterator")
	}
	if i.state == IterationStartedState {
		i.current = i.wrapNode(i.current.Left)
	}
	if i.current.Left == nil {
		i.state = LeftEndReachedState
	} else {
		i.state = IterationStartedState
	}
	return i.current
}
Prev returns the previous Element in the Iterator and moves back the internal pointer. The method panics if there is
no previous Element that can be retrieved (always use HasPrev to check if another Element can be requested).
package thresholdmap
import (
	"github.com/emirpasic/gods/trees/redblacktree"
	"github.com/emirpasic/gods/utils"
	"github.com/iotaledger/hive.go/datastructure/genericcomparator"
)
type ThresholdMap struct {
	mode Mode
	tree *redblacktree.Tree
}
func New(mode Mode, optionalComparator ...genericcomparator.Type) *ThresholdMap {
	if len(optionalComparator) >= 1 {
		return &ThresholdMap{
			mode: mode,
			tree: redblacktree.NewWith(utils.Comparator(optionalComparator[0])),
		}
	}
	return &ThresholdMap{
		mode: mode,
		tree: redblacktree.NewWith(genericcomparator.Comparator),
	}
}
func (t *ThresholdMap) Set(key interface{}, value interface{}) {
	t.tree.Put(key, value)
}
func (t *ThresholdMap) Get(key interface{}) (value interface{}, exists bool) {
	var foundNode *redblacktree.Node
	switch t.mode {
	case UpperThresholdMode:
		foundNode, exists = t.tree.Ceiling(key)
	case LowerThresholdMode:
		foundNode, exists = t.tree.Floor(key)
	default:
		panic("unsupported mode")
	}
	if exists {
		value = foundNode.Value
	}
	return
}
func (t *ThresholdMap) Floor(key interface{}) (floorKey interface{}, floorValue interface{}, exists bool) {
	if node, exists := t.tree.Floor(key); exists {
		return node.Key, node.Value, true
	}
	return nil, nil, false
}
func (t *ThresholdMap) Ceiling(key interface{}) (floorKey interface{}, floorValue interface{}, exists bool) {
	if node, exists := t.tree.Ceiling(key); exists {
		return node.Key, node.Value, true
	}
	return nil, nil, false
}
func (t *ThresholdMap) Delete(key interface{}) (element *Element, success bool) {
	node := t.lookup(key)
	if node != nil {
		t.tree.Remove(key)
	}
	return t.wrapNode(node), node != nil
}
func (t *ThresholdMap) lookup(key interface{}) *redblacktree.Node {
	node := t.tree.Root
	for node != nil {
		compare := t.tree.Comparator(key, node.Key)
		switch {
		case compare == 0:
			return node
		case compare < 0:
			node = node.Left
		case compare > 0:
			node = node.Right
		}
	}
	return nil
}
func (t *ThresholdMap) Keys() []interface{} {
	return t.tree.Keys()
}
func (t *ThresholdMap) Values() []interface{} {
	return t.tree.Values()
}
func (t *ThresholdMap) GetElement(key interface{}) *Element {
	switch t.mode {
	case UpperThresholdMode:
		ceiling, _ := t.tree.Ceiling(key)
		return t.wrapNode(ceiling)
	default:
		floor, _ := t.tree.Floor(key)
		return t.wrapNode(floor)
	}
}
func (t *ThresholdMap) MinElement() *Element {
	return t.wrapNode(t.tree.Left())
}
func (t *ThresholdMap) MaxElement() *Element {
	return t.wrapNode(t.tree.Right())
}
func (t *ThresholdMap) DeleteElement(element *Element) {
	if element == nil {
		return
	}
	t.tree.Remove(element.Node)
}
func (t *ThresholdMap) ForEach(iterator func(node *Element) bool) {
	for it := t.tree.Iterator(); it.Next(); {
		if !iterator(t.wrapNode(&redblacktree.Node{Key: it.Key(), Value: it.Value()})) {
			break
		}
	}
}
func (t *ThresholdMap) Iterator(optionalStartingNode ...*Element) *Iterator {
	if len(optionalStartingNode) >= 1 {
		return NewIterator(optionalStartingNode[0])
	}
	return NewIterator(t.wrapNode(t.tree.Left()))
}
func (t *ThresholdMap) Size() int {
	return t.tree.Size()
}
func (t *ThresholdMap) Empty() bool {
	return t.tree.Empty()
}
func (t *ThresholdMap) Clear() {
	t.tree.Clear()
}
func (t *ThresholdMap) wrapNode(node *redblacktree.Node) (element *Element) {
	if node == nil {
		return
	}
	return &Element{node}
}
type Element struct {
	*redblacktree.Node
}
func (e *Element) Key() interface{} {
	return e.Node.Key
}
func (e *Element) Value() interface{} {
	return e.Node.Value
}
type Mode bool
const (
	LowerThresholdMode = true
	UpperThresholdMode = false
)
type Iterator struct {
	start   *Element
	current *Element
	state   IteratorState
}
func NewIterator(startingElement *Element) *Iterator {
	return &Iterator{
		start:   startingElement,
		current: startingElement,
	}
}
func (i *Iterator) State() IteratorState {
	return i.state
}
func (i *Iterator) HasNext() bool {
	switch i.state {
	case InitialState:
		fallthrough
	case LeftEndReachedState:
		return i.current != nil
	case IterationStartedState:
		return i.current.Right != nil
	}
	return false
}
func (i *Iterator) HasPrev() bool {
	switch i.state {
	case InitialState:
		fallthrough
	case RightEndReachedState:
		return i.current != nil
	case IterationStartedState:
		return i.current.Left != nil
	}
	return false
}
func (i *Iterator) Next() *Element {
	if i.state == RightEndReachedState || i.current == nil {
		panic("no next element found in iterator")
	}
	if i.state == IterationStartedState {
		i.current = i.wrapNode(i.current.Right)
	}
	if i.current.Right == nil {
		i.state = RightEndReachedState
	} else {
		i.state = IterationStartedState
	}
	return i.current
}