Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
ec115e83
Commit
ec115e83
authored
Feb 12, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Iterable<E>.toLockFreeLinkedList()` and `Sequence<E>.toLockFreeLinkedList()`
parent
71834394
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
1 deletion
+47
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
...onMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
+47
-1
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
View file @
ec115e83
...
@@ -68,6 +68,20 @@ fun <E> LockFreeLinkedList<E>.asSequence(): Sequence<E> {
...
@@ -68,6 +68,20 @@ fun <E> LockFreeLinkedList<E>.asSequence(): Sequence<E> {
}
}
}
}
/**
* 构建链表结构然后转为 [LockFreeLinkedList]
*/
fun
<
E
>
Iterable
<
E
>.
toLockFreeLinkedList
():
LockFreeLinkedList
<
E
>
{
return
LockFreeLinkedList
<
E
>().
apply
{
addLastAll
(
this
@
toLockFreeLinkedList
)
}
}
/**
* 构建链表结构然后转为 [LockFreeLinkedList]
*/
fun
<
E
>
Sequence
<
E
>.
toLockFreeLinkedList
():
LockFreeLinkedList
<
E
>
{
return
LockFreeLinkedList
<
E
>().
apply
{
addLastAll
(
this
@
toLockFreeLinkedList
)
}
}
/**
/**
* Implementation of lock-free LinkedList.
* Implementation of lock-free LinkedList.
*
*
...
@@ -111,8 +125,10 @@ open class LockFreeLinkedList<E> {
...
@@ -111,8 +125,10 @@ open class LockFreeLinkedList<E> {
}
}
open
fun
addLast
(
element
:
E
)
{
open
fun
addLast
(
element
:
E
)
{
val
node
=
element
.
asNode
(
tail
)
addLastNode
(
element
.
asNode
(
tail
))
}
private
fun
addLastNode
(
node
:
Node
<
E
>)
{
while
(
true
)
{
while
(
true
)
{
val
tail
=
head
.
iterateBeforeFirst
{
it
===
tail
}
// find the last node.
val
tail
=
head
.
iterateBeforeFirst
{
it
===
tail
}
// find the last node.
if
(
tail
.
nextNodeRef
.
compareAndSet
(
this
.
tail
,
node
))
{
// ensure the last node is the last node
if
(
tail
.
nextNodeRef
.
compareAndSet
(
this
.
tail
,
node
))
{
// ensure the last node is the last node
...
@@ -121,6 +137,36 @@ open class LockFreeLinkedList<E> {
...
@@ -121,6 +137,36 @@ open class LockFreeLinkedList<E> {
}
}
}
}
/**
* 先把元素建立好链表, 再加入到 list.
*/
@Suppress
(
"DuplicatedCode"
)
open
fun
addLastAll
(
iterable
:
Iterable
<
E
>)
{
var
currentNode
:
Node
<
E
>?
=
null
iterable
.
forEach
{
val
nextNode
=
it
.
asNode
(
tail
)
currentNode
?.
nextNode
=
nextNode
currentNode
=
nextNode
}
addLastNode
(
currentNode
?:
error
(
"iterable is empty"
))
}
/**
* 先把元素建立好链表, 再加入到 list.
*/
@Suppress
(
"DuplicatedCode"
)
open
fun
addLastAll
(
iterable
:
Sequence
<
E
>)
{
var
currentNode
:
Node
<
E
>?
=
null
iterable
.
forEach
{
val
nextNode
=
it
.
asNode
(
tail
)
if
(
currentNode
!=
null
)
{
// do not use `?.` because atomicfu cannot transform properly: IllegalArgumentException: null passed
currentNode
!!
.
nextNodeRef
.
value
=
nextNode
}
currentNode
=
nextNode
}
addLastNode
(
currentNode
?:
error
(
"iterable is empty"
))
}
open
operator
fun
plusAssign
(
element
:
E
)
=
this
.
addLast
(
element
)
open
operator
fun
plusAssign
(
element
:
E
)
=
this
.
addLast
(
element
)
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment