1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! # swap3
//!
//! Provides utility functions for simultaneously swapping three values by rotating them
//! either left (`abc` → `bca`) or right (`abc` → `cab`). These functions can come in handy e.g.
//! when rotating elements of a binary tree in list representation.
//!
//! The provided functions work on arbitrary types and do *not* require the type to be [`Clone`], [`Copy`]
//! or [`Default`].
//!
//! ## Crate features
//!
//! * `unsafe` - The `unsafe` feature enables the use of (potentially faster) unsafe code.
//!              It is disabled by default; when disabled, `forbid(unsafe_code)` is implied.
//!
//! ## Examples
//!
//! For individual references, the [`swap3_bca`] (rotate left) and [`swap3_cab`] (rotate right)
//! functions are available:
//!
//! ```
//! let mut a = 10;
//! let mut b = 20;
//! let mut c = 30;
//!
//! swap3::swap3_bca(&mut a, &mut b, &mut c);
//! assert_eq!([a, b, c], [20, 30, 10]);
//! ```
//!
//! For slices, the [`swap3_bca_slice`] and [`swap3_cab_slice`] functions can be used:
//!
//! ```
//! let mut vec = vec![10, 20, 30, 40, 50, 60];
//! swap3::swap3_bca_slice(&mut vec, 0, 1, 4);
//! assert_eq!(vec, &[20, 50, 30, 40, 10, 60]);
//! ```
//!
//! ... or using the [`Swap3`] trait imported from the prelude:
//!
//! ```
//! use swap3::prelude::*;
//!
//! let mut vec = vec![10, 20, 30, 40, 50, 60];
//! vec.swap3_bca(0, 1, 4);
//! assert_eq!(vec, &[20, 50, 30, 40, 10, 60]);
//! ```

// SPDX-FileCopyrightText: 2023 Markus Mayer
// SPDX-License-Identifier: MIT

#![cfg_attr(feature = "unsafe", allow(unsafe_code))]
#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))]
// only enables the `doc_cfg` feature when
// the `docsrs` configuration attribute is defined
#![cfg_attr(docsrs, feature(doc_cfg))]

/// Provides simple access to commonly used traits.
pub mod prelude {
    pub use crate::Swap3;
}

/// Rotates three values to the left.
///
/// ## Arguments
///
/// * `a` - The first value, to be assigned with the value of `b`.
/// * `b` - The second value, to be assigned with the value of `c`.
/// * `c` - The third value, to be assigned with the value of `a`.
///
/// ## Example
///
/// ```
/// let mut a = 10;
/// let mut b = 20;
/// let mut c = 30;
/// swap3::swap3_bca(&mut a, &mut b, &mut c);
/// assert_eq!([a, b, c], [20, 30, 10]);
/// ```
pub fn swap3_bca<T>(a: &mut T, b: &mut T, c: &mut T) {
    std::mem::swap(a, b);
    std::mem::swap(b, c);
}

/// Rotates three values to the right.
///
/// ## Arguments
///
/// * `a` - The first value, to be assigned with the value of `c`.
/// * `b` - The second value, to be assigned with the value of `a`.
/// * `c` - The third value, to be assigned with the value of `b`.
///
/// ## Example
///
/// ```
/// let mut a = 10;
/// let mut b = 20;
/// let mut c = 30;
/// swap3::swap3_cab(&mut a, &mut b, &mut c);
/// assert_eq!([a, b, c], [30, 10, 20]);
/// ```
pub fn swap3_cab<T>(a: &mut T, b: &mut T, c: &mut T) {
    std::mem::swap(a, c);
    std::mem::swap(b, c);
}

/// Rotates three values to the left.
///
/// ## Arguments
///
/// * `data` - The slice whose elements to swap.
/// * `a` - The first index, to be assigned with the value of `data[b]`.
/// * `b` - The second index, to be assigned with the value of `data[c]`.
/// * `c` - The third index, to be assigned with the value of `data[a]`.
///
/// ## Example
///
/// ```
/// let mut vec = vec![50, 10, 90, 25, 30, 75];
/// swap3::swap3_bca_slice(&mut vec, 0, 1, 4);
/// assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
/// ```
#[inline(always)]
pub fn swap3_bca_slice<T>(data: &mut [T], a: usize, b: usize, c: usize) {
    #[cfg(feature = "unsafe")]
    slice::bca_unsafe(data, a, b, c);
    #[cfg(not(feature = "unsafe"))]
    slice::bca_safe(data, a, b, c);
}

/// Rotates three values to the right.
///
/// ## Arguments
///
/// * `data` - The slice whose elements to swap.
/// * `a` - The first index, to be assigned with the value of `data[c]`.
/// * `b` - The second index, to be assigned with the value of `data[a]`.
/// * `c` - The third index, to be assigned with the value of `data[b]`.
///
/// ## Example
///
/// ```
/// let mut vec = vec![50, 10, 90, 25, 30, 75];
/// swap3::swap3_bca_slice(&mut vec, 0, 1, 4);
/// assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
/// ```
#[inline(always)]
pub fn swap3_cab_slice<T>(data: &mut [T], a: usize, b: usize, c: usize) {
    #[cfg(feature = "unsafe")]
    slice::cab_unsafe(data, a, b, c);
    #[cfg(not(feature = "unsafe"))]
    slice::cab_safe(data, a, b, c);
}

/// Trait providing the [`Swap3::swap3_bca`] and [`Swap3::swap3_cab`] functions directly.
pub trait Swap3<I = usize> {
    /// Rotates three values to the left.
    ///
    /// ## Arguments
    ///
    /// * `a` - The first index, to be assigned with the value of `data[b]`.
    /// * `b` - The second index, to be assigned with the value of `data[c]`.
    /// * `c` - The third index, to be assigned with the value of `data[a]`.
    ///
    /// ## Example
    ///
    /// ```
    /// use swap3::prelude::*;
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// vec.swap3_bca(0, 1, 4);
    /// assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    /// ```
    fn swap3_bca(&mut self, a: I, b: I, c: I);

    /// Rotates three values to the right.
    ///
    /// ## Arguments
    ///
    /// * `a` - The first index, to be assigned with the value of `data[c]`.
    /// * `b` - The second index, to be assigned with the value of `data[a]`.
    /// * `c` - The third index, to be assigned with the value of `data[b]`.
    ///
    /// ## Example
    ///
    /// ```
    /// use swap3::prelude::*;
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// vec.swap3_cab(0, 1, 4);
    /// assert_eq!(vec, &[30, 50, 90, 25, 10, 75]);
    /// ```
    fn swap3_cab(&mut self, a: I, b: I, c: I);
}

impl<T> Swap3<usize> for [T] {
    fn swap3_bca(&mut self, a: usize, b: usize, c: usize) {
        swap3_bca_slice(self, a, b, c)
    }

    fn swap3_cab(&mut self, a: usize, b: usize, c: usize) {
        swap3_cab_slice(self, a, b, c)
    }
}

pub mod slice {
    /// Rotates three values to the left.
    ///
    /// ## Arguments
    ///
    /// * `data` - The slice whose elements to swap.
    /// * `a` - The first index, to be assigned with the value of `data[b]`.
    /// * `b` - The second index, to be assigned with the value of `data[c]`.
    /// * `c` - The third index, to be assigned with the value of `data[a]`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// swap3::slice::bca_safe(&mut vec, 0, 1, 4);
    /// assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    /// ```
    #[inline(always)]
    pub fn bca_safe<T>(data: &mut [T], a: usize, b: usize, c: usize) {
        data.swap(a, b);
        data.swap(b, c);
    }

    /// Rotates three values to the left.
    ///
    /// ## Arguments
    ///
    /// * `data` - The slice whose elements to swap.
    /// * `a` - The first index, to be assigned with the value of `data[b]`.
    /// * `b` - The second index, to be assigned with the value of `data[c]`.
    /// * `c` - The third index, to be assigned with the value of `data[a]`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// swap3::slice::bca_unsafe(&mut vec, 0, 1, 4);
    /// assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "unsafe")))]
    #[cfg(feature = "unsafe")]
    #[inline(always)]
    pub fn bca_unsafe<T>(data: &mut [T], a: usize, b: usize, c: usize) {
        use std::ptr;

        // SPDX-SnippetBegin
        // SDPX—SnippetName: Adjusted slice::swap() from the Rust core library.
        // SPDX-SnippetCopyrightText: The Rust Core Library authors
        // SPDX-SnippetContributor: Adjusted by Markus Mayer for three arguments.
        // SPDX-License-Identifier: MIT

        let pa = ptr::addr_of_mut!(data[a]);
        let pb = ptr::addr_of_mut!(data[b]);
        let pc = ptr::addr_of_mut!(data[c]);
        // SAFETY: `pa`, `pb` and `pc` have been created from safe mutable references and refer
        // to elements in the slice and therefore are guaranteed to be valid and aligned.
        // Note that accessing the elements behind `a`, `b` and `c` is checked and will
        // panic when out of bounds.
        unsafe {
            ptr::swap(pa, pb);
            ptr::swap(pb, pc);
            // ptr::swap_nonoverlapping(pa, pb, 1);
            // ptr::swap_nonoverlapping(pb, pc, 1);
        }

        // SPDX-SnippetEnd
    }

    /// Rotates three values to the right.
    ///
    /// ## Arguments
    ///
    /// * `data` - The slice whose elements to swap.
    /// * `a` - The first index, to be assigned with the value of `data[c]`.
    /// * `b` - The second index, to be assigned with the value of `data[a]`.
    /// * `c` - The third index, to be assigned with the value of `data[b]`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// swap3::slice::cab_safe(&mut vec, 0, 1, 4);
    /// assert_eq!(vec, &[30, 50, 90, 25, 10, 75]);
    /// ```
    #[inline(always)]
    pub fn cab_safe<T>(data: &mut [T], a: usize, b: usize, c: usize) {
        data.swap(a, c);
        data.swap(b, c);
    }

    /// Rotates three values to the left.
    ///
    /// ## Arguments
    ///
    /// * `data` - The slice whose elements to swap.
    /// * `a` - The first index, to be assigned with the value of `data[b]`.
    /// * `b` - The second index, to be assigned with the value of `data[c]`.
    /// * `c` - The third index, to be assigned with the value of `data[a]`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut vec = vec![50, 10, 90, 25, 30, 75];
    /// swap3::slice::cab_unsafe(&mut vec, 0, 1, 4);
    /// assert_eq!(vec, &[30, 50, 90, 25, 10, 75]);
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "unsafe")))]
    #[cfg(feature = "unsafe")]
    #[inline(always)]
    pub fn cab_unsafe<T>(data: &mut [T], a: usize, b: usize, c: usize) {
        use std::ptr;

        // SPDX-SnippetBegin
        // SDPX—SnippetName: Adjusted slice::swap() from the Rust core library.
        // SPDX-SnippetCopyrightText: The Rust Core Library authors
        // SPDX-SnippetContributor: Adjusted by Markus Mayer for three arguments.
        // SPDX-License-Identifier: MIT

        let pa = ptr::addr_of_mut!(data[a]);
        let pb = ptr::addr_of_mut!(data[b]);
        let pc = ptr::addr_of_mut!(data[c]);
        // SAFETY: `pa`, `pb` and `pc` have been created from safe mutable references and refer
        // to elements in the slice and therefore are guaranteed to be valid and aligned.
        // Note that accessing the elements behind `a`, `b` and `c` is checked and will
        // panic when out of bounds.
        unsafe {
            ptr::swap(pa, pc);
            ptr::swap(pb, pc);
            // ptr::swap_nonoverlapping(pa, pb, 1);
            // ptr::swap_nonoverlapping(pb, pc, 1);
        }

        // SPDX-SnippetEnd
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_swap3_bca() {
        let mut a = 65;
        let mut b = 66;
        let mut c = 67;
        swap3_bca(&mut a, &mut b, &mut c);
        assert_eq!([a, b, c], [66, 67, 65]);
    }

    #[test]
    fn test_swap3_cab() {
        let mut a = 65;
        let mut b = 66;
        let mut c = 67;
        swap3_cab(&mut a, &mut b, &mut c);
        assert_eq!([a, b, c], [67, 65, 66]);
    }

    #[test]
    fn test_swap3_bca_vec() {
        let mut vec = vec![50, 10, 90, 25, 30, 75];
        slice::bca_safe(&mut vec, 0, 1, 4);
        assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    }

    #[test]
    #[cfg(feature = "unsafe")]
    fn test_swap3_bca_vec_unsafe() {
        let mut vec = vec![50, 10, 90, 25, 30, 75];
        slice::bca_unsafe(&mut vec, 0, 1, 4);
        assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    }

    #[test]
    fn test_swap3_cab_vec() {
        let mut vec = vec![50, 10, 90, 25, 30, 75];
        slice::cab_safe(&mut vec, 0, 1, 4);
        assert_eq!(vec, &[30, 50, 90, 25, 10, 75]);
    }

    #[test]
    #[cfg(feature = "unsafe")]
    fn test_swap3_cab_vec_unsafe() {
        let mut vec = vec![50, 10, 90, 25, 30, 75];
        slice::cab_unsafe(&mut vec, 0, 1, 4);
        assert_eq!(vec, &[30, 50, 90, 25, 10, 75]);
    }

    #[test]
    fn test_vec_trait_bca() {
        let mut vec = vec![50, 10, 90, 25, 30, 75];
        vec.swap3_bca(0, 1, 4);
        assert_eq!(vec, &[10, 30, 90, 25, 50, 75]);
    }

    #[test]
    fn test_array_trait_cab() {
        let mut vec = [50, 10, 90, 25, 30, 75];
        vec.swap3_cab(0, 1, 4);
        assert_eq!(vec, [30, 50, 90, 25, 10, 75]);
    }
}