Skip to content

Commit

Permalink
Merge pull request #4139 from yisraelU/MapRef-DefaultCtor
Browse files Browse the repository at this point in the history
Add default constructor for `MapRef`
  • Loading branch information
armanbilge authored Nov 21, 2024
2 parents e9d3db2 + 030ef43 commit 2d27493
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/MapRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] {

object MapRef extends MapRefCompanionPlatform {

/**
* Default constructor for [[MapRef]]. If [[Sync]] is available, it will delegate to
* [[ofConcurrentHashMap]], otherwise it will fallback to [[ofShardedImmutableMap]].
*/
def apply[F[_]: Concurrent, K, V]: F[MapRef[F, K, Option[V]]] = {
Concurrent[F] match {
case s: Sync[F] =>
ofConcurrentHashMap()(s)
case _ =>
ofShardedImmutableMap[F, K, V](shardCount = Runtime.getRuntime.availableProcessors())
}

}

/**
* Creates a sharded map ref to reduce atomic contention on the Map, given an efficient and
* equally distributed hash, the contention should allow for interaction like a general
Expand Down Expand Up @@ -511,5 +525,8 @@ object MapRef extends MapRefCompanionPlatform {
val (set, out) = f(v)
(set.some, out.some)
}

def withDefaultValue(default: V)(implicit E: Eq[V], F: Functor[F]): MapRef[F, K, V] =
defaultedMapRef(mRef, default)
}
}

0 comments on commit 2d27493

Please sign in to comment.