// Locate searches for and returns the neares (shallowest) node with the given
// Resource ID (resid).  It returns nil if not found.
551572
func (n *Node) Locate(v Visited, resid int) *Node {
	if n == nil {
		return nil
	} else if _, ok := v[n]; ok {
		return nil
	}
	v[n] = struct{}{}

	if n.ResId == resid {
		return n
	} else if got := n.Child1.Locate(v, resid); got != nil {
		return got
	} else if got := n.Child2.Locate(v, resid); got != nil {
		return got
	}
	return nil
}
755895
func (s *Server) LocateServiceInstance(req ServiceInstanceRequest, res *service.LocationInstance) (err error) {
	location, err := s.f.LocateServiceInstance(s.context(), req.ServiceID, req.InstanceID)
	if err != nil {
		return
	}
	*res = *location
	return
}
827456
func (r *Repo) Find(ctx context.Context, id uuid.UUID) (eh.Entity, error) {
	r.FindCalled = true
	if r.LoadErr != nil {
		return nil, r.LoadErr
	}
	return r.Entity, nil
}
675865
func (nh *nodeHighlighter) findScope(search text.Region, node *parser.Node) *parser.Node {
	idx := sort.Search(len(node.Children), func(i int) bool {
		return node.Children[i].Range.A >= search.A || node.Children[i].Range.Covers(search)
	})
	for idx < len(node.Children) {
		c := node.Children[idx]
		if c.Range.A > search.B {
			break
		}
		if c.Range.Covers(search) {
			if node.Name != "" && node != nh.lastScopeNode {
				if nh.lastScopeBuf.Len() > 0 {
					nh.lastScopeBuf.WriteByte(' ')
				}
				nh.lastScopeBuf.WriteString(node.Name)
			}
			return nh.findScope(search, node.Children[idx])
		}
		idx++
	}
	if node != nh.lastScopeNode && node.Range.Covers(search) && node.Name != "" {
		if nh.lastScopeBuf.Len() > 0 {
			nh.lastScopeBuf.WriteByte(' ')
		}
		nh.lastScopeBuf.WriteString(node.Name)
		return node
	}
	return nil
}
683340
func containsNode(node, x ast.Node) (found bool) {
	ast.Walk(funcVisitor(func(n ast.Node) bool {
		if !found {
			found = n == x
		}
		return !found
	}),
		node)
	return
}
732431
func (t *UDPv4) findnode(toid enode.ID, toaddr *net.UDPAddr, target encPubkey) ([]*node, error) {
	// If we haven't seen a ping from the destination node for a while, it won't remember
	// our endpoint proof and reject findnode. Solicit a ping first.
	if time.Since(t.db.LastPingReceived(toid, toaddr.IP)) > bondExpiration {
		<-t.sendPing(toid, toaddr, nil)
		// Wait for them to ping back and process our pong.
		time.Sleep(respTimeout)
	}

	// Add a matcher for 'neighbours' replies to the pending reply queue. The matcher is
	// active until enough nodes have been received.
	nodes := make([]*node, 0, bucketSize)
	nreceived := 0
	errc := t.pending(toid, toaddr.IP, p_neighborsV4, func(r interface{}) (matched bool, requestDone bool) {
		reply := r.(*neighborsV4)
		for _, rn := range reply.Nodes {
			nreceived++
			n, err := t.nodeFromRPC(toaddr, rn)
			if err != nil {
				t.log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err)
				continue
			}
			nodes = append(nodes, n)
		}
		return true, nreceived >= bucketSize
	})
	t.send(toaddr, toid, p_findnodeV4, &findnodeV4{
		Target:     target,
		Expiration: uint64(time.Now().Add(expiration).Unix()),
	})
	return nodes, <-errc
}
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
}
1258477
func (m pathMaps) find(id reflect.Value, key string) *pathMap {
	for i := range m {
		if m[i].ma == id && m[i].key == key {
			return m[i]
		}
	}
	return nil
}
661236
func (e *ElasticIP) Find(context *fi.Context) (*ElasticIP, error) {
	return e.find(context.Cloud.(awsup.AWSCloud))
}
1098481
func (list *BalanceList) Find(id string) *Balance {
	for _, item := range list.Data {
		if item.ID == id {
			return item
		}
	}

	return nil
}
111687
func (s *EventsService) Find(e EventKey) (EventInfoDTO, *http.Response, error) {
	var t EventInfoDTO
	res, err := s.client.get(e.URI(), &t)
	return t, res, err
}
96170
func (t *Tab) GetElementByLocation(x, y int) (*Element, error) {
	_, nodeId, err := t.DOM.GetNodeForLocation(x, y, false)
	if err != nil {
		return nil, err
	}
	ele, _ := t.GetElementByNodeId(nodeId)
	return ele, nil
}
1324161
func (l *List) Find(v *Node) (e *Element) {
	if l.len == 0 {
		return nil
	}
	for e = l.Front(); e != nil && e.Value != v; e = e.Next() {
	}
	return
}
1108649
func (list *OccurrenceList) Find(id string) *Occurrence {
	for _, item := range list.Data {
		if item.ID == id {
			return item
		}
	}

	return nil
}
118783
func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx context.Context, id string) (*url.URL, http.RoundTripper, error) {
	// Allow ID as "podname" or "podname:port" or "scheme:podname:port".
	// If port is not specified, try to use the first defined port on the pod.
	scheme, name, port, valid := utilnet.SplitSchemeNamePort(id)
	if !valid {
		return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid pod request %q", id))
	}
	// TODO: if port is not a number but a "(container)/(portname)", do a name lookup.

	pod, err := getPod(getter, ctx, name)
	if err != nil {
		return nil, nil, err
	}

	// Try to figure out a port.
	if port == "" {
		for i := range pod.Spec.Containers {
			if len(pod.Spec.Containers[i].Ports) > 0 {
				port = fmt.Sprintf("%d", pod.Spec.Containers[i].Ports[0].ContainerPort)
				break
			}
		}
	}

	if err := proxyutil.IsProxyableIP(pod.Status.PodIP); err != nil {
		return nil, nil, errors.NewBadRequest(err.Error())
	}

	loc := &url.URL{
		Scheme: scheme,
	}
	if port == "" {
		loc.Host = pod.Status.PodIP
	} else {
		loc.Host = net.JoinHostPort(pod.Status.PodIP, port)
	}
	return loc, rt, nil
}
131136
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
	return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
}
41042
func (rm *StateResourceManager) findResource(id, desc string) (core.Resource, bool, error) {
	alreadyExists := false
	rsrcType, ok := resourceRegistry[desc]
	if !ok {
		return nil, alreadyExists,
			core.Errorf("No resource found for description: %q", desc)
	}

	val := reflect.New(rsrcType)
	// sanity checks
	if !val.Elem().FieldByName("CommonState").IsValid() {
		return nil, false, core.Errorf("The state structure %v is missing core.CommonState", rsrcType)
	}
	//the following works as every core.State is expected to embed core.CommonState struct
	val.Elem().FieldByName("CommonState").FieldByName("StateDriver").Set(reflect.ValueOf(rm.stateDriver))
	val.Elem().FieldByName("CommonState").FieldByName("ID").Set(reflect.ValueOf(id))

	rsrc := val.Interface().(core.Resource)
	rsrcs, err := rsrc.ReadAll()
	if core.ErrIfKeyExists(err) != nil {
		return nil, alreadyExists, err
	} else if err != nil {
		// set the slice as empty in case of 'key not found' error
		rsrcs = []core.State{}
	}

	for _, r := range rsrcs {
		//the following works as every core.State is expected to embed core.CommonState struct
		cs := reflect.ValueOf(r).Elem().FieldByName("CommonState").Interface().(core.CommonState)
		if cs.ID == id {
			alreadyExists = true
			return r.(core.Resource), alreadyExists, nil
		}
	}
	return rsrc, alreadyExists, nil
}