-
Notifications
You must be signed in to change notification settings - Fork 105
add answers to exercise 1-7 chapter 3 #16
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 answers to exercise 1-7 chapter 3 #16
Conversation
|
||
var r = new AtomicReference[List[T]](List.empty[T]) | ||
|
||
def push(x: T): Unit = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can require that this method is tail recursive, to avoid the danger of a stack overflow.
Thanks for the PR! |
Hi @axel22, P.S. |
class ConcurrentSortedList[T](implicit val ord: Ordering[T]) { | ||
|
||
private val h: AtomicReference[Option[T]] = new AtomicReference[Option[T]](None) | ||
private val t: AtomicReference[Option[ConcurrentSortedList[T]]] = new AtomicReference[Option[ConcurrentSortedList[T]]](None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would model the list with the following inner class:
class Node(val head: T) {
val tail = new AtomicReference[Option[Node]](None)
}
The ConcurrentSortedList
would then contain a single reference:
val root = AtomicReference[Option[Node]](None)
This would make the implementation of the add
method simpler, and easier to reason about. There would be no need to call set
in addToTail
, since you set it only once anyway.
Thanks for replying, |
Hi @axel22 , |
LGTM - thanks a lot for following up! |
add answers to exercise 1-7 chapter 3
thank you for your time |
Hi @axel22, pls check this answers