-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
VectorInstances.scala
88 lines (73 loc) · 3.36 KB
/
VectorInstances.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
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cats.kernel
package instances
import compat.scalaVersionSpecific._
import scala.annotation.nowarn
@suppressUnusedImportWarningForScalaVersionSpecific
trait VectorInstances extends VectorInstances1 {
implicit def catsKernelStdOrderForVector[A: Order]: Order[Vector[A]] =
new VectorOrder[A]
implicit def catsKernelStdMonoidForVector[A]: Monoid[Vector[A]] =
VectorMonoid[A]
}
private[instances] trait VectorInstances1 extends VectorInstances2 {
implicit def catsKernelStdPartialOrderForVector[A: PartialOrder]: PartialOrder[Vector[A]] =
new VectorPartialOrder[A]
implicit def catsKernelStdHashForVector[A: Hash]: Hash[Vector[A]] =
new VectorHash[A]
}
private[instances] trait VectorInstances2 {
implicit def catsKernelStdEqForVector[A: Eq]: Eq[Vector[A]] =
new VectorEq[A]
}
class VectorOrder[A](implicit ev: Order[A]) extends Order[Vector[A]] {
def compare(xs: Vector[A], ys: Vector[A]): Int =
if (xs eq ys) 0
else StaticMethods.iteratorCompare(xs.iterator, ys.iterator)
}
class VectorPartialOrder[A](implicit ev: PartialOrder[A]) extends PartialOrder[Vector[A]] {
def partialCompare(xs: Vector[A], ys: Vector[A]): Double =
if (xs eq ys) 0.0
else StaticMethods.iteratorPartialCompare(xs.iterator, ys.iterator)
}
class VectorHash[A](implicit ev: Hash[A]) extends VectorEq[A] with Hash[Vector[A]] {
def hash(xs: Vector[A]): Int = StaticMethods.orderedHash(xs)
}
class VectorEq[A](implicit ev: Eq[A]) extends Eq[Vector[A]] {
def eqv(xs: Vector[A], ys: Vector[A]): Boolean =
if (xs eq ys) true
else StaticMethods.iteratorEq(xs.iterator, ys.iterator)
}
@deprecated("Use VectorMonoid.apply, which does not allocate a new instance", "2.9.0")
class VectorMonoid[A] extends Monoid[Vector[A]] {
def empty: Vector[A] = Vector.empty
def combine(x: Vector[A], y: Vector[A]): Vector[A] = x ++ y
override def combineN(x: Vector[A], n: Int): Vector[A] =
StaticMethods.combineNIterable(Vector.newBuilder[A], x, n)
override def combineAll(xs: IterableOnce[Vector[A]]): Vector[A] =
StaticMethods.combineAllIterable(Vector.newBuilder[A], xs)
}
object VectorMonoid {
@nowarn("msg=deprecated")
private[this] val singleton: Monoid[Vector[Any]] = new VectorMonoid[Any]
def apply[A]: Monoid[Vector[A]] = singleton.asInstanceOf[Monoid[Vector[A]]]
}