-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from YosysHQ/dsf
Add dsf crates with union_find and tracked_union_find
- Loading branch information
Showing
19 changed files
with
1,521 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ members = [ | |
"imctk", | ||
"lit", | ||
"stable_set", | ||
"union_find", | ||
|
||
# comment to force multi-line layout | ||
] | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! An allocator for IDs that allows concurrent access from multiple threads. | ||
use std::sync::atomic::Ordering::Relaxed; | ||
use std::{marker::PhantomData, sync::atomic::AtomicUsize}; | ||
|
||
use crate::{Id, IdRange}; | ||
|
||
/// An allocator for IDs that allows concurrent access from multiple threads. | ||
pub struct IdAlloc<T> { | ||
counter: AtomicUsize, | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T: Id> Default for IdAlloc<T> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
/// `IdAllocError` indicates that there are not enough IDs remaining. | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct IdAllocError; | ||
|
||
impl<T: Id> IdAlloc<T> { | ||
/// Constructs a new ID allocator. | ||
pub const fn new() -> Self { | ||
Self { | ||
counter: AtomicUsize::new(0), | ||
_phantom: PhantomData, | ||
} | ||
} | ||
fn alloc_indices(&self, n: usize) -> Result<usize, IdAllocError> { | ||
self.counter | ||
.fetch_update(Relaxed, Relaxed, |current_id| { | ||
current_id | ||
.checked_add(n) | ||
.filter(|&index| index <= T::MAX_ID_INDEX.saturating_add(1)) | ||
}) | ||
.map_err(|_| IdAllocError) | ||
} | ||
/// Allocates a single ID. | ||
pub fn alloc(&self) -> Result<T, IdAllocError> { | ||
self.alloc_indices(1).map(|index| { | ||
// SAFETY: the precondition was checked by `alloc_indices` | ||
unsafe { T::from_id_index_unchecked(index) } | ||
}) | ||
} | ||
/// Allocates a contiguous range of the specified size. | ||
pub fn alloc_range(&self, n: usize) -> Result<IdRange<T>, IdAllocError> { | ||
self.alloc_indices(n).map(|start| { | ||
// SAFETY: the precondition was checked by `alloc_indices` | ||
unsafe { IdRange::from_index_range_unchecked(start..start + n) } | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.