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
/*! Assorted helpers */
use std::rc::Rc;

use crate::float_ord::FloatOrd;

use std::borrow::Borrow;
use std::cmp::{Ordering, PartialOrd};
use std::hash::{ Hash, Hasher };
use std::ops::Mul;

pub mod c {
    use super::*;
    
    use std::cell::RefCell;
    use std::ffi::{ CStr, CString };
    use std::os::raw::c_char;
    use std::rc::Rc;
    use std::str::Utf8Error;
    use std::sync::{Arc, Mutex};

    // traits
    
    use std::borrow::ToOwned;
    
    // The lifetime on input limits the existence of the result
    pub fn as_str(s: &*const c_char) -> Result<Option<&str>, Utf8Error> {
        if s.is_null() {
            Ok(None)
        } else {
            unsafe {CStr::from_ptr(*s)}
                .to_str()
                .map(Some)
        }
    }

    pub fn as_cstr(s: &*const c_char) -> Option<&CStr> {
        if s.is_null() {
            None
        } else {
            Some(unsafe {CStr::from_ptr(*s)})
        }
    }

    pub fn into_cstring(s: *const c_char) -> Result<Option<CString>, std::ffi::NulError> {
        if s.is_null() {
            Ok(None)
        } else {
            CString::new(
                unsafe {CStr::from_ptr(s)}.to_bytes()
            ).map(Some)
        }
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
        use std::ptr;
        
        #[test]
        fn test_null_cstring() {
            assert_eq!(into_cstring(ptr::null()), Ok(None))
        }
        
        #[test]
        fn test_null_str() {
            assert_eq!(as_str(&ptr::null()), Ok(None))
        }
    }
    
    /// Marker trait for values that can be transferred to/received from C.
    /// They must be either *const or *mut or repr(transparent).
    pub trait COpaquePtr {}

    /// Wraps structures to pass them safely to/from C
    /// Since C doesn't respect borrowing rules,
    /// RefCell will enforce them dynamically (only 1 writer/many readers)
    /// Rc is implied and will ensure timely dropping
    #[repr(transparent)]
    pub struct Wrapped<T>(*const RefCell<T>);

    // It would be nice to implement `Borrow`
    // directly on the raw pointer to avoid one conversion call,
    // but the `borrow()` call needs to extract a `Rc`,
    // and at the same time return a reference to it (`std::cell::Ref`)
    // to take advantage of `Rc<RefCell>::borrow()` runtime checks.
    // Unfortunately, that needs a `Ref` struct with self-referential fields,
    // which is a bit too complex for now.

    impl<T> Wrapped<T> {
        pub fn new(value: T) -> Wrapped<T> {
            Wrapped::wrap(Rc::new(RefCell::new(value)))
        }
        pub fn wrap(state: Rc<RefCell<T>>) -> Wrapped<T> {
            Wrapped(Rc::into_raw(state))
        }
        /// Extracts the reference to the data.
        /// It may cause problems if attempted in more than one place
        pub unsafe fn unwrap(self) -> Rc<RefCell<T>> {
            Rc::from_raw(self.0)
        }
        
        /// Creates a new Rc reference to the same data.
        /// Use for accessing the underlying data as a reference.
        pub fn clone_ref(&self) -> Rc<RefCell<T>> {
            // A bit dangerous: the Rc may be in use elsewhere
            let used_rc = unsafe { Rc::from_raw(self.0) };
            let rc = used_rc.clone();
            Rc::into_raw(used_rc); // prevent dropping the original reference
            rc
        }
    }
    
    impl<T> Clone for Wrapped<T> {
        fn clone(&self) -> Wrapped<T> {
            Wrapped::wrap(self.clone_ref())
        }
    }
    
    /// ToOwned won't work here
    /// because it's really difficult to implement Borrow on Wrapped<T>
    /// with the Rc<RefCell<>> chain on the way to the data
    impl<T: Clone> CloneOwned for Wrapped<T> {
        type Owned = T;

        fn clone_owned(&self) -> T {
            let rc = self.clone_ref();
            let r = RefCell::borrow(&rc);
            r.to_owned()
        }
    }

    impl<T> COpaquePtr for Wrapped<T> {}
    
    /// Similar to Wrapped, except thread-safe.
    #[repr(transparent)]
    pub struct ArcWrapped<T>(*const Mutex<T>);
    
    impl<T> ArcWrapped<T> {
        pub fn new(value: T) -> Self {
            Self::wrap(Arc::new(Mutex::new(value)))
        }
        pub fn wrap(state: Arc<Mutex<T>>) -> Self {
            Self(Arc::into_raw(state))
        }
        /// Extracts the reference to the data.
        /// It may cause problems if attempted in more than one place
        pub unsafe fn unwrap(self) -> Arc<Mutex<T>> {
            Arc::from_raw(self.0)
        }
        
        /// Creates a new Rc reference to the same data.
        /// Use for accessing the underlying data as a reference.
        pub fn clone_ref(&self) -> Arc<Mutex<T>> {
            // A bit dangerous: the Rc may be in use elsewhere
            let used_rc = unsafe { Arc::from_raw(self.0) };
            let rc = used_rc.clone();
            let _ = Arc::into_raw(used_rc); // prevent dropping the original reference
            rc
        }
    }
    
    impl<T> Clone for ArcWrapped<T> {
        fn clone(&self) -> Self {
            Self::wrap(self.clone_ref())
        }
    }
    
    /// ToOwned won't work here
    impl<T: Clone> CloneOwned for ArcWrapped<T> {
        type Owned = T;

        fn clone_owned(&self) -> T {
            let rc = self.clone_ref();
            // FIXME: this panic here is inelegant.
            // It will only happen in case of crashes elsewhere, but still.
            let r = rc.lock().unwrap();
            r.to_owned()
        }
    }
}

/// Clones the underlying data structure, like ToOwned.
pub trait CloneOwned {
    type Owned;
    fn clone_owned(&self) -> Self::Owned;
}

pub fn find_max_double<T, I, F>(iterator: I, get: F)
    -> f64
    where I: Iterator<Item=T>,
        F: Fn(&T) -> f64
{
    iterator.map(|value| FloatOrd(get(&value)))
        .max().unwrap_or(FloatOrd(0f64))
        .0
}

pub trait DivCeil<Rhs = Self> {
    type Output;
    fn div_ceil(self, rhs: Rhs) -> Self::Output;
}

/// Newer Rust introduces this natively,
/// but we don't always have newer Rust.
impl DivCeil for i32 {
    type Output = Self;
    fn div_ceil(self, other: i32) -> Self::Output {
        let d = self / other;
        let m = self % other;
        if m == 0 { d } else { d + 1}
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Rational<T> {
    pub numerator: T,
    pub denominator: u32,
}

impl<U, T: DivCeil<i32, Output=U>> Rational<T> {
    pub fn ceil(self) -> U {
        self.numerator.div_ceil(self.denominator as i32)
    }
}

impl<T: Mul<i32, Output=T>> Mul<i32> for Rational<T> {
    type Output = Self;
    fn mul(self, m: i32) -> Self {
        Self {
            numerator: self.numerator * m,
            denominator: self.denominator,
        }
    }
}

impl<U, T: Mul<U, Output=T>> Mul<Rational<U>> for Rational<T> {
    type Output = Self;
    fn mul(self, m: Rational<U>) -> Self {
        Self {
            numerator: self.numerator * m.numerator,
            denominator: self.denominator * m.denominator,
        }
    }
}

impl PartialEq for Rational<i32> {
    fn eq(&self, other: &Self) -> bool {
        (self.denominator as i64).saturating_mul(other.numerator as i64)
            == (other.denominator as i64).saturating_mul(self.numerator as i64)
    }
}

impl Eq for Rational<i32> {}

impl Ord for Rational<i32> {
    fn cmp(&self, other: &Self) -> Ordering {
        // Using 64-bit values to make overflows unlikely.
        // If i32_max * u32_max can exceed i64_max,
        // then this is actually PartialOrd.
        // Saturating mul used just to avoid propagating mistakes.
        (other.denominator as i64).saturating_mul(self.numerator as i64)
            .cmp(
                &(self.denominator as i64).saturating_mul(other.numerator as i64)
             )
    }
}

impl PartialOrd for Rational<i32> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Compares pointers but not internal values of Rc
pub struct Pointer<T>(pub Rc<T>);

impl<T> Pointer<T> {
    pub fn new(value: T) -> Self {
        Pointer(Rc::new(value))
    }
}

impl<T> Hash for Pointer<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        (&*self.0 as *const T).hash(state);
    }
}

impl<T> PartialEq for Pointer<T> {
    fn eq(&self, other: &Pointer<T>) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl<T> Eq for Pointer<T> {}

impl<T> Clone for Pointer<T> {
    fn clone(&self) -> Self {
        Pointer(self.0.clone())
    }
}

impl<T> Borrow<Rc<T>> for Pointer<T> {
    fn borrow(&self) -> &Rc<T> {
        &self.0
    }
}

pub trait WarningHandler {
    /// Handle a warning
    fn handle(&mut self, warning: &str);
}

/// Removes the first matcing item
pub fn vec_remove<T, F: FnMut(&T) -> bool>(v: &mut Vec<T>, pred: F) -> Option<T> {
    let idx = v.iter().position(pred);
    idx.map(|idx| v.remove(idx))
}

/// Repeats all the items of the iterator forever,
/// but returns the cycle number alongside.
/// Inefficient due to all the vectors, but doesn't have to be fast.
pub fn cycle_count<T, I: Clone + Iterator<Item=T>>(iter: I)
    -> impl Iterator<Item=(T, usize)>
{
    let numbered_copies = vec![iter].into_iter()
        .cycle()
        .enumerate();
    numbered_copies.flat_map(|(idx, cycle)|
        // Pair each element from the cycle with a copy of the index.
        cycle.zip(
            vec![idx].into_iter().cycle() // Repeat the index forever.
        )
    )
}

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

    use std::collections::HashSet;

    #[test]
    fn check_set() {
        let mut s = HashSet::new();
        let first = Rc::new(1u32);
        s.insert(Pointer(first.clone()));
        assert_eq!(s.insert(Pointer(Rc::new(2u32))), true);
        assert_eq!(s.remove(&Pointer(first)), true);
    }

    #[test]
    fn check_count() {
        assert_eq!(
            cycle_count(5..8).take(7).collect::<Vec<_>>(),
            vec![(5, 0), (6, 0), (7, 0), (5, 1), (6, 1), (7, 1), (5, 2)]
        );
    }
    
    #[test]
    fn check_rational_cmp() {
        assert_eq!(
            Rational { numerator: 1, denominator: 1 },
            Rational { numerator: 1, denominator: 1 },
        );
        assert_eq!(
            Rational { numerator: 1, denominator: 1 },
            Rational { numerator: 2, denominator: 2 },
        );
        assert!(
            Rational { numerator: 1, denominator: 1 }
            < Rational { numerator: 2, denominator: 1 }
        );
    }
}