Introduce Cursor/CursorMut/CursorMutKey thrichotomy for `BTreeS… · model-checking/verify-rust-std@c7be27f (original) (raw)

`@@ -1249,25 +1249,25 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {

`

1249

1249

`///

`

1250

1250

`/// let mut set = BTreeSet::from([1, 2, 3, 4]);

`

1251

1251

`///

`

1252

``

`-

/// let mut cursor = unsafe { set.lower_bound_mut(Bound::Included(&2)) };

`

``

1252

`+

/// let mut cursor = set.lower_bound_mut(Bound::Included(&2));

`

1253

1253

`/// assert_eq!(cursor.peek_prev(), Some(&mut 1));

`

1254

1254

`/// assert_eq!(cursor.peek_next(), Some(&mut 2));

`

1255

1255

`///

`

1256

``

`-

/// let mut cursor = unsafe { set.lower_bound_mut(Bound::Excluded(&2)) };

`

``

1256

`+

/// let mut cursor = set.lower_bound_mut(Bound::Excluded(&2));

`

1257

1257

`/// assert_eq!(cursor.peek_prev(), Some(&mut 2));

`

1258

1258

`/// assert_eq!(cursor.peek_next(), Some(&mut 3));

`

1259

1259

`///

`

1260

``

`-

/// let mut cursor = unsafe { set.lower_bound_mut(Bound::Unbounded) };

`

``

1260

`+

/// let mut cursor = set.lower_bound_mut(Bound::Unbounded);

`

1261

1261

`/// assert_eq!(cursor.peek_prev(), None);

`

1262

1262

`/// assert_eq!(cursor.peek_next(), Some(&mut 1));

`

1263

1263

```` /// ```

````

1264

1264

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

1265

``

`-

pub unsafe fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>

`

``

1265

`+

pub fn lower_bound_mut<Q: ?Sized>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, T, A>

`

1266

1266

`where

`

1267

1267

`T: Borrow + Ord,

`

1268

1268

`Q: Ord,

`

1269

1269

`{

`

1270

``

`-

CursorMut { inner: unsafe { self.map.lower_bound_mut(bound).with_mutable_key() } }

`

``

1270

`+

CursorMut { inner: self.map.lower_bound_mut(bound) }

`

1271

1271

`}

`

1272

1272

``

1273

1273

`` /// Returns a [Cursor] pointing at the gap after the greatest element

``

`@@ -1353,7 +1353,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {

`

1353

1353

`T: Borrow + Ord,

`

1354

1354

`Q: Ord,

`

1355

1355

`{

`

1356

``

`-

CursorMut { inner: unsafe { self.map.upper_bound_mut(bound).with_mutable_key() } }

`

``

1356

`+

CursorMut { inner: self.map.upper_bound_mut(bound) }

`

1357

1357

`}

`

1358

1358

`}

`

1359

1359

``

`@@ -2010,6 +2010,31 @@ impl<K: Debug> Debug for Cursor<'_, K> {

`

2010

2010

`}

`

2011

2011

`}

`

2012

2012

``

``

2013

`` +

/// A cursor over a BTreeSet with editing operations.

``

``

2014

`+

///

`

``

2015

`` +

/// A Cursor is like an iterator, except that it can freely seek back-and-forth, and can

``

``

2016

`+

/// safely mutate the set during iteration. This is because the lifetime of its yielded

`

``

2017

`+

/// references is tied to its own lifetime, instead of just the underlying map. This means

`

``

2018

`+

/// cursors cannot yield multiple elements at once.

`

``

2019

`+

///

`

``

2020

`+

/// Cursors always point to a gap between two elements in the set, and can

`

``

2021

`+

/// operate on the two immediately adjacent elements.

`

``

2022

`+

///

`

``

2023

`` +

/// A CursorMut is created with the [BTreeSet::lower_bound_mut] and [BTreeSet::upper_bound_mut]

``

``

2024

`+

/// methods.

`

``

2025

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2026

`+

pub struct CursorMut<'a, K: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A = Global>

`

``

2027

`+

{

`

``

2028

`+

inner: super::map::CursorMut<'a, K, SetValZST, A>,

`

``

2029

`+

}

`

``

2030

+

``

2031

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2032

`+

impl<K: Debug, A> Debug for CursorMut<'_, K, A> {

`

``

2033

`+

fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

`

``

2034

`+

f.write_str("CursorMut")

`

``

2035

`+

}

`

``

2036

`+

}

`

``

2037

+

2013

2038

`` /// A cursor over a BTreeSet with editing operations, and which allows

``

2014

2039

`/// mutating elements.

`

2015

2040

`///

`

`@@ -2021,8 +2046,8 @@ impl<K: Debug> Debug for Cursor<'_, K> {

`

2021

2046

`/// Cursors always point to a gap between two elements in the set, and can

`

2022

2047

`/// operate on the two immediately adjacent elements.

`

2023

2048

`///

`

2024

``

`` -

/// A CursorMut is created with the [BTreeSet::lower_bound_mut] and

``

2025

``

`` -

/// [BTreeSet::upper_bound_mut] methods.

``

``

2049

`` +

/// A CursorMutKey is created from a [CursorMut] with the

``

``

2050

`` +

/// [CursorMut::with_mutable_key] method.

``

2026

2051

`///

`

2027

2052

`/// # Safety

`

2028

2053

`///

`

`@@ -2032,15 +2057,18 @@ impl<K: Debug> Debug for Cursor<'_, K> {

`

2032

2057

`/// * The newly inserted element must be unique in the tree.

`

2033

2058

`/// * All elements in the tree must remain in sorted order.

`

2034

2059

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2035

``

`-

pub struct CursorMut<'a, K: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A = Global>

`

2036

``

`-

{

`

``

2060

`+

pub struct CursorMutKey<

`

``

2061

`+

'a,

`

``

2062

`+

K: 'a,

`

``

2063

`+

#[unstable(feature = "allocator_api", issue = "32838")] A = Global,

`

``

2064

`+

{

`

2037

2065

`inner: super::map::CursorMutKey<'a, K, SetValZST, A>,

`

2038

2066

`}

`

2039

2067

``

2040

2068

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2041

``

`-

impl<K: Debug, A> Debug for CursorMut<'_, K, A> {

`

``

2069

`+

impl<K: Debug, A> Debug for CursorMutKey<'_, K, A> {

`

2042

2070

`fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

`

2043

``

`-

f.write_str("CursorMut")

`

``

2071

`+

f.write_str("CursorMutKey")

`

2044

2072

`}

`

2045

2073

`}

`

2046

2074

``

`@@ -2089,7 +2117,7 @@ impl<'a, T, A> CursorMut<'a, T, A> {

`

2089

2117

`` /// If the cursor is already at the end of the set then None is returned

``

2090

2118

`/// and the cursor is not moved.

`

2091

2119

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2092

``

`-

pub fn next(&mut self) -> Option<&mut T> {

`

``

2120

`+

pub fn next(&mut self) -> Option<&T> {

`

2093

2121

`self.inner.next().map(|(k, _)| k)

`

2094

2122

`}

`

2095

2123

``

`@@ -2099,23 +2127,23 @@ impl<'a, T, A> CursorMut<'a, T, A> {

`

2099

2127

`` /// If the cursor is already at the start of the set then None is returned

``

2100

2128

`/// and the cursor is not moved.

`

2101

2129

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2102

``

`-

pub fn prev(&mut self) -> Option<&mut T> {

`

``

2130

`+

pub fn prev(&mut self) -> Option<&T> {

`

2103

2131

`self.inner.prev().map(|(k, _)| k)

`

2104

2132

`}

`

2105

2133

``

2106

2134

`/// Returns a reference to the next element without moving the cursor.

`

2107

2135

`///

`

2108

2136

`` /// If the cursor is at the end of the set then None is returned.

``

2109

2137

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2110

``

`-

pub fn peek_next(&mut self) -> Option<&mut T> {

`

``

2138

`+

pub fn peek_next(&mut self) -> Option<&T> {

`

2111

2139

`self.inner.peek_next().map(|(k, _)| k)

`

2112

2140

`}

`

2113

2141

``

2114

2142

`/// Returns a reference to the previous element without moving the cursor.

`

2115

2143

`///

`

2116

2144

`` /// If the cursor is at the start of the set then None is returned.

``

2117

2145

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2118

``

`-

pub fn peek_prev(&mut self) -> Option<&mut T> {

`

``

2146

`+

pub fn peek_prev(&mut self) -> Option<&T> {

`

2119

2147

`self.inner.peek_prev().map(|(k, _)| k)

`

2120

2148

`}

`

2121

2149

``

`@@ -2129,6 +2157,70 @@ impl<'a, T, A> CursorMut<'a, T, A> {

`

2129

2157

`pub fn as_cursor(&self) -> Cursor<'_, T> {

`

2130

2158

`Cursor { inner: self.inner.as_cursor() }

`

2131

2159

`}

`

``

2160

+

``

2161

`` +

/// Converts the cursor into a [CursorMutKey], which allows mutating

``

``

2162

`+

/// elements in the tree.

`

``

2163

`+

///

`

``

2164

`+

/// # Safety

`

``

2165

`+

///

`

``

2166

`+

/// Since this cursor allows mutating elements, you must ensure that the

`

``

2167

`` +

/// BTreeSet invariants are maintained. Specifically:

``

``

2168

`+

///

`

``

2169

`+

/// * The newly inserted element must be unique in the tree.

`

``

2170

`+

/// * All elements in the tree must remain in sorted order.

`

``

2171

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2172

`+

pub unsafe fn with_mutable_key(self) -> CursorMutKey<'a, T, A> {

`

``

2173

`+

CursorMutKey { inner: unsafe { self.inner.with_mutable_key() } }

`

``

2174

`+

}

`

``

2175

`+

}

`

``

2176

+

``

2177

`+

impl<'a, T, A> CursorMutKey<'a, T, A> {

`

``

2178

`+

/// Advances the cursor to the next gap, returning the element that it

`

``

2179

`+

/// moved over.

`

``

2180

`+

///

`

``

2181

`` +

/// If the cursor is already at the end of the set then None is returned

``

``

2182

`+

/// and the cursor is not moved.

`

``

2183

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2184

`+

pub fn next(&mut self) -> Option<&mut T> {

`

``

2185

`+

self.inner.next().map(|(k, _)| k)

`

``

2186

`+

}

`

``

2187

+

``

2188

`+

/// Advances the cursor to the previous gap, returning the element that it

`

``

2189

`+

/// moved over.

`

``

2190

`+

///

`

``

2191

`` +

/// If the cursor is already at the start of the set then None is returned

``

``

2192

`+

/// and the cursor is not moved.

`

``

2193

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2194

`+

pub fn prev(&mut self) -> Option<&mut T> {

`

``

2195

`+

self.inner.prev().map(|(k, _)| k)

`

``

2196

`+

}

`

``

2197

+

``

2198

`+

/// Returns a reference to the next element without moving the cursor.

`

``

2199

`+

///

`

``

2200

`` +

/// If the cursor is at the end of the set then None is returned

``

``

2201

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2202

`+

pub fn peek_next(&mut self) -> Option<&mut T> {

`

``

2203

`+

self.inner.peek_next().map(|(k, _)| k)

`

``

2204

`+

}

`

``

2205

+

``

2206

`+

/// Returns a reference to the previous element without moving the cursor.

`

``

2207

`+

///

`

``

2208

`` +

/// If the cursor is at the start of the set then None is returned.

``

``

2209

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2210

`+

pub fn peek_prev(&mut self) -> Option<&mut T> {

`

``

2211

`+

self.inner.peek_prev().map(|(k, _)| k)

`

``

2212

`+

}

`

``

2213

+

``

2214

`+

/// Returns a read-only cursor pointing to the same location as the

`

``

2215

`` +

/// CursorMutKey.

``

``

2216

`+

///

`

``

2217

`` +

/// The lifetime of the returned Cursor is bound to that of the

``

``

2218

`` +

/// CursorMutKey, which means it cannot outlive the CursorMutKey and that the

``

``

2219

`` +

/// CursorMutKey is frozen for the lifetime of the Cursor.

``

``

2220

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2221

`+

pub fn as_cursor(&self) -> Cursor<'_, T> {

`

``

2222

`+

Cursor { inner: self.inner.as_cursor() }

`

``

2223

`+

}

`

2132

2224

`}

`

2133

2225

``

2134

2226

`impl<'a, T: Ord, A: Allocator + Clone> CursorMut<'a, T, A> {

`

`@@ -2217,6 +2309,92 @@ impl<'a, T: Ord, A: Allocator + Clone> CursorMut<'a, T, A> {

`

2217

2309

`}

`

2218

2310

`}

`

2219

2311

``

``

2312

`+

impl<'a, T: Ord, A: Allocator + Clone> CursorMutKey<'a, T, A> {

`

``

2313

`+

/// Inserts a new element into the set in the gap that the

`

``

2314

`+

/// cursor is currently pointing to.

`

``

2315

`+

///

`

``

2316

`+

/// After the insertion the cursor will be pointing at the gap before the

`

``

2317

`+

/// newly inserted element.

`

``

2318

`+

///

`

``

2319

`+

/// # Safety

`

``

2320

`+

///

`

``

2321

`` +

/// You must ensure that the BTreeSet invariants are maintained.

``

``

2322

`+

/// Specifically:

`

``

2323

`+

///

`

``

2324

`+

/// * The key of the newly inserted element must be unique in the tree.

`

``

2325

`+

/// * All elements in the tree must remain in sorted order.

`

``

2326

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2327

`+

pub unsafe fn insert_after_unchecked(&mut self, value: T) {

`

``

2328

`+

unsafe { self.inner.insert_after_unchecked(value, SetValZST) }

`

``

2329

`+

}

`

``

2330

+

``

2331

`+

/// Inserts a new element into the set in the gap that the

`

``

2332

`+

/// cursor is currently pointing to.

`

``

2333

`+

///

`

``

2334

`+

/// After the insertion the cursor will be pointing at the gap after the

`

``

2335

`+

/// newly inserted element.

`

``

2336

`+

///

`

``

2337

`+

/// # Safety

`

``

2338

`+

///

`

``

2339

`` +

/// You must ensure that the BTreeSet invariants are maintained.

``

``

2340

`+

/// Specifically:

`

``

2341

`+

///

`

``

2342

`+

/// * The newly inserted element must be unique in the tree.

`

``

2343

`+

/// * All elements in the tree must remain in sorted order.

`

``

2344

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2345

`+

pub unsafe fn insert_before_unchecked(&mut self, value: T) {

`

``

2346

`+

unsafe { self.inner.insert_before_unchecked(value, SetValZST) }

`

``

2347

`+

}

`

``

2348

+

``

2349

`+

/// Inserts a new element into the set in the gap that the

`

``

2350

`+

/// cursor is currently pointing to.

`

``

2351

`+

///

`

``

2352

`+

/// After the insertion the cursor will be pointing at the gap before the

`

``

2353

`+

/// newly inserted element.

`

``

2354

`+

///

`

``

2355

`+

/// If the inserted element is not greater than the element before the

`

``

2356

`+

/// cursor (if any), or if it not less than the element after the cursor (if

`

``

2357

`` +

/// any), then an [UnorderedKeyError] is returned since this would

``

``

2358

`` +

/// invalidate the [Ord] invariant between the elements of the set.

``

``

2359

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2360

`+

pub fn insert_after(&mut self, value: T) -> Result<(), UnorderedKeyError> {

`

``

2361

`+

self.inner.insert_after(value, SetValZST)

`

``

2362

`+

}

`

``

2363

+

``

2364

`+

/// Inserts a new element into the set in the gap that the

`

``

2365

`+

/// cursor is currently pointing to.

`

``

2366

`+

///

`

``

2367

`+

/// After the insertion the cursor will be pointing at the gap after the

`

``

2368

`+

/// newly inserted element.

`

``

2369

`+

///

`

``

2370

`+

/// If the inserted element is not greater than the element before the

`

``

2371

`+

/// cursor (if any), or if it not less than the element after the cursor (if

`

``

2372

`` +

/// any), then an [UnorderedKeyError] is returned since this would

``

``

2373

`` +

/// invalidate the [Ord] invariant between the elements of the set.

``

``

2374

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2375

`+

pub fn insert_before(&mut self, value: T) -> Result<(), UnorderedKeyError> {

`

``

2376

`+

self.inner.insert_before(value, SetValZST)

`

``

2377

`+

}

`

``

2378

+

``

2379

`` +

/// Removes the next element from the BTreeSet.

``

``

2380

`+

///

`

``

2381

`+

/// The element that was removed is returned. The cursor position is

`

``

2382

`+

/// unchanged (before the removed element).

`

``

2383

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2384

`+

pub fn remove_next(&mut self) -> Option {

`

``

2385

`+

self.inner.remove_next().map(|(k, _)| k)

`

``

2386

`+

}

`

``

2387

+

``

2388

`` +

/// Removes the precending element from the BTreeSet.

``

``

2389

`+

///

`

``

2390

`+

/// The element that was removed is returned. The cursor position is

`

``

2391

`+

/// unchanged (after the removed element).

`

``

2392

`+

#[unstable(feature = "btree_cursors", issue = "107540")]

`

``

2393

`+

pub fn remove_prev(&mut self) -> Option {

`

``

2394

`+

self.inner.remove_prev().map(|(k, _)| k)

`

``

2395

`+

}

`

``

2396

`+

}

`

``

2397

+

2220

2398

`#[unstable(feature = "btree_cursors", issue = "107540")]

`

2221

2399

`pub use super::map::UnorderedKeyError;

`

2222

2400

``