-
Notifications
You must be signed in to change notification settings - Fork 6
/
item_container.go
183 lines (165 loc) · 4.03 KB
/
item_container.go
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
174
175
176
177
178
179
180
181
182
183
package sort
import (
"math/rand"
"github.com/alibaba/pairec/v2/module"
)
// Strategy use to split items by which way
type Strategy int
const (
// RecallName split Items to ItemMap by recallName
SplitByRecallName Strategy = iota
)
const (
ItemDefaultName = "DEFAULT"
)
type SortStrategy int
const (
SortByScoreStrategy SortStrategy = iota
SortByRandomStrategy
)
type ItemContainer struct {
itemMap map[string]*ItemSlot
strategy Strategy
size int
}
// ItemSlot is a group of items split by the Strategy, like SplitByRecallName
type ItemSlot struct {
// size in this slot, need return number of items
size int
index int
// capacity the slot at most have number of items, general, capacity >= size
// if other slot not have enough items, can use the extra items to supplement
capacity int
sortStrategy SortStrategy
items []*module.Item
}
func (s *ItemSlot) full() bool {
return len(s.items) == s.capacity
}
func NewItemContaienr(strategy Strategy, size int) *ItemContainer {
container := &ItemContainer{
itemMap: make(map[string]*ItemSlot),
strategy: strategy,
size: size,
}
return container
}
func (c *ItemContainer) Assign(name string, size int, capacity int, sortStrategy SortStrategy) {
if _, ok := c.itemMap[name]; !ok {
slot := &ItemSlot{
size: size,
index: 0,
capacity: capacity,
sortStrategy: sortStrategy,
}
c.itemMap[name] = slot
}
}
func (c *ItemContainer) Split(items []*module.Item) {
if c.strategy == SplitByRecallName {
c.splitByRecallName(items)
}
}
func (c *ItemContainer) splitByRecallName(items []*module.Item) {
for _, item := range items {
name := item.GetRecallName()
slot, ok := c.itemMap[name]
if ok {
if !slot.full() {
slot.items = append(slot.items, item)
}
} else {
// not found, put item in ItemDefaultName map
slot = c.itemMap[ItemDefaultName]
if slot != nil && !slot.full() {
slot.items = append(slot.items, item)
}
}
}
}
func (c *ItemContainer) Assembly() (ret []*module.Item) {
newItems := make([]*module.Item, c.size)
for _, slot := range c.itemMap {
// 第一轮优先只处理 SortByRandomStrategy, 防止后续需要移动元素位置
if slot.sortStrategy != SortByRandomStrategy {
continue
}
var itemSet []*module.Item
if len(slot.items) >= slot.size {
itemSet = append(itemSet, slot.items[:slot.size]...)
slot.index = slot.size
} else {
itemSet = append(itemSet, slot.items...)
slot.index = len(slot.items)
}
for _, item := range itemSet {
index := rand.Intn(c.size)
i := index
if newItems[i] != nil {
for i = (index + 1) % c.size; i != index; i = (i + 1) % c.size {
if newItems[i] == nil {
break
}
}
}
if newItems[i] == nil {
newItems[i] = item
}
}
}
for _, slot := range c.itemMap {
// 第二轮不再处理 SortByRandomStrategy, 已经在第一轮处理过
if slot.sortStrategy == SortByRandomStrategy {
continue
}
var itemSet []*module.Item
if len(slot.items) >= slot.size {
itemSet = append(itemSet, slot.items[:slot.size]...)
slot.index = slot.size
} else {
itemSet = append(itemSet, slot.items...)
slot.index = len(slot.items)
}
i := 0
for _, item := range itemSet {
for i < c.size && newItems[i] != nil {
i++
}
if i >= c.size {
break
}
newItems[i] = item
i++
}
}
for _, item := range newItems {
if item != nil {
ret = append(ret, item)
}
}
// if ret size not enough
if len(ret) < c.size {
diff := c.size - len(ret)
var diffItems []*module.Item
// first use ItemDefaultName slot
if slot, ok := c.itemMap[ItemDefaultName]; ok {
if len(slot.items) > slot.index {
diffItems = append(diffItems, slot.items[slot.index:]...)
}
}
if len(diffItems) < diff {
for name, slot := range c.itemMap {
if name != ItemDefaultName {
if len(slot.items) > slot.index {
diffItems = append(diffItems, slot.items[slot.index:]...)
}
}
}
}
if len(diffItems) > diff {
diffItems = diffItems[:diff]
}
ret = append(ret, diffItems...)
}
return
}