-
Notifications
You must be signed in to change notification settings - Fork 23
/
columns.go
181 lines (158 loc) · 3.68 KB
/
columns.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
package songlist
import (
"github.com/ambientsound/pms/song"
"github.com/ambientsound/pms/utils"
)
type Column struct {
tag string
items int
totalWidth int
maxWidth int
avg int
width int
}
type Columns []*Column
type ColumnMap map[string]*Column
// NewColumn returns a new Column.
func NewColumn(tag string) *Column {
return &Column{tag: tag}
}
// Set calculates all song's widths.
func (c *Column) Set(s Songlist) {
c.Reset()
for _, song := range s.Songs() {
c.Add(song)
}
}
// Add a single song's width to the total and maximum width.
func (c *Column) Add(song *song.Song) {
l := len(song.Tags[c.tag])
if l == 0 {
return
}
c.avg = 0
c.items++
c.totalWidth += l
c.maxWidth = utils.Max(c.maxWidth, l)
}
// Remove a single song's tag width from the total and maximum width.
func (c *Column) Remove(song *song.Song) {
l := len(song.Tags[c.tag])
if l == 0 {
return
}
c.avg = 0
c.items--
c.totalWidth -= l
// TODO: c.maxWidth is not updated
}
// Reset sets all values to zero.
func (c *Column) Reset() {
c.items = 0
c.totalWidth = 0
c.maxWidth = 0
c.avg = 0
c.width = 0
}
// Weight returns the relative usefulness of this column. It might happen that
// a tag appears rarely, but is very long. In this case we reduce the field so
// that other tags get more space.
func (c *Column) Weight(max int) float64 {
return float64(c.items) / float64(max)
}
// Avg returns the average length of the tag values in this column.
func (c *Column) Avg() int {
if c.avg == 0 {
if c.items == 0 {
c.avg = 0
} else {
c.avg = c.totalWidth / c.items
}
}
//console.Log("Avg() of %s is %d", c.Tag(), c.avg)
return c.avg
}
// Tag returns the tag name.
func (c *Column) Tag() string {
return c.tag
}
// MaxWidth returns the length of the longest tag value in this column.
func (c *Column) MaxWidth() int {
return c.maxWidth
}
// Width returns the column width.
func (c *Column) Width() int {
return c.width
}
// SetWidth sets the width that the column should consume.
func (c *Column) SetWidth(width int) {
c.width = width
}
// expand adjusts the column widths equally between the different columns,
// giving affinity to weight.
func (columns Columns) Expand(totalWidth int) {
if len(columns) == 0 {
return
}
usedWidth := 0
poolSize := len(columns)
saturated := make([]bool, poolSize)
// Start with the average value
for i := range columns {
avg := columns[i].Avg()
columns[i].SetWidth(avg)
usedWidth += avg
}
// expand as long as there is space left
for {
for i := range columns {
if usedWidth > totalWidth {
return
}
if poolSize > 0 && saturated[i] {
continue
}
col := columns[i]
if poolSize > 0 && col.Width() > col.MaxWidth() {
saturated[i] = true
poolSize--
continue
}
col.SetWidth(col.Width() + 1)
usedWidth++
}
}
}
// Add adds song tags to all applicable columns.
func (c ColumnMap) Add(song *song.Song) {
for tag := range song.StringTags {
c[tag].Add(song)
}
}
// Remove removes song tags from all applicable columns.
func (c ColumnMap) Remove(song *song.Song) {
for tag := range song.StringTags {
c[tag].Remove(song)
}
}
// ensureColumns makes sure that all of a song's tags exists in the column map.
func (s *BaseSonglist) ensureColumns(song *song.Song) {
for tag := range song.StringTags {
if _, ok := s.columns[tag]; !ok {
s.columns[tag] = NewColumn(tag)
}
}
}
// Columns returns a slice of columns, containing only the columns which has
// the specified tags.
func (s *BaseSonglist) Columns(columns []string) Columns {
cols := make(Columns, 0)
for _, tag := range columns {
col := s.columns[tag]
if col == nil {
col = NewColumn(tag)
}
cols = append(cols, col)
}
return cols
}