// Search for a Comparable in the tree and returns the node that contains it.
// If no node does, returns nil.
540246
func (b *BinaryTree) search(target Comparable) *node {
	current := b.root
	for current != nil {
		switch current.value.CompareTo(target) {
		case -1:
			current = current.left
		case 0:
			return current
		case 1:
			current = current.right
		}
	}
	return nil
}
246680
func nodesEqual(a, b *api.Node) bool {
	a, b = a.Copy(), b.Copy()

	a.Status, b.Status = api.NodeStatus{}, api.NodeStatus{}
	a.Meta, b.Meta = api.Meta{}, api.Meta{}

	return reflect.DeepEqual(a, b)
}
1107836
func (n *Node) searchKey(comparator Comparator, value interface{}) (*Key, int) {
	i := n.search(comparator, value)

	if n.IsLeaf && i == len(n.ChildValues) { // not found
		return nil, i
	}

	if n.IsLeaf { // equal number of ids and values
		return n.ChildKeys[i], i
	}

	if i == len(n.ChildValues) { // we need to go to the farthest node to the write
		return n.ChildKeys[len(n.ChildKeys)-1], i
	}

	return n.ChildKeys[i], i
}
832197
func (t *Tree) Get(k generic.T) (v generic.U, ok bool) {
	q := t.r
	if q == nil {
		return
	}

	for {
		var i int
		if i, ok = t.find(q, k); ok {
			switch x := q.(type) {
			case *x:
				return x.x[i].sep.d[0].v, true
			case *d:
				return x.d[i].v, true
			}
		}
		switch x := q.(type) {
		case *x:
			q = x.x[i].ch
		default:
			return
		}
	}
}
399623
func (t *redBlackTree) Search(key keytype) (valuetype, bool) {
	if t.root == nil {
		return nil, false
	}
	return t.root.search(key)
}
538938
func (mtt *mttNode) search(nodes []*mttNode, ns []string) []*mttNode {
	parent := mtt
	var children []*mttNode

	if parent.children == nil {
		return nodes
	}
	if len(ns) == 1 {
		// the last element of ns is under searching process
		switch ns[0] {
		case "*":
			// fetch all descendants when wildcard ends namespace
			children = parent.fetch([]string{})
		default:
			children = parent.gatherChildren(ns[0])
		}
		nodes = append(nodes, children...)
		return nodes
	}
	children = parent.gatherChildren(ns[0])

	for _, child := range children {
		nodes = child.search(nodes, ns[1:])
	}
	return nodes
}
370739
func (node ColIdent) Equal(in ColIdent) bool {
	return node.Lowered() == in.Lowered()
}
166226
func (t *PrimaryTree) Seek(k string) (e *PrimaryEnumerator, ok bool) {
	q := t.r
	if q == nil {
		e = btEPool1.get(nil, false, 0, k, nil, t, atomic.LoadUint64(&t.ver))
		return
	}

	for {
		var i int
		if i, ok = t.find(q, k); ok {
			switch x := q.(type) {
			case *x1:
				q = x.x[i+1].ch
				continue
			case *d1:
				return btEPool1.get(nil, ok, i, k, x, t, atomic.LoadUint64(&t.ver)), true
			}
		}

		switch x := q.(type) {
		case *x1:
			q = x.x[i].ch
		case *d1:
			return btEPool1.get(nil, ok, i, k, x, t, atomic.LoadUint64(&t.ver)), false
		}
	}
}
690919
func (tn *TreeNode) FindChild(path string) (*TreeNode, bool) {
	tn.RLock()
	defer tn.RUnlock()
	node, ok := tn.children[path]
	return node, ok
}
550785
func (c *Cursor) search(key []byte, pgid pgid) {
	p, n := c.bucket.pageNode(pgid)
	if p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 {
		panic(fmt.Sprintf("invalid page type: %d: %x", p.id, p.flags))
	}
	e := elemRef{page: p, node: n}
	c.stack = append(c.stack, e)

	// If we're on a leaf page/node then find the specific node.
	if e.isLeaf() {
		c.nsearch(key)
		return
	}

	if n != nil {
		c.searchNode(key, n)
		return
	}
	c.searchPage(key, p)
}
805020
func (n *Node) ComparableResources() *ComparableResources {
	// Node already has 0.9+ behavior
	if n.NodeResources != nil {
		return n.NodeResources.Comparable()
	}

	// Upgrade path
	return &ComparableResources{
		Flattened: AllocatedTaskResources{
			Cpu: AllocatedCpuResources{
				CpuShares: int64(n.Resources.CPU),
			},
			Memory: AllocatedMemoryResources{
				MemoryMB: int64(n.Resources.MemoryMB),
			},
		},
		Shared: AllocatedSharedResources{
			DiskMB: int64(n.Resources.DiskMB),
		},
	}
}
525922
func (t *Mutable) get(key []byte) (*treapNode, *treapNode) {
	var parent *treapNode
	for node := t.root; node != nil; {
		// Traverse left or right depending on the result of the
		// comparison.
		compareResult := bytes.Compare(key, node.key)
		if compareResult < 0 {
			parent = node
			node = node.left
			continue
		}
		if compareResult > 0 {
			parent = node
			node = node.right
			continue
		}

		// The key exists.
		return node, parent
	}

	// A nil node was reached which means the key does not exist.
	return nil, nil
}
645788
func (t *Tree) Search(qname string) (*Elem, bool) {
	if t.Root == nil {
		return nil, false
	}
	n, res := t.Root.search(qname)
	if n == nil {
		return nil, res
	}
	return n.Elem, res
}
1367951
func (t *Trie) findNode(key string) (node *Node, index int) {
	curNode := t.root
	f := false
	for k, v := range key {
		if f {
			index = k
			f = false
		}
		if curNode.child[v] == nil {
			return nil, index
		}
		curNode = curNode.child[v]
		if curNode.exists {
			f = true
		}
	}

	if curNode.exists {
		index = len(key)
	}

	return curNode, index
}
131682
func (g *genericScheduler) findNodesThatFit(pod *v1.Pod, nodes []*v1.Node) ([]*v1.Node, FailedPredicateMap, error) {
	var filtered []*v1.Node
	failedPredicateMap := FailedPredicateMap{}

	if len(g.predicates) == 0 {
		filtered = nodes
	} else {
		allNodes := int32(g.cache.NodeTree().NumNodes())
		numNodesToFind := g.numFeasibleNodesToFind(allNodes)

		// Create filtered list with enough space to avoid growing it
		// and allow assigning.
		filtered = make([]*v1.Node, numNodesToFind)
		errs := errors.MessageCountMap{}
		var (
			predicateResultLock sync.Mutex
			filteredLen         int32
		)

		ctx, cancel := context.WithCancel(context.Background())

		// We can use the same metadata producer for all nodes.
		meta := g.predicateMetaProducer(pod, g.nodeInfoSnapshot.NodeInfoMap)

		checkNode := func(i int) {
			nodeName := g.cache.NodeTree().Next()
			fits, failedPredicates, err := podFitsOnNode(
				pod,
				meta,
				g.nodeInfoSnapshot.NodeInfoMap[nodeName],
				g.predicates,
				g.schedulingQueue,
				g.alwaysCheckAllPredicates,
			)
			if err != nil {
				predicateResultLock.Lock()
				errs[err.Error()]++
				predicateResultLock.Unlock()
				return
			}
			if fits {
				length := atomic.AddInt32(&filteredLen, 1)
				if length > numNodesToFind {
					cancel()
					atomic.AddInt32(&filteredLen, -1)
				} else {
					filtered[length-1] = g.nodeInfoSnapshot.NodeInfoMap[nodeName].Node()
				}
			} else {
				predicateResultLock.Lock()
				failedPredicateMap[nodeName] = failedPredicates
				predicateResultLock.Unlock()
			}
		}

		// Stops searching for more nodes once the configured number of feasible nodes
		// are found.
		workqueue.ParallelizeUntil(ctx, 16, int(allNodes), checkNode)

		filtered = filtered[:filteredLen]
		if len(errs) > 0 {
			return []*v1.Node{}, FailedPredicateMap{}, errors.CreateAggregateFromMessageCountMap(errs)
		}
	}

	if len(filtered) > 0 && len(g.extenders) != 0 {
		for _, extender := range g.extenders {
			if !extender.IsInterested(pod) {
				continue
			}
			filteredList, failedMap, err := extender.Filter(pod, filtered, g.nodeInfoSnapshot.NodeInfoMap)
			if err != nil {
				if extender.IsIgnorable() {
					klog.Warningf("Skipping extender %v as it returned error %v and has ignorable flag set",
						extender, err)
					continue
				} else {
					return []*v1.Node{}, FailedPredicateMap{}, err
				}
			}

			for failedNodeName, failedMsg := range failedMap {
				if _, found := failedPredicateMap[failedNodeName]; !found {
					failedPredicateMap[failedNodeName] = []predicates.PredicateFailureReason{}
				}
				failedPredicateMap[failedNodeName] = append(failedPredicateMap[failedNodeName], predicates.NewFailureReason(failedMsg))
			}
			filtered = filteredList
			if len(filtered) == 0 {
				break
			}
		}
	}
	return filtered, failedPredicateMap, nil
}
1155107
func (r *Router) findNode(pathSegments []string, fuzzy bool) (*routingTreeNode, []string) {
	node := r.root
	for i, p := range pathSegments {
		if c, found := node.children[p]; found {
			node = c
		} else {
			if fuzzy { // try fuzzy search
				candidates := []string{}
				for key, _ := range node.children {
					if strings.HasPrefix(key, p) {
						candidates = append(candidates, key)
					}
				}
				if len(candidates) == 1 {
					node = node.children[candidates[0]]
					continue
				}
			}
			return node, pathSegments[i:]
		}
	}
	return node, nil
}
51501
func (tr *RTree) Search(min, max []float64, iter func(item interface{}) bool) bool {
	bbox := &treeNode{min: min, max: max}
	if !tr.data.intersects(bbox) {
		return true
	}
	return tr.search(tr.data, bbox, iter)
}