-
Notifications
You must be signed in to change notification settings - Fork 343
/
AdaptiveVector.scala
252 lines (222 loc) · 9.01 KB
/
AdaptiveVector.scala
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2012 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.twitter.algebird
import com.twitter.algebird.collections.compat._
import scala.annotation.tailrec
/**
* Some functions to create or convert AdaptiveVectors
*/
object AdaptiveVector {
/**
* When density >= this value * size, we switch to dense vectors
*/
val THRESHOLD: Double = 0.25
def fill[V](size: Int)(sparse: V): AdaptiveVector[V] =
SparseVector(Map.empty[Int, V], sparse, size)
def fromVector[V](v: Vector[V], sparseVal: V): AdaptiveVector[V] =
if (v.size == 0) {
fill[V](0)(sparseVal)
} else {
val denseCount = v.count(_ != sparseVal)
val sz = v.size
if (denseCount < sz * THRESHOLD)
SparseVector(toMap(v, sparseVal), sparseVal, sz)
else
DenseVector(v, sparseVal, denseCount)
}
def fromMap[V](m: Map[Int, V], sparseVal: V, sizeOfDense: Int): AdaptiveVector[V] =
if (m.size == 0) {
fill[V](sizeOfDense)(sparseVal)
} else {
val maxIdx = m.keys.max
require(maxIdx < sizeOfDense, "Max key (" + maxIdx + ") exceeds valid for size (" + sizeOfDense + ")")
val denseCount = m.count(_._2 != sparseVal)
if (denseCount < sizeOfDense * THRESHOLD)
SparseVector(m, sparseVal, sizeOfDense)
else
DenseVector(toVector(m, sparseVal, sizeOfDense), sparseVal, denseCount)
}
def toMap[V](v: AdaptiveVector[V]): Map[Int, V] =
v match {
case DenseVector(is, sv, _) => toMap(is, sv)
case SparseVector(m, _, _) => m
}
def toMap[V](iseq: IndexedSeq[V], sparse: V): Map[Int, V] =
iseq.view.zipWithIndex.filter(_._1 != sparse).map(_.swap).toMap
def toVector[V](m: Map[Int, V], sparse: V, size: Int): Vector[V] = {
// Mutable local variable to optimize performance
import scala.collection.mutable.Buffer
val buf = Buffer.fill[V](size)(sparse)
m.foreach { case (idx, v) => buf(idx) = v }
Vector.from(buf)
}
def toVector[V](v: AdaptiveVector[V]): Vector[V] =
v match {
case DenseVector(is, _, _) => is
case SparseVector(m, v, sz) => toVector(m, v, sz)
}
private def withSparse[V](v: AdaptiveVector[V], sv: V): AdaptiveVector[V] =
if (v.sparseValue == sv) v
else fromVector(toVector(v), sv)
private class AVSemigroup[V: Semigroup] extends Semigroup[AdaptiveVector[V]] {
private def valueIsNonZero(v: V): Boolean = implicitly[Semigroup[V]] match {
case m: Monoid[_] => m.isNonZero(v)
case _ => true
}
override def plus(left: AdaptiveVector[V], right: AdaptiveVector[V]): AdaptiveVector[V] =
if (left.sparseValue != right.sparseValue) {
if (left.denseCount > right.denseCount)
plus(withSparse(left, right.sparseValue), right)
else plus(left, withSparse(right, left.sparseValue))
} else {
// they have the same sparse value
val maxSize = Ordering[Int].max(left.size, right.size)
(left, right) match {
case (DenseVector(lv, ls, _), DenseVector(rv, _, _)) =>
val vec = Semigroup.plus[IndexedSeq[V]](lv, rv) match {
case v: Vector[_] => v.asInstanceOf[Vector[V]]
case notV => Vector.from(notV)
}
fromVector(vec, ls)
case _ if valueIsNonZero(left.sparseValue) =>
fromVector(
Vector.from(Semigroup.plus(toVector(left): IndexedSeq[V], toVector(right): IndexedSeq[V])),
left.sparseValue
)
case _ => // sparse is zero:
fromMap(Semigroup.plus(toMap(left), toMap(right)), left.sparseValue, maxSize)
}
}
}
private class AVMonoid[V: Monoid] extends AVSemigroup[V] with Monoid[AdaptiveVector[V]] {
override val zero: AdaptiveVector[V] = AdaptiveVector.fill[V](0)(Monoid.zero[V])
override def isNonZero(v: AdaptiveVector[V]): Boolean = !isZero(v)
def isZero(v: AdaptiveVector[V]): Boolean = (v.size == 0) || {
val sparseAreZero =
if (Monoid.isNonZero(v.sparseValue)) v.denseCount == v.size else true
sparseAreZero &&
v.denseIterator.forall(idxv => !Monoid.isNonZero(idxv._2))
}
}
private class AVGroup[V: Group] extends AVMonoid[V] with Group[AdaptiveVector[V]] {
override def negate(v: AdaptiveVector[V]): AdaptiveVector[V] =
fromVector(toVector(v).map(Group.negate(_)), Group.negate(v.sparseValue))
}
implicit def semigroup[V: Semigroup]: Semigroup[AdaptiveVector[V]] =
new AVSemigroup[V]
implicit def monoid[V: Monoid]: Monoid[AdaptiveVector[V]] = new AVMonoid[V]
implicit def group[V: Group]: Group[AdaptiveVector[V]] = new AVGroup[V]
/*
* Equality when considering only the dense values (so size doesn't matter)
*/
def denseEquiv[V: Equiv]: Equiv[AdaptiveVector[V]] =
Equiv.fromFunction[AdaptiveVector[V]] { (l, r) =>
val (lit, rit) = (l.denseIterator, r.denseIterator)
@tailrec
def iteq: Boolean =
(lit.hasNext, rit.hasNext) match {
case (true, true) =>
val (lnext, rnext) = (lit.next(), rit.next())
if (lnext._1 == rnext._1 && Equiv[V].equiv(lnext._2, rnext._2))
iteq
else
false
case (false, false) => true
case _ => false
}
Equiv[V].equiv(l.sparseValue, r.sparseValue) && iteq
}
implicit def equiv[V: Equiv]: Equiv[AdaptiveVector[V]] =
Equiv.fromFunction[AdaptiveVector[V]] { (l, r) =>
(l.size == r.size) && (denseEquiv[V].equiv(l, r) ||
toVector(l).view.zip(toVector(r)).forall { case (lv, rv) =>
Equiv[V].equiv(lv, rv)
})
}
}
/**
* An IndexedSeq that automatically switches representation between dense and sparse depending on sparsity
* Should be an efficient representation for all sizes, and it should not be necessary to special case
* immutable algebras based on the sparsity of the vectors.
*/
sealed trait AdaptiveVector[V] extends IndexedSeq[V] {
override def length: Int = size
def sparseValue: V
/** How many items are not sparse */
def denseCount: Int
def size: Int
override def apply(idx: Int): V
def updated(idx: Int, v: V): AdaptiveVector[V]
/** Grow by adding count sparse values to the end */
def extend(count: Int): AdaptiveVector[V]
/** Iterator of indices and values of all non-sparse values */
def denseIterator: Iterator[(Int, V)]
/*
* Note that IndexedSeq provides hashCode and equals that
* work correctly based on length and apply.
*/
}
case class DenseVector[V](iseq: Vector[V], override val sparseValue: V, override val denseCount: Int)
extends AdaptiveVector[V] {
override def length: Int = iseq.size
override def apply(idx: Int): V = iseq(idx)
override def updated(idx: Int, v: V): AdaptiveVector[V] = {
val oldIsSparse = if (iseq(idx) == sparseValue) 1 else 0
val newIsSparse = if (v == sparseValue) 1 else 0
val newCount = denseCount - newIsSparse + oldIsSparse
if (denseCount < size * AdaptiveVector.THRESHOLD) {
// Go sparse
SparseVector(AdaptiveVector.toMap(iseq, sparseValue), sparseValue, size)
} else {
DenseVector(iseq.updated(idx, v), sparseValue, newCount)
}
}
override def extend(cnt: Int): AdaptiveVector[V] = {
val newSize = size + cnt
if (denseCount < newSize * AdaptiveVector.THRESHOLD) {
// Go sparse
SparseVector(AdaptiveVector.toMap(iseq, sparseValue), sparseValue, newSize)
} else {
// Stay dense
DenseVector(iseq ++ Vector.fill(cnt)(sparseValue), sparseValue, denseCount)
}
}
override def denseIterator: Iterator[(Int, V)] =
iseq.view.zipWithIndex
.filter(_._1 != sparseValue)
.map(_.swap)
.iterator
}
case class SparseVector[V](map: Map[Int, V], override val sparseValue: V, override val length: Int)
extends AdaptiveVector[V] {
override def denseCount: Int = map.size
override def apply(idx: Int): V = {
require(idx >= 0 && idx < size, "Index out of range")
map.getOrElse(idx, sparseValue)
}
override def updated(idx: Int, v: V): AdaptiveVector[V] =
if (v == sparseValue) {
SparseVector(map - idx, sparseValue, size)
} else {
val newM = map + (idx -> v)
if (newM.size >= size * AdaptiveVector.THRESHOLD) {
// Go dense:
DenseVector(AdaptiveVector.toVector(newM, sparseValue, size), sparseValue, newM.size)
} else {
SparseVector(newM, sparseValue, size)
}
}
override def extend(cnt: Int): SparseVector[V] = SparseVector(map, sparseValue, size + cnt)
private lazy val sortedList = map.toList.sortBy(_._1)
override def denseIterator: Iterator[(Int, V)] = sortedList.iterator
}