Exemplo n.º 1
0
func ExampleUnique() {
	// Create some array of data
	data := []int{1, 5, 4, 3, 1, 3, 2, 5, 4, 3, 3, 0, 0}

	// Sort it
	sort.Ints(data)

	// Get unique elements and siplay them
	n := sortext.Unique(sort.IntSlice(data))
	fmt.Println("Uniques:", data[:n])

	// Output:
	// Uniques: [0 1 2 3 4 5]
}
Exemplo n.º 2
0
// Merges two leafsets and returns the result.
func (o *Overlay) mergeLeaves(a, b []*big.Int) []*big.Int {
	// Append, circular sort and fetch uniques
	res := append(a, b...)
	sort.Sort(idSlice{o.nodeId, res})
	res = res[:sortext.Unique(idSlice{o.nodeId, res})]

	// Look for the origin point
	origin := 0
	for o.nodeId.Cmp(res[origin]) != 0 {
		origin++
	}
	// Fetch the nearest nodes in both directions
	min := mathext.MaxInt(0, origin-config.PastryLeaves/2)
	max := mathext.MinInt(len(res), origin+config.PastryLeaves/2)
	return res[min:max]
}
Exemplo n.º 3
0
// Searches a potential routing table for nodes not yet connected.
func (o *Overlay) discover(t *table) []*big.Int {
	o.lock.RLock()
	defer o.lock.RUnlock()

	ids := []*big.Int{}
	for _, id := range t.leaves {
		if id.Cmp(o.nodeId) != 0 {
			if _, ok := o.livePeers[id.String()]; !ok {
				ids = append(ids, id)
			}
		}
	}
	for _, row := range t.routes {
		for _, id := range row {
			if id != nil {
				if _, ok := o.livePeers[id.String()]; !ok {
					ids = append(ids, id)
				}
			}
		}
	}
	sortext.BigInts(ids)
	return ids[:sortext.Unique(sortext.BigIntSlice(ids))]
}