// GetSelection implements AddrBook.
// It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
// Must never return a nil address.
763160
func (a *addrBook) GetSelection() []*p2p.NetAddress {
	a.mtx.Lock()
	defer a.mtx.Unlock()

	bookSize := a.size()
	if bookSize <= 0 {
		if bookSize < 0 {
			panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
		}
		return nil
	}

	numAddresses := cmn.MaxInt(
		cmn.MinInt(minGetSelection, bookSize),
		bookSize*getSelectionPercent/100)
	numAddresses = cmn.MinInt(maxGetSelection, numAddresses)

	// XXX: instead of making a list of all addresses, shuffling, and slicing a random chunk,
	// could we just select a random numAddresses of indexes?
	allAddr := make([]*p2p.NetAddress, bookSize)
	i := 0
	for _, ka := range a.addrLookup {
		allAddr[i] = ka.Addr
		i++
	}

	// Fisher-Yates shuffle the array. We only need to do the first
	// `numAddresses' since we are throwing the rest.
	for i := 0; i < numAddresses; i++ {
		// pick a number between current index and the end
		j := cmn.RandIntn(len(allAddr)-i) + i
		allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
	}

	// slice off the limit we are willing to share.
	return allAddr[:numAddresses]
}
808987
func (s *Selection) IndexOfSelection(sel *Selection) int {
	if sel != nil && len(sel.Nodes) > 0 {
		return indexInSlice(s.Nodes, sel.Nodes[0])
	}
	return -1
}
816713
func (s *selection) getSelectedPackagesIn(id ProjectIdentifier) map[string]int {
	// TODO(sdboyer) this is horribly inefficient to do on the fly; we need a method to
	// precompute it on pushing a new dep, and preferably with an immut
	// structure so that we can pop with zero cost.
	uniq := make(map[string]int)
	for _, p := range s.projects {
		if p.a.a.id.eq(id) {
			for _, pkg := range p.a.pl {
				uniq[pkg] = uniq[pkg] + 1
			}
		}
	}

	return uniq
}
656619
func (f *Selectable) GetResult() (interface{}, error) {
	if atomic.LoadUint32(&f.filled) == 0 {
		<-f.wchan()
	}
	return f.val, f.err
}
774704
func ParseSelection(s *goquery.Selection) (a *Article, err error) {
	a = &Article{}

	a.Title = parseH3(s)
	a.Year = parseGreenLine(s)
	a.ClusterId, a.NumCite, a.NumVer, a.InfoId = parseBottom(s)
	a.Link = parseSideBar(s)

	if a.InfoId != "" {
		time.Sleep(1.0 * time.Second) // TODO: make 1.0 parameter
		a.BibTeX, err = getBibTeX(generateBibTeXLink(a.InfoId))
		if err != nil {
			return nil, err
		}
	}

	if a.BibTeX != "" {
		bibmap, err := gobibtex.Decode(a.BibTeX)
		if err != nil {
			return nil, err
		}

		if author, ok := bibmap["author"]; ok {
			a.Author = author.([]string)
		}
		if journal, ok := bibmap["journal"]; ok {
			a.Journal = journal.(string)
		}
		if booktitle, ok := bibmap["booktitle"]; ok {
			a.Booktitle = booktitle.(string)
		}
		if volume, ok := bibmap["volume"]; ok {
			a.Volume = volume.(string)
		}
		if number, ok := bibmap["number"]; ok {
			a.Number = number.(string)
		}
		if pages, ok := bibmap["pages"]; ok {
			a.Pages = pages.(string)
		}
		if publisher, ok := bibmap["publisher"]; ok {
			a.Publisher = publisher.(string)
		}
	}

	if !a.isValid() {
		return nil, errors.New(fmt.Sprintf("\"%v\" is not a valid article", a.Title.Name))
	}

	return a, nil
}
635658
func SelectionChange(listener func(*vecty.Event)) *vecty.EventListener {
	return &vecty.EventListener{Name: "selectionchange", Listener: listener}
}
1225680
func (e *Element) IsSelected() (bool, error) {
	e.lock.RLock()
	defer e.lock.RUnlock()

	if !e.ready {
		return false, &ElementNotReadyErr{}
	}

	checked, ok := e.attributes["checked"]
	if ok == true && checked != "false" {
		return true, nil
	}
	return false, nil
}
98923
func (t *Table) Select(i int) {
	t.SetSelected(i)
	if t.onSelectionChanged != nil {
		t.onSelectionChanged(t)
	}
}
932110
func (r *Random) Select(pool HostPool, request *http.Request) *UpstreamHost {

	// Because the number of available hosts isn't known
	// up front, the host is selected via reservoir sampling
	// https://en.wikipedia.org/wiki/Reservoir_sampling
	var randHost *UpstreamHost
	count := 0
	for _, host := range pool {
		if !host.Available() {
			continue
		}

		// (n % 1 == 0) holds for all n, therefore randHost
		// will always get assigned a value if there is
		// at least 1 available host
		count++
		if (rand.Int() % count) == 0 {
			randHost = host
		}
	}
	return randHost
}
732552
func (m *nodeMap) getByAddress(address string) *Node {
	m.RLock()
	node, _ := m.nodes[address]
	m.RUnlock()

	return node
}
258708
func (t *Tab) GetElementsBySelector(selector string) ([]*Element, error) {
	return t.GetDocumentElementsBySelector(t.GetTopNodeId(), selector)
}
721060
func (o ListOptions) GetTotalAndSelected() (total, selected uint64) {
	return o.total, o.selected
}
862243
func (s *Selection) PrevMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {
	if sel == nil {
		return s.PrevMatcher(filter)
	}
	return s.PrevMatcherUntilNodes(filter, sel.Nodes...)
}
1356248
func (voteSet *VoteSet) GetByIndex(valIndex int) *Vote {
	if voteSet == nil {
		return nil
	}
	voteSet.mtx.Lock()
	defer voteSet.mtx.Unlock()
	return voteSet.votes[valIndex]
}
27494
func (lbp *Random) Choose(peers []fab.Peer) (fab.Peer, error) {
	if len(peers) == 0 {
		logger.Warn("No peers to choose from!")
		return nil, nil
	}

	index := rand.Intn(len(peers))
	logger.Debugf("Choosing peer at index %d", index)
	return peers[index], nil
}
1123288
func (l *lru) Select(aps []AvailablePlugin, _ string) (AvailablePlugin, error) {
	t := time.Now()
	index := -1
	for i, ap := range aps {
		// look for the least recently used
		if ap.LastHit().Before(t) || index == -1 {
			index = i
			t = ap.LastHit()
		}
	}
	if index > -1 {
		l.logger.WithFields(log.Fields{
			"block":     "select",
			"strategy":  l.String(),
			"pool size": len(aps),
			"index":     aps[index].String(),
			"hitcount":  aps[index].HitCount(),
		}).Debug("plugin selected")
		return aps[index], nil
	}
	l.logger.WithFields(log.Fields{
		"block":    "select",
		"strategy": l.String(),
		"error":    ErrCouldNotSelect,
	}).Error("error selecting")
	return nil, ErrCouldNotSelect
}
449450
func lineSelection(text []byte) Selection {
	i, j := 0, 0
	return func() (seg []int) {
		// find next newline, if any
		for j < len(text) {
			j++
			if text[j-1] == '\n' {
				break
			}
		}
		if i < j {
			// text[i:j] constitutes a line
			seg = []int{i, j}
			i = j
		}
		return
	}
}