func (sl *NeighborList) GetIndex(id []byte) int {
	for i, n := range sl.ToRemoteNodeList(true) {
		if CompareID(n.Id, id) == 0 {
			return i
		}
	}
	return -1
}
GetIndex returns the index of the node with an id when NeighborList is
sorted. Returns -1 if id not found.
package chord
import (
	"errors"
	"fmt"
	"math/big"
	"math/rand"
	"sort"
	"sync"
	"github.com/nknorg/nnet/log"
	"github.com/nknorg/nnet/node"
	"github.com/nknorg/nnet/protobuf"
)
const (
	randomQueryProbability = 0.5
)
type NeighborList struct {
	startID         []byte
	endID           []byte
	reversed        bool
	nodeIDBits      uint32
	maxNumNodes     uint32
	maxNumNodesLock sync.RWMutex
	nodes           sync.Map
}
func NewNeighborList(startID, endID []byte, nodeIDBits, maxNumNodes uint32, reversed bool) (*NeighborList, error) {
	if uint32(len(startID)) > nodeIDBits || uint32(len(endID)) > nodeIDBits {
		return nil, fmt.Errorf("key has more than %d bits", nodeIDBits)
	}
	sl := &NeighborList{
		startID:     startID,
		endID:       endID,
		nodeIDBits:  nodeIDBits,
		maxNumNodes: maxNumNodes,
		reversed:    reversed,
	}
	return sl, nil
}
func (sl *NeighborList) cmp(node1, node2 *protobuf.Node) int {
	res := Distance(sl.startID, node1.Id, sl.nodeIDBits).Cmp(Distance(sl.startID, node2.Id, sl.nodeIDBits))
	if sl.reversed {
		return -res
	}
	return res
}
func (sl *NeighborList) IsIDInRange(id []byte) bool {
	if sl.reversed {
		return BetweenIncl(sl.endID, sl.startID, id)
	}
	return BetweenIncl(sl.startID, sl.endID, id)
}
func (sl *NeighborList) IsEmpty() bool {
	isEmpty := true
	sl.nodes.Range(func(key, value interface{}) bool {
		isEmpty = false
		return false
	})
	return isEmpty
}
func (sl *NeighborList) Len() uint32 {
	var length uint32
	sl.nodes.Range(func(key, value interface{}) bool {
		length++
		return true
	})
	return length
}
func (sl *NeighborList) Cap() uint32 {
	sl.maxNumNodesLock.RLock()
	defer sl.maxNumNodesLock.RUnlock()
	return sl.maxNumNodes
}
func (sl *NeighborList) SetMaxNumNodes(maxNumNodes uint32) {
	sl.maxNumNodesLock.Lock()
	sl.maxNumNodes = maxNumNodes
	sl.maxNumNodesLock.Unlock()
}
func (sl *NeighborList) GetByID(id []byte) *node.RemoteNode {
	value, ok := sl.nodes.Load(string(id))
	if ok {
		rn, ok := value.(*node.RemoteNode)
		if ok {
			return rn
		}
	}
	return nil
}
func (sl *NeighborList) Exists(id []byte) bool {
	rn := sl.GetByID(id)
	return rn != nil
}