-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: pathfinding: filter out paths with repeated tracks during explo…
…ration Signed-off-by: Eloi Charpentier <[email protected]>
- Loading branch information
Showing
3 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
core/kt-osrd-utils/src/main/kotlin/fr/sncf/osrd/utils/AppendOnlySet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package fr.sncf.osrd.utils | ||
|
||
/** | ||
* Append-only set implementation. The internal structure is a linked list. Used to store data on | ||
* diverging paths while minimizing copies. See also `AppendOnlyLinkedList`. | ||
*/ | ||
class AppendOnlySet<T>( | ||
private val list: AppendOnlyLinkedList<T>, | ||
private val keyFilter: BloomFilter<T>, | ||
) { | ||
|
||
/** Add the given value to the set. O(1). */ | ||
fun add(k: T) { | ||
keyFilter.add(k) | ||
list.add(k) | ||
} | ||
|
||
/** Returns a copy of the set. The underlying structure is *not* copied. O(1). */ | ||
fun shallowCopy(): AppendOnlySet<T> { | ||
return AppendOnlySet(list.shallowCopy(), keyFilter.copy()) | ||
} | ||
|
||
/** | ||
* Returns true if the value is in the set. O(n) in worst case, O(1) if the value is near the | ||
* end. Pre-filtered using a bloom filter. | ||
*/ | ||
fun contains(key: T): Boolean { | ||
if (!keyFilter.mayContain(key)) return false | ||
return list.findLast { it == key } != null | ||
} | ||
} | ||
|
||
/** Returns a new empty set */ | ||
fun <T> appendOnlySetOf(): AppendOnlySet<T> { | ||
return AppendOnlySet(appendOnlyLinkedListOf(), emptyBloomFilter()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters