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
111d9d0d
Commit
111d9d0d
authored
Dec 13, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add docs
parent
47d6d6e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
10 deletions
+57
-10
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
...onMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
+57
-10
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
View file @
111d9d0d
...
@@ -4,6 +4,7 @@ package net.mamoe.mirai.utils
...
@@ -4,6 +4,7 @@ package net.mamoe.mirai.utils
import
kotlinx.atomicfu.AtomicRef
import
kotlinx.atomicfu.AtomicRef
import
kotlinx.atomicfu.atomic
import
kotlinx.atomicfu.atomic
import
net.mamoe.mirai.utils.Node.Companion.equals
@MiraiExperimentalAPI
@MiraiExperimentalAPI
...
@@ -15,7 +16,10 @@ inline fun <E> lockFreeLinkedListOf(vararg elements: E): LockFreeLinkedList<E> =
...
@@ -15,7 +16,10 @@ inline fun <E> lockFreeLinkedListOf(vararg elements: E): LockFreeLinkedList<E> =
inline
fun
<
E
>
lockFreeLinkedListOf
():
LockFreeLinkedList
<
E
>
=
LockFreeLinkedList
<
E
>()
inline
fun
<
E
>
lockFreeLinkedListOf
():
LockFreeLinkedList
<
E
>
=
LockFreeLinkedList
<
E
>()
/**
/**
* 无锁链表实现. 元素值不能为 null
* Implementation of lock-free LinkedList.
*
* Modifying can be performed concurrently.
* Iterating concurrency is guaranteed. // TODO: 2019/12/13 ADD MORE
*/
*/
@MiraiExperimentalAPI
@MiraiExperimentalAPI
class
LockFreeLinkedList
<
E
>
:
MutableList
<
E
>,
RandomAccess
{
class
LockFreeLinkedList
<
E
>
:
MutableList
<
E
>,
RandomAccess
{
...
@@ -37,7 +41,7 @@ class LockFreeLinkedList<E> : MutableList<E>, RandomAccess {
...
@@ -37,7 +41,7 @@ class LockFreeLinkedList<E> : MutableList<E>, RandomAccess {
}
}
internal
fun
getLinkStucture
():
String
=
buildString
{
internal
fun
getLinkStucture
():
String
=
buildString
{
head
.
childIterate
<
Node
<
*
>>({
head
.
childIterate
ReturnsLastSatisfying
<
Node
<
*
>>({
append
(
it
.
toString
())
append
(
it
.
toString
())
append
(
"->"
)
append
(
"->"
)
it
.
nextNode
it
.
nextNode
...
@@ -253,9 +257,10 @@ class LockFreeLinkedList<E> : MutableList<E>, RandomAccess {
...
@@ -253,9 +257,10 @@ class LockFreeLinkedList<E> : MutableList<E>, RandomAccess {
private
inline
fun
<
E
>
E
.
asNode
(
nextNode
:
Node
<
E
>):
Node
<
E
>
=
Node
(
nextNode
).
apply
{
nodeValueRef
.
value
=
this
@
asNode
}
private
inline
fun
<
E
>
E
.
asNode
(
nextNode
:
Node
<
E
>):
Node
<
E
>
=
Node
(
nextNode
).
apply
{
nodeValueRef
.
value
=
this
@
asNode
}
/**
/**
* 使用 [iterator] 进行自我迭代, 直到 [mustBeTrue] 返回 false 时停止迭代. 返回最后一个满足条件的元素
* Self-iterate using the [iterator], until [mustBeTrue] returns `false`.
* Returns the element at the last time when the [mustBeTrue] returns `true`
*/
*/
private
inline
fun
<
N
:
Node
<
*
>>
N
.
childIterate
(
iterator
:
(
N
)
->
N
,
mustBeTrue
:
(
N
)
->
Boolean
):
N
{
private
inline
fun
<
N
:
Node
<
*
>>
N
.
childIterate
ReturnsLastSatisfying
(
iterator
:
(
N
)
->
N
,
mustBeTrue
:
(
N
)
->
Boolean
):
N
{
if
(!
mustBeTrue
(
this
))
return
this
if
(!
mustBeTrue
(
this
))
return
this
var
value
:
N
=
this
var
value
:
N
=
this
...
@@ -272,7 +277,8 @@ private inline fun <N : Node<*>> N.childIterate(iterator: (N) -> N, mustBeTrue:
...
@@ -272,7 +277,8 @@ private inline fun <N : Node<*>> N.childIterate(iterator: (N) -> N, mustBeTrue:
}
}
/**
/**
* 使用 [iterator] 进行自我迭代, 直到 [mustBeTrue] 返回 false 时停止迭代. 返回第一个不满足条件的元素
* Self-iterate using the [iterator], until [mustBeTrue] returns `false`.
* Returns the element at the first time when the [mustBeTrue] returns `false`
*/
*/
private
inline
fun
<
E
>
E
.
childIterateReturnFirstUnsitisfying
(
iterator
:
(
E
)
->
E
,
mustBeTrue
:
(
E
)
->
Boolean
):
E
{
private
inline
fun
<
E
>
E
.
childIterateReturnFirstUnsitisfying
(
iterator
:
(
E
)
->
E
,
mustBeTrue
:
(
E
)
->
Boolean
):
E
{
if
(!
mustBeTrue
(
this
))
return
this
if
(!
mustBeTrue
(
this
))
return
this
...
@@ -291,7 +297,8 @@ private inline fun <E> E.childIterateReturnFirstUnsitisfying(iterator: (E) -> E,
...
@@ -291,7 +297,8 @@ private inline fun <E> E.childIterateReturnFirstUnsitisfying(iterator: (E) -> E,
}
}
/**
/**
* 使用 [iterator] 进行自我迭代, 直到 [mustBeTrue] 返回 false 时停止迭代. 返回满足条件的元素数量
* Self-iterate using the [iterator], until [mustBeTrue] returns `false`.
* Returns the count of elements being iterated.
*/
*/
private
inline
fun
<
E
>
E
.
countChildIterate
(
iterator
:
(
E
)
->
E
,
mustBeTrue
:
(
E
)
->
Boolean
):
Int
{
private
inline
fun
<
E
>
E
.
countChildIterate
(
iterator
:
(
E
)
->
E
,
mustBeTrue
:
(
E
)
->
Boolean
):
Int
{
var
count
=
0
var
count
=
0
...
@@ -330,6 +337,9 @@ private open class Node<E>(
...
@@ -330,6 +337,9 @@ private open class Node<E>(
val
nodeValueRef
:
AtomicRef
<
E
?
>
=
atomic
(
null
)
val
nodeValueRef
:
AtomicRef
<
E
?
>
=
atomic
(
null
)
/**
* Short cut for accessing [nodeValueRef]
*/
inline
var
nodeValue
:
E
?
inline
var
nodeValue
:
E
?
get
()
=
nodeValueRef
.
value
get
()
=
nodeValueRef
.
value
set
(
value
)
{
set
(
value
)
{
...
@@ -338,6 +348,10 @@ private open class Node<E>(
...
@@ -338,6 +348,10 @@ private open class Node<E>(
@Suppress
(
"LeakingThis"
)
@Suppress
(
"LeakingThis"
)
val
nextNodeRef
:
AtomicRef
<
Node
<
E
>>
=
atomic
(
nextNode
?:
this
)
val
nextNodeRef
:
AtomicRef
<
Node
<
E
>>
=
atomic
(
nextNode
?:
this
)
/**
* Short cut for accessing [nextNodeRef]
*/
inline
var
nextNode
:
Node
<
E
>
inline
var
nextNode
:
Node
<
E
>
get
()
=
nextNodeRef
.
value
get
()
=
nextNodeRef
.
value
set
(
value
)
{
set
(
value
)
{
...
@@ -345,28 +359,61 @@ private open class Node<E>(
...
@@ -345,28 +359,61 @@ private open class Node<E>(
}
}
inline
fun
iterateWhile
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Node
<
E
>
=
this
.
childIterate
<
Node
<
E
>>({
it
.
nextNode
},
filter
)
/**
* Returns the former node of the last node whence [filter] returnes true
*/
inline
fun
iterateBeforeFirst
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Node
<
E
>
=
inline
fun
iterateBeforeFirst
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Node
<
E
>
=
this
.
childIterate
<
Node
<
E
>>({
it
.
nextNode
},
{
!
filter
(
it
)
})
this
.
childIterate
ReturnsLastSatisfying
<
Node
<
E
>>({
it
.
nextNode
},
{
!
filter
(
it
)
})
/**
* Returns the last node whence [filter] returnes true.
*/
inline
fun
iterateStopOnFirst
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Node
<
E
>
=
inline
fun
iterateStopOnFirst
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Node
<
E
>
=
iterateBeforeFirst
(
filter
).
nextNode
iterateBeforeFirst
(
filter
).
nextNode
/**
* Returns the former node of next node whose value is not null.
* [Tail] is returned if no node is found
*/
@Suppress
(
"NOTHING_TO_INLINE"
)
@Suppress
(
"NOTHING_TO_INLINE"
)
inline
fun
iterateBeforeNotnull
():
Node
<
E
>
=
iterateBeforeFirst
{
it
.
nodeValue
!=
null
}
inline
fun
iterateBeforeNotnull
():
Node
<
E
>
=
iterateBeforeFirst
{
it
.
nodeValue
!=
null
}
/**
* Returns the next node which is not [Tail] or [Head] and with a notnull value.
* [Tail] is returned if no node is found
*/
@Suppress
(
"NOTHING_TO_INLINE"
)
@Suppress
(
"NOTHING_TO_INLINE"
)
inline
fun
nextValidElement
():
Node
<
E
>
=
this
.
iterateBeforeFirst
{
!
it
.
isValidElementNode
()
}
inline
fun
nextValidElement
():
Node
<
E
>
=
this
.
iterateBeforeFirst
{
!
it
.
isValidElementNode
()
}
/**
* Returns the next node whose value is not null.
* [Tail] is returned if no node is found
*/
@Suppress
(
"NOTHING_TO_INLINE"
)
@Suppress
(
"NOTHING_TO_INLINE"
)
inline
fun
nextNotnull
():
Node
<
E
>
=
this
.
iterateBeforeFirst
{
it
.
nodeValueRef
.
value
==
null
}
inline
fun
nextNotnull
():
Node
<
E
>
=
this
.
iterateBeforeFirst
{
it
.
nodeValueRef
.
value
==
null
}
inline
fun
allMatching
(
filter
:
(
Node
<
E
>)
->
Boolean
):
Boolean
=
this
.
iterateWhile
(
filter
)
!
is
Tail
/**
* Check if all the node which is not [Tail] matches the [condition]
*
* Head, which is this, is also being tested.
* [Tail], is not being tested.
*/
inline
fun
allMatching
(
condition
:
(
Node
<
E
>)
->
Boolean
):
Boolean
=
this
.
childIterateReturnsLastSatisfying
<
Node
<
E
>>({
it
.
nextNode
},
condition
)
!
is
Tail
/**
* Stop on and returns the former element of the element that is [equals] to the [element]
*
* E.g.: for `head <- 1 <- 2 <- 3 <- tail`, `iterateStopOnNodeValue(2)` returns the node whose value is 1
*/
@Suppress
(
"NOTHING_TO_INLINE"
)
@Suppress
(
"NOTHING_TO_INLINE"
)
inline
fun
iterateBeforeNodeValue
(
element
:
E
):
Node
<
E
>
=
this
.
iterateBeforeFirst
{
it
.
nodeValueRef
.
value
==
element
}
inline
fun
iterateBeforeNodeValue
(
element
:
E
):
Node
<
E
>
=
this
.
iterateBeforeFirst
{
it
.
nodeValueRef
.
value
==
element
}
/**
* Stop on and returns the element that is [equals] to the [element]
*
* E.g.: for `head <- 1 <- 2 <- 3 <- tail`, `iterateStopOnNodeValue(2)` returns the node whose value is 2
*/
@Suppress
(
"NOTHING_TO_INLINE"
)
@Suppress
(
"NOTHING_TO_INLINE"
)
inline
fun
iterateStopOnNodeValue
(
element
:
E
):
Node
<
E
>
=
this
.
iterateBeforeNodeValue
(
element
).
nextNode
inline
fun
iterateStopOnNodeValue
(
element
:
E
):
Node
<
E
>
=
this
.
iterateBeforeNodeValue
(
element
).
nextNode
}
}
...
...
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