From b21316ea08ae9380b1aac0937bd112017c752f2a Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 9 Feb 2026 17:28:31 -0800 Subject: [PATCH] [DequeModule] Add some top-level RigidDeque/UniqueDeque docs --- .../UniqueArray/UniqueArray.swift | 10 ++- .../DequeModule/RigidDeque/RigidDeque.swift | 81 ++++++++++++++++++- .../DequeModule/UniqueDeque/UniqueDeque.swift | 71 ++++++++++++++++ 3 files changed, 157 insertions(+), 5 deletions(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index 17cfaae3..404c7c15 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -37,26 +37,28 @@ public struct UniqueArray: ~Copyable { /// /// `UniqueArray` instances automatically resize their underlying storage as /// needed to accommodate newly inserted items, using a geometric growth curve. -/// This frees code using `UniqueArray` from having to allocate enough +/// This lets code using `UniqueArray` avoid having to allocate enough /// capacity in advance; on the other hand, it makes it difficult to tell /// when and where such reallocations may happen. /// -/// For example, appending an element to a dynamic array has highly variable +/// For example, appending an element to a `UniqueArray` has highly variable /// complexity; often, it runs at a constant cost, but if the operation has to /// resize storage, then the cost of an individual append suddenly becomes /// proportional to the size of the whole array. /// /// The geometric growth curve allows the cost of such latency spikes to /// get amortized across repeated invocations, bringing the average cost back -/// to O(1); but they make this construct less suitable for use cases that +/// to O(1); but the spikes make this construct less suitable for use cases that /// expect predictable, consistent performance on every operation. /// /// Implicit growth also makes it more difficult to predict/analyze the amount /// of memory an algorithm would need. Developers targeting environments with /// stringent limits on heap allocations may prefer to avoid using dynamically -/// resizing array types as a matter of policy. The type `RigidArray` provides +/// resizing container types as a matter of policy. The type `RigidArray` provides /// a fixed-capacity array variant that caters specifically for these use cases, /// trading ease-of-use for more consistent/predictable execution. +/// For copyable elements, the copy-on-write `Array` type is an +/// even more convenient and expressive choice. @available(SwiftStdlib 5.0, *) @frozen public struct UniqueArray: ~Copyable { diff --git a/Sources/DequeModule/RigidDeque/RigidDeque.swift b/Sources/DequeModule/RigidDeque/RigidDeque.swift index 1f7f82d8..caace267 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque.swift @@ -29,7 +29,86 @@ public struct RigidDeque: ~Copyable { #else /// A fixed capacity, heap allocated, noncopyable double-ended queue of -/// potentially noncopyable elements. +/// potentially noncopyable elements. A deque (pronounced "deck") +/// is an ordered random-access container that supports efficient +/// insertions and removals from both ends. +/// +/// Deques implement the same indexing semantics as arrays: they use integer +/// indices, and the first element of a nonempty deque is always at logical +/// index zero. +/// +/// var colors = RigidDeque(capacity: 5) +/// colors.append("red") +/// colors.prepend("yellow") +/// colors.append("blue") +/// // `colors` now contains "yellow", "red", "blue" +/// +/// print(colors[0]) // prints "yellow" +/// print(colors[1]) // prints "red" +/// +/// print(colors.removeFirst()) // prints "yellow" +/// print(colors.removeLast()) // prints "blue" +/// // `colors` now contains "red" +/// +/// This double-ended nature makes deques particularly well-suited for +/// representing first-in-first-out (FIFO) buffers, where new items are +/// inserted at one end, and old items are retrieved from the other. +/// For example, they are ideal for modeling buffered data streams, such as +/// networking channels, or other producer/consumer scenarios. +/// +/// ### Implementation Notes +/// +/// `RigidDeque` implements a ring buffer. It stores its elements in a +/// single heap-allocated buffer, and to allow fast insertions and removals +/// from both ends, the buffer behaves as if its ends were glued together into +/// a ring shape. This comes at the cost of potentially discontiguous storage, as +/// the contents of the ring buffer sometimes wrap around the edges. +/// +/// While the first item in the deque is always addressed by the logical index +/// zero, its physical location can be anywhere in the underlying ring buffer. +/// Compare this with `RigidArray`'s contiguous storage, where the first item +/// is always stored at the start of the buffer, so new data can always be +/// efficiently appended to the end, but inserting at the front is relatively +/// slow, as existing elements need to be shifted to make room. +/// +/// ### Fixed Storage Capacity +/// +/// `RigidDeque` instances are created with a specific maximum capacity. +/// Elements can be added to the deque up to that capacity, but no more: trying +/// to add an item to a full deque results in a runtime trap. +/// +/// var items = RigidDeque(capacity: 2) +/// items.prepend(1) +/// items.prepend(2) +/// items.prepend(3) // Runtime error: RigidDeque capacity overflow +/// +/// Rigid deques provide convenience properties to help verify that it has +/// enough available capacity: `isFull` and `freeCapacity`. +/// +/// guard items.freeCapacity >= 4 else { throw CapacityOverflow() } +/// items.append(copying: newItems) +/// +/// It is possible to extend or shrink the capacity of a rigid deque instance, +/// but this needs to be done explicitly, with operations dedicated to this +/// purpose (such as ``reserveCapacity`` and ``reallocate(capacity:)``). +/// The deque never resizes itself automatically. +/// +/// Therefore, it is necessary to perform careful manual analysis (or up front +/// runtime capacity checks) to prevent the deque from overflowing its storage. +/// This makes this type more difficult to use than a dynamically resizing +/// container. However, it allows this construct to provide predictably stable +/// performance. +/// +/// This trading of usability in favor of stable performance limits `RigidDeque` +/// to the most resource-constrained of use cases, such as space-constrained +/// environments that require carefully accounting of every heap allocation, or +/// time-constrained applications that cannot accommodate unexpected latency +/// spikes due to a reallocation getting triggered at an inopportune moment. +/// +/// For use cases outside of these narrow domains, we generally recommmend +/// to use the dynamically resizing ``UniqueDeque`` type rather than +/// `RigidDeque`. For copyable elements, the copy-on-write `Deque` type is an +/// even more convenient and expressive choice. @available(SwiftStdlib 5.0, *) @frozen @safe diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift index 761eb6c5..ab52644c 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift @@ -16,6 +16,10 @@ import ContainersPreview #if compiler(<6.2) +/// A dynamically self-resizing, heap allocated, noncopyable double-ended +/// queue of potentially noncopyable elements. A deque (pronounced "deck") +/// is an ordered random-access container that supports efficient +/// insertions and removals from both ends. @frozen @available(*, unavailable, message: "UniqueDeque requires a Swift 6.2 toolchain") public struct UniqueDeque: ~Copyable { @@ -26,6 +30,73 @@ public struct UniqueDeque: ~Copyable { #else +/// A dynamically self-resizing, heap allocated, noncopyable double-ended +/// queue of potentially noncopyable elements. A deque (pronounced "deck") +/// is an ordered random-access container that supports efficient +/// insertions and removals from both ends. +/// +/// Deques implement the same indexing semantics as arrays: they use integer +/// indices, and the first element of a nonempty deque is always at logical +/// index zero. +/// +/// var colors = UniqueDeque() +/// colors.append("red") +/// colors.prepend("yellow") +/// colors.append("blue") +/// // `colors` now contains "yellow", "red", "blue" +/// +/// print(colors[0]) // prints "yellow" +/// print(colors[1]) // prints "red" +/// +/// print(colors.removeFirst()) // prints "yellow" +/// print(colors.removeLast()) // prints "blue" +/// // `colors` now contains "red" +/// +/// This double-ended nature makes deques particularly well-suited for +/// representing first-in-first-out (FIFO) buffers, where new items are +/// inserted at one end, and old items are retrieved from the other. +/// For example, they are ideal for modeling buffered data streams, such as +/// networking channels, or other producer/consumer scenarios. +/// +/// ### Implementation Notes +/// +/// `UniqueDeque` implements a ring buffer. It stores its elements in a +/// single heap-allocated buffer, and to allow fast insertions and removals +/// from both ends, the buffer behaves as if its ends were glued together into +/// a ring shape. This comes at the cost of potentially discontiguous storage, as +/// the contents of the ring buffer sometimes wrap around the edges. +/// +/// While the first item in the deque is always addressed by the logical index +/// zero, its physical location can be anywhere in the underlying ring buffer. +/// Compare this with `UniqueArray`'s contiguous storage, where the first item +/// is always stored at the start of the buffer, so new data can always be +/// efficiently appended to the end, but inserting at the front is relatively +/// slow, as existing elements need to be shifted to make room. +/// +/// ### Dynamic Storage Sizing +/// +/// `UniqueDeque` instances automatically resize their underlying storage as +/// needed to accommodate newly inserted items, using a geometric growth curve. +/// This lets code using `UniqueDeque` avoid having to carefully allocate enough +/// capacity in advance; on the other hand, it makes it difficult to tell +/// when and where such reallocations may happen. +/// +/// For example, appending an element to a `UniqueDeque` has highly variable +/// complexity; often, it runs at a constant cost, but if the operation has to +/// resize storage, then the cost of an individual append suddenly becomes +/// proportional to the size of the whole container. +/// +/// The geometric growth curve allows the cost of such latency spikes to +/// get amortized across repeated invocations, bringing the average cost back +/// to O(1); but the spikes make this construct less suitable for use cases that +/// expect predictable, consistent performance on every operation. +/// +/// Implicit growth also makes it more difficult to predict/analyze the amount +/// of memory an algorithm would need. Developers targeting environments with +/// stringent limits on heap allocations may prefer to avoid using dynamically +/// resizing container types as a matter of policy. The type `RigidDeque` +/// provides a fixed-capacity deque variant that caters specifically for these +/// use cases, trading ease-of-use for more consistent/predictable execution. @available(SwiftStdlib 5.0, *) @frozen @safe