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
bf645717
Commit
bf645717
authored
Feb 04, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve LockFreeLinkedList
parent
42221c5d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
5 deletions
+38
-5
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt
.../commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt
+3
-3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
...onMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
+35
-2
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt
View file @
bf645717
...
@@ -24,10 +24,12 @@ class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedLis
...
@@ -24,10 +24,12 @@ class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedLis
operator
fun
get
(
id
:
Long
):
C
=
delegate
[
id
]
operator
fun
get
(
id
:
Long
):
C
=
delegate
[
id
]
fun
getOrNull
(
id
:
Long
):
C
?
=
delegate
.
getOrNull
(
id
)
fun
getOrNull
(
id
:
Long
):
C
?
=
delegate
.
getOrNull
(
id
)
fun
containsId
(
id
:
Long
):
Boolean
=
delegate
.
getOrNull
(
id
)
!=
null
@Deprecated
(
"Use contains instead"
,
ReplaceWith
(
"contains(id)"
))
fun
containsId
(
id
:
Long
):
Boolean
=
contains
(
id
)
val
size
:
Int
get
()
=
delegate
.
size
val
size
:
Int
get
()
=
delegate
.
size
operator
fun
contains
(
element
:
C
):
Boolean
=
delegate
.
contains
(
element
)
operator
fun
contains
(
element
:
C
):
Boolean
=
delegate
.
contains
(
element
)
operator
fun
contains
(
id
:
Long
):
Boolean
=
delegate
.
getOrNull
(
id
)
!=
null
fun
containsAll
(
elements
:
Collection
<
C
>):
Boolean
=
elements
.
all
{
contains
(
it
)
}
fun
containsAll
(
elements
:
Collection
<
C
>):
Boolean
=
elements
.
all
{
contains
(
it
)
}
fun
isEmpty
():
Boolean
=
delegate
.
isEmpty
()
fun
isEmpty
():
Boolean
=
delegate
.
isEmpty
()
inline
fun
forEach
(
block
:
(
C
)
->
Unit
)
=
delegate
.
forEach
(
block
)
inline
fun
forEach
(
block
:
(
C
)
->
Unit
)
=
delegate
.
forEach
(
block
)
...
@@ -50,5 +52,3 @@ inline fun <C : Contact> LockFreeLinkedList<C>.filteringGetOrNull(filter: (C) ->
...
@@ -50,5 +52,3 @@ inline fun <C : Contact> LockFreeLinkedList<C>.filteringGetOrNull(filter: (C) ->
return
null
return
null
}
}
fun
<
C
:
Contact
>
LockFreeLinkedList
<
C
>.
getOrAdd
(
id
:
Long
,
supplier
:
()
->
C
):
C
=
filteringGetOrAdd
({
it
.
id
==
id
},
supplier
)
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
View file @
bf645717
...
@@ -19,12 +19,12 @@ inline fun <E> LockFreeLinkedList<E>.joinToString(
...
@@ -19,12 +19,12 @@ inline fun <E> LockFreeLinkedList<E>.joinToString(
}.
dropLast
(
separator
.
length
)
+
postfix
}.
dropLast
(
separator
.
length
)
+
postfix
/**
/**
*
Returns a [List] containing all the elements in [this] in the same order
*
Collect all the elements into a [MutableList] then cast it as a [List]
*/
*/
fun
<
E
>
LockFreeLinkedList
<
E
>.
toList
():
List
<
E
>
=
toMutableList
()
fun
<
E
>
LockFreeLinkedList
<
E
>.
toList
():
List
<
E
>
=
toMutableList
()
/**
/**
*
Returns a [MutableList] containing all the elements in [this] in the same order
*
Collect all the elements into a [MutableList].
*/
*/
fun
<
E
>
LockFreeLinkedList
<
E
>.
toMutableList
():
MutableList
<
E
>
{
fun
<
E
>
LockFreeLinkedList
<
E
>.
toMutableList
():
MutableList
<
E
>
{
val
list
=
mutableListOf
<
E
>()
val
list
=
mutableListOf
<
E
>()
...
@@ -32,6 +32,33 @@ fun <E> LockFreeLinkedList<E>.toMutableList(): MutableList<E> {
...
@@ -32,6 +32,33 @@ fun <E> LockFreeLinkedList<E>.toMutableList(): MutableList<E> {
return
list
return
list
}
}
/**
* Collect all the elements into a [MutableSet] then cast it as a [Set]
*/
fun
<
E
>
LockFreeLinkedList
<
E
>.
toSet
():
Set
<
E
>
=
toMutableSet
()
/**
* Collect all the elements into a [MutableSet].
*/
fun
<
E
>
LockFreeLinkedList
<
E
>.
toMutableSet
():
MutableSet
<
E
>
{
val
list
=
mutableSetOf
<
E
>()
this
.
forEach
{
list
.
add
(
it
)
}
return
list
}
/**
* Builds a [Sequence] containing all the elements in [this] in the same order.
*
* Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required
*/
fun
<
E
>
LockFreeLinkedList
<
E
>.
asSequence
():
Sequence
<
E
>
{
return
sequence
{
forEach
{
yield
(
it
)
}
}
}
/**
/**
* Implementation of lock-free LinkedList.
* Implementation of lock-free LinkedList.
*
*
...
@@ -87,6 +114,9 @@ open class LockFreeLinkedList<E> {
...
@@ -87,6 +114,9 @@ open class LockFreeLinkedList<E> {
open
operator
fun
plusAssign
(
element
:
E
)
=
this
.
addLast
(
element
)
open
operator
fun
plusAssign
(
element
:
E
)
=
this
.
addLast
(
element
)
/**
* 过滤并获取, 获取不到则添加一个元素.
*/
inline
fun
filteringGetOrAdd
(
filter
:
(
E
)
->
Boolean
,
noinline
supplier
:
()
->
E
):
E
{
inline
fun
filteringGetOrAdd
(
filter
:
(
E
)
->
Boolean
,
noinline
supplier
:
()
->
E
):
E
{
val
node
=
LazyNode
(
tail
,
supplier
)
val
node
=
LazyNode
(
tail
,
supplier
)
...
@@ -149,6 +179,9 @@ open class LockFreeLinkedList<E> {
...
@@ -149,6 +179,9 @@ open class LockFreeLinkedList<E> {
}
}
}
}
/**
* 动态计算的大小
*/
val
size
:
Int
get
()
=
head
.
countChildIterate
<
Node
<
E
>>({
it
.
nextNode
},
{
it
!
is
Tail
})
-
1
// empty head is always included
val
size
:
Int
get
()
=
head
.
countChildIterate
<
Node
<
E
>>({
it
.
nextNode
},
{
it
!
is
Tail
})
-
1
// empty head is always included
open
operator
fun
contains
(
element
:
E
):
Boolean
{
open
operator
fun
contains
(
element
:
E
):
Boolean
{
...
...
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