Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement the SortBy stream adapter #43

Merged
merged 9 commits into from
Jan 31, 2024
Merged

Commits on Jan 11, 2024

  1. feat: Replace arrayvec by smallvec.

    The motivation behind this patch is to replace `arrayvec` by `smallvec`.
    Why? Because `arrayvec` cannot grow, it's an array-baked vector. Once
    the full capacity is reached, it cannot grow any more. That was fine
    up until now because it was only used by the `Limit` stream adapter
    which needs only 2 values in a buffer that is typed by an `ArrayVec`.
    But for other stream adapters, it's a limitation. What if a stream
    adapter needs a buffer of more than 2 values?
    
    Ideally, we want to preserve the stack-allocated property for obvious
    performance reasons (cache locality, reducing allocator traffic and so
    on). However, we also want to be able to fallback to the heap for larger
    allocations.
    
    `smallvec` provides this feature. Hence, it does what `arrayvec` does,
    with the addition of heap-allocated fallback when it's needed.
    Hywan committed Jan 11, 2024
    Configuration menu
    Copy the full SHA
    9ecfc62 View commit details
    Browse the repository at this point in the history
  2. chore: Rename VectorDiffContainerStreamLimitBuf.

    This type alias is renamed to `VectorDiffContainerStreamBuffer`.
    Hywan committed Jan 11, 2024
    Configuration menu
    Copy the full SHA
    d0173f6 View commit details
    Browse the repository at this point in the history

Commits on Jan 21, 2024

  1. feat: Implement the Sort stream adapter.

    `Sort` is `VectorDiff` stream adapter that presents a sorted view of the
    underlying `ObservableVector` items.
    
    ```rust
    use eyeball_im::{ObservableVector, VectorDiff};
    use eyeball_im_util::vector::VectorObserverExt;
    use imbl::vector;
    use std::cmp::Ordering;
    use stream_assert::{assert_closed, assert_next_eq, assert_pending};
    
    // A comparison function that is used to sort our
    // `ObservableVector` values.
    fn cmp<T>(left: &T, right: &T) -> Ordering
    where
        T: Ord,
    {
        left.cmp(right)
    }
    
    // Our vector.
    let mut ob = ObservableVector::<char>::new();
    let (values, mut sub) = ob.subscribe().sort(cmp);
    //                                          ^^^
    //                                          | our comparison function
    
    assert!(values.is_empty());
    assert_pending!(sub);
    
    // Append multiple unsorted values.
    ob.append(vector!['d', 'b', 'e']);
    // We get a `VectorDiff::Append` with sorted values!
    assert_next_eq!(sub, VectorDiff::Append { values: vector!['b', 'd', 'e'] });
    
    // Let's recap what we have. `ob` is our `ObservableVector`,
    // `sub` is the “sorted view”/“sorted stream” of `ob`:
    // | `ob`  | d b e |
    // | `sub` | b d e |
    
    // Append other multiple values.
    ob.append(vector!['f', 'g', 'a', 'c']);
    // We get three `VectorDiff`s!
    assert_next_eq!(sub, VectorDiff::PushFront { value: 'a' });
    assert_next_eq!(sub, VectorDiff::Insert { index: 2, value: 'c' });
    assert_next_eq!(sub, VectorDiff::Append { values: vector!['f', 'g'] });
    
    // Let's recap what we have:
    // | `ob`  | d b e f g a c |
    // | `sub` | a b c d e f g |
    //           ^   ^     ^^^
    //           |   |     |
    //           |   |     with `VectorDiff::Append { .. }`
    //           |   with `VectorDiff::Insert { index: 2, .. }`
    //           with `VectorDiff::PushFront { .. }`
    
    // Technically, `Sort` emits `VectorDiff` that mimics a sorted `Vector`.
    
    drop(ob);
    assert_closed!(sub);
    ```
    Hywan committed Jan 21, 2024
    Configuration menu
    Copy the full SHA
    4970ec0 View commit details
    Browse the repository at this point in the history

Commits on Jan 22, 2024

  1. chore: Rename Sort to SortBy.

    Hywan committed Jan 22, 2024
    Configuration menu
    Copy the full SHA
    c51b10d View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    e8a51b6 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    15e77e5 View commit details
    Browse the repository at this point in the history
  4. chore: Format.

    Hywan committed Jan 22, 2024
    Configuration menu
    Copy the full SHA
    c729d94 View commit details
    Browse the repository at this point in the history
  5. chore: Elide some lifetimes.

    Hywan committed Jan 22, 2024
    Configuration menu
    Copy the full SHA
    d2692ae View commit details
    Browse the repository at this point in the history

Commits on Jan 31, 2024

  1. feat: Use binary search for better performance.

    This patch uses a binary search when the position of a value must be
    found. Finding a new value based on the `compare` function can be
    costly, hence the use of a binary search.
    Hywan committed Jan 31, 2024
    Configuration menu
    Copy the full SHA
    c08bac6 View commit details
    Browse the repository at this point in the history