This library contains a collection of data structures implemented in Rust. It is designed to be freely used and serves as a laboratory for understanding the usage of references in Rust. The library exclusively uses safe references to ensure memory safety and prevent common issues such as dangling pointers and memory leaks.
The primary purpose of this library is to provide a set of data structures that can be used in various applications.
Additionally, it serves as a learning tool for understanding how to work with references in Rust, including the use of Rc
, RefCell
, and Weak
references. These structures emphasize safe memory management while maintaining flexibility and performance.
A vertex is a fundamental building block for linked data structures. Each vertex contains data and pointers to other vertices. This implementation is highly flexible and supports complex connections such as bi-directional links or custom relationships.
- Linked lists (single and doubly linked)
- Circular linked lists
- Graphs and networks
- Access: O(1)
- Insertion: O(1)
- Deletion: O(1)
- Each vertex is encapsulated in
Rc<RefCell<Vertex<T>>>
for multiple ownership and interior mutability. - Connections between vertices are stored in a
HashMap
, allowing flexibility in naming relationships (e.g.,PointerName::Left
,PointerName::Right
, or custom names). - Weak references (
Weak
) are used for self-referencing to prevent memory leaks caused by reference cycles.
A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the end of the queue back to the front, forming a circle. This implementation uses a doubly linked list of vertices, where each vertex can point to its neighboring vertices.
- Task scheduling
- Buffer management
- Round-robin scheduling
- Insertion: O(1)
- Removal: O(1)
- Check full/empty status: O(1)
- Implemented using a doubly linked list for efficient insertion and removal from both ends.
- Each vertex is managed using
Rc<RefCell<Vertex<T>>>
, enabling shared ownership and interior mutability. - The circular queue maintains a cursor pointing to the current vertex, supporting bi-directional traversal.
- Handles resizing dynamically with configurable maximum size (including unlimited size when set to zero).
The FIFO is a specialized queue implemented using the CircularQueue
. It enforces the FIFO principle, where elements are inserted at one end and removed from the other.
- Job queues
- Message processing
- Buffering operations
- Push: O(1)
- Pop: O(1)
- Check full/empty status: O(1)
- Built as a lightweight wrapper around the
CircularQueue
. - Provides a simplified API (
push
andpop
) for FIFO-specific operations. - Supports dynamic resizing and efficient memory reuse.
This library exclusively uses safe references to manage memory. The following types are employed to ensure safety:
Rc
(Reference Counted): Enables multiple ownership of data while automatically deallocating memory when no owners remain.RefCell
: Allows interior mutability, enabling mutation of data even when theRefCell
itself is immutable.Weak
: Provides non-owning references, preventing reference cycles and enabling safe self-referencing.
- Memory Safety: By leveraging Rust's ownership model and safe references, the library ensures that memory-related bugs like use-after-free or double-free are impossible.
- Flexibility: The use of generic types and modular design allows these structures to be applied in a variety of contexts.
- Performance: Operations are designed to achieve constant time complexity (O(1)) where possible, ensuring efficiency even under heavy workloads.
To use this library, add the module to your Rust project. You can find the core modules under the linked_list
namespace:
pub mod linked_list {
pub mod vertex;
pub mod circular_queue;
pub mod fifo;
}
use data_structures::linked_list::fifo::FIFO;
let mut fifo = FIFO::new(5);
fifo.push(1).unwrap();
fifo.push(2).unwrap();
fifo.push(3).unwrap();
assert_eq!(fifo.pop(), Some(1));
assert_eq!(fifo.pop(), Some(2));
assert_eq!(fifo.is_empty(), false);
assert_eq!(fifo.pop(), Some(3));
assert_eq!(fifo.is_empty(), true);
The library includes extensive unit tests to ensure correctness and performance. Run the tests using:
cargo test
This library is open-source and available under the MIT license. Feel free to use and modify it for your projects.