Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default constructor for MapRef #4139

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -45,6 +45,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())
durban marked this conversation as resolved.
Show resolved Hide resolved
}

}

/**
* 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 @@ -522,5 +536,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)
}
}
Loading