aboutsummaryrefslogtreecommitdiffstats
path: root/les/randselect.go
blob: 1a9d0695bdcf89058e507bf62b43b459168a77d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package les implements the Light Ethereum Subprotocol.
package les

import (
    "math/rand"
)

// wrsItem interface should be implemented by any entries that are to be selected from
// a weightedRandomSelect set. Note that recalculating monotonously decreasing item
// weights on-demand (without constantly calling update) is allowed
type wrsItem interface {
    Weight() int64
}

// weightedRandomSelect is capable of weighted random selection from a set of items
type weightedRandomSelect struct {
    root *wrsNode
    idx  map[wrsItem]int
}

// newWeightedRandomSelect returns a new weightedRandomSelect structure
func newWeightedRandomSelect() *weightedRandomSelect {
    return &weightedRandomSelect{root: &wrsNode{maxItems: wrsBranches}, idx: make(map[wrsItem]int)}
}

// update updates an item's weight, adds it if it was non-existent or removes it if
// the new weight is zero. Note that explicitly updating decreasing weights is not necessary.
func (w *weightedRandomSelect) update(item wrsItem) {
    w.setWeight(item, item.Weight())
}

// remove removes an item from the set
func (w *weightedRandomSelect) remove(item wrsItem) {
    w.setWeight(item, 0)
}

// setWeight sets an item's weight to a specific value (removes it if zero)
func (w *weightedRandomSelect) setWeight(item wrsItem, weight int64) {
    idx, ok := w.idx[item]
    if ok {
        w.root.setWeight(idx, weight)
        if weight == 0 {
            delete(w.idx, item)
        }
    } else {
        if weight != 0 {
            if w.root.itemCnt == w.root.maxItems {
                // add a new level
                newRoot := &wrsNode{sumWeight: w.root.sumWeight, itemCnt: w.root.itemCnt, level: w.root.level + 1, maxItems: w.root.maxItems * wrsBranches}
                newRoot.items[0] = w.root
                newRoot.weights[0] = w.root.sumWeight
                w.root = newRoot
            }
            w.idx[item] = w.root.insert(item, weight)
        }
    }
}

// choose randomly selects an item from the set, with a chance proportional to its
// current weight. If the weight of the chosen element has been decreased since the
// last stored value, returns it with a newWeight/oldWeight chance, otherwise just
// updates its weight and selects another one
func (w *weightedRandomSelect) choose() wrsItem {
    for {
        if w.root.sumWeight == 0 {
            return nil
        }
        val := rand.Int63n(w.root.sumWeight)
        choice, lastWeight := w.root.choose(val)
        weight := choice.Weight()
        if weight != lastWeight {
            w.setWeight(choice, weight)
        }
        if weight >= lastWeight || rand.Int63n(lastWeight) < weight {
            return choice
        }
    }
}

const wrsBranches = 8 // max number of branches in the wrsNode tree

// wrsNode is a node of a tree structure that can store wrsItems or further wrsNodes.
type wrsNode struct {
    items                    [wrsBranches]interface{}
    weights                  [wrsBranches]int64
    sumWeight                int64
    level, itemCnt, maxItems int
}

// insert recursively inserts a new item to the tree and returns the item index
func (n *wrsNode) insert(item wrsItem, weight int64) int {
    branch := 0
    for n.items[branch] != nil && (n.level == 0 || n.items[branch].(*wrsNode).itemCnt == n.items[branch].(*wrsNode).maxItems) {
        branch++
        if branch == wrsBranches {
            panic(nil)
        }
    }
    n.itemCnt++
    n.sumWeight += weight
    n.weights[branch] += weight
    if n.level == 0 {
        n.items[branch] = item
        return branch
    } else {
        var subNode *wrsNode
        if n.items[branch] == nil {
            subNode = &wrsNode{maxItems: n.maxItems / wrsBranches, level: n.level - 1}
            n.items[branch] = subNode
        } else {
            subNode = n.items[branch].(*wrsNode)
        }
        subIdx := subNode.insert(item, weight)
        return subNode.maxItems*branch + subIdx
    }
}

// setWeight updates the weight of a certain item (which should exist) and returns
// the change of the last weight value stored in the tree
func (n *wrsNode) setWeight(idx int, weight int64) int64 {
    if n.level == 0 {
        oldWeight := n.weights[idx]
        n.weights[idx] = weight
        diff := weight - oldWeight
        n.sumWeight += diff
        if weight == 0 {
            n.items[idx] = nil
            n.itemCnt--
        }
        return diff
    }
    branchItems := n.maxItems / wrsBranches
    branch := idx / branchItems
    diff := n.items[branch].(*wrsNode).setWeight(idx-branch*branchItems, weight)
    n.weights[branch] += diff
    n.sumWeight += diff
    if weight == 0 {
        n.itemCnt--
    }
    return diff
}

// choose recursively selects an item from the tree and returns it along with its weight
func (n *wrsNode) choose(val int64) (wrsItem, int64) {
    for i, w := range n.weights {
        if val < w {
            if n.level == 0 {
                return n.items[i].(wrsItem), n.weights[i]
            } else {
                return n.items[i].(*wrsNode).choose(val)
            }
        } else {
            val -= w
        }
    }
    panic(nil)
}