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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C)-2017 2015 Guillaume Fraux
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
use std::ops::{Drop, Index};
use std::u64;
use std::iter::IntoIterator;
use std::slice::Iter;

use chemfiles_sys::*;
use errors::{check, Error};
use strings;
use frame::Frame;
use Result;

#[derive(Debug, Clone, PartialEq, Eq)]
/// A `Match` is a set of atomic indexes matching a given selection. It can
/// mostly be used like a `&[u64]`.
pub struct Match(chfl_match);

#[allow(len_without_is_empty)]
impl Match {
    fn zero() -> Match {
        Match(chfl_match {
            size: 0,
            atoms: [0; 4],
        })
    }

    /// Get the length of the Match.
    ///
    /// # Example
    ///
    /// ```
    /// # use chemfiles::Match;
    /// let atomic_match = Match::new(&[3, 4, 5]);
    /// assert_eq!(atomic_match.len(), 3);
    /// ```
    #[allow(cast_possible_truncation)]
    pub fn len(&self) -> usize {
        self.0.size as usize
    }

    /// Create a new match containing the atoms in the `atoms` slice.
    ///
    /// # Panics
    ///
    /// If the slice contains more than 4 elements, which is the maximal size
    /// of a match.
    ///
    /// # Example
    ///
    /// ```
    /// # use chemfiles::Match;
    /// let atomic_match = Match::new(&[3, 4, 5]);
    /// assert_eq!(atomic_match.len(), 3);
    /// assert_eq!(atomic_match[0], 3);
    /// assert_eq!(atomic_match[1], 4);
    /// assert_eq!(atomic_match[2], 5);
    /// ```
    pub fn new(atoms: &[u64]) -> Match {
        assert!(atoms.len() <= 4);
        let size = atoms.len();
        let mut matches = [u64::MAX; 4];
        for (i, atom) in atoms.iter().enumerate() {
            matches[i] = *atom;
        }
        Match(chfl_match {
            size: size as u64,
            atoms: matches,
        })
    }

    /// Iterate over the atomic indexes in the match.
    ///
    /// # Example
    ///
    /// ```
    /// # use chemfiles::Match;
    /// let atomic_match = Match::new(&[3, 4, 5]);
    /// let mut iter = atomic_match.iter();
    ///
    /// assert_eq!(iter.next(), Some(&3));
    /// assert_eq!(iter.next(), Some(&4));
    /// assert_eq!(iter.next(), Some(&5));
    /// assert_eq!(iter.next(), None);
    /// ```
    pub fn iter(&self) -> Iter<u64> {
        self.0.atoms[..self.len()].iter()
    }
}

impl Index<usize> for Match {
    type Output = u64;
    fn index(&self, i: usize) -> &u64 {
        assert!(i < self.len());
        &self.0.atoms[i]
    }
}

impl<'a> IntoIterator for &'a Match {
    type Item = &'a u64;
    type IntoIter = Iter<'a, u64>;
    fn into_iter(self) -> Iter<'a, u64> {
        self.0.atoms[..self.len()].into_iter()
    }
}

/// A `Selection` allow to select atoms in a `Frame`, from a selection
/// language. The selection language is built by combining basic operations.
/// Each basic operation follows the `<selector>[(<variable>)] <operator>
/// <value>` structure, where `<operator>` is a comparison operator in
/// `== != < <= > >=`.
pub struct Selection {
    handle: *const CHFL_SELECTION,
}

impl Clone for Selection {
    fn clone(&self) -> Selection {
        unsafe {
            let new_handle = chfl_selection_copy(self.as_ptr());
            Selection::from_ptr(new_handle).expect("Out of memory when copying a Selection")
        }
    }
}

impl Drop for Selection {
    fn drop(&mut self) {
        unsafe {
            let status = chfl_selection_free(self.as_mut_ptr());
            debug_assert_eq!(status, chfl_status::CHFL_SUCCESS);
        }
    }
}

impl Selection {
    /// Create a `Selection` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the pointer,
    /// except for it being non-null.
    #[inline]
    #[doc(hidden)]
    pub unsafe fn from_ptr(ptr: *const CHFL_SELECTION) -> Result<Selection> {
        if ptr.is_null() {
            Err(Error::null_ptr())
        } else {
            Ok(Selection { handle: ptr })
        }
    }

    /// Get the underlying C pointer as a const pointer.
    #[inline]
    #[doc(hidden)]
    pub fn as_ptr(&self) -> *const CHFL_SELECTION {
        self.handle
    }

    /// Get the underlying C pointer as a mutable pointer.
    #[inline]
    #[doc(hidden)]
    pub fn as_mut_ptr(&mut self) -> *mut CHFL_SELECTION {
        self.handle as *mut CHFL_SELECTION
    }

    /// Create a new selection from the given selection string.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Selection;
    /// let selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
    /// ```
    pub fn new<'a, S: Into<&'a str>>(selection: S) -> Result<Selection> {
        let buffer = strings::to_c(selection.into());
        unsafe {
            let handle = chfl_selection(buffer.as_ptr());
            Selection::from_ptr(handle)
        }
    }

    /// Get the size of the selection, i.e. the number of atoms we are selecting
    /// together.
    ///
    /// This value is 1 for the 'atom' context, 2 for the 'pair' and 'bond'
    /// context, 3 for the 'three' and 'angles' contextes and 4 for the 'four'
    /// and 'dihedral' contextes.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Selection;
    /// let selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
    /// assert_eq!(selection.size(), Ok(2));
    /// ```
    pub fn size(&self) -> Result<u64> {
        let mut size = 0;
        unsafe {
            try!(check(chfl_selection_size(self.as_ptr(), &mut size)));
        }
        return Ok(size);
    }

    /// Get the selection string used to create this selection.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Selection;
    /// let selection = Selection::new("name H").unwrap();
    /// assert_eq!(selection.string(), Ok(String::from("name H")));
    /// ```
    pub fn string(&self) -> Result<String> {
        let get_string = |ptr, len| unsafe { chfl_selection_string(self.as_ptr(), ptr, len) };
        let selection = try!(strings::call_autogrow_buffer(1024, get_string));
        return Ok(strings::from_c(selection.as_ptr()));
    }

    /// Evaluate a selection for a given frame, and return the corresponding
    /// matches.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{Selection, Frame, Atom};
    /// let mut frame = Frame::new().unwrap();
    /// frame.add_atom(&Atom::new("H").unwrap(), [1.0, 0.0, 0.0], None).unwrap();
    /// frame.add_atom(&Atom::new("O").unwrap(), [0.0, 0.0, 0.0], None).unwrap();
    /// frame.add_atom(&Atom::new("H").unwrap(), [-1.0, 0.0, 0.0], None).unwrap();
    ///
    /// let mut selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
    /// let matches = selection.evaluate(&frame).unwrap();
    ///
    /// assert_eq!(matches.len(), 2);
    ///
    /// assert_eq!(matches[0].len(), 2);
    /// assert_eq!(matches[0][0], 0);
    /// assert_eq!(matches[0][1], 1);
    ///
    /// assert_eq!(matches[1].len(), 2);
    /// assert_eq!(matches[1][0], 2);
    /// assert_eq!(matches[1][1], 1);
    /// ```
    pub fn evaluate(&mut self, frame: &Frame) -> Result<Vec<Match>> {
        let mut matches_count = 0;
        unsafe {
            try!(check(
                chfl_selection_evaluate(self.as_mut_ptr(), frame.as_ptr(), &mut matches_count)
            ));
        }

        let mut matches = vec![Match::zero(); matches_count as usize];
        unsafe {
            try!(check(chfl_selection_matches(
                self.handle,
                matches.as_mut_ptr() as *mut _,
                matches_count
            )));
        }
        return Ok(matches);
    }

    /// Evaluates a selection of size 1 on a given `frame`. This function
    /// returns the list of atomic indexes in the frame matching this selection.
    ///
    /// # Panics
    ///
    /// If the selection size is not 1.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{Selection, Frame, Atom};
    /// let mut frame = Frame::new().unwrap();
    /// frame.add_atom(&Atom::new("H").unwrap(), [1.0, 0.0, 0.0], None).unwrap();
    /// frame.add_atom(&Atom::new("O").unwrap(), [0.0, 0.0, 0.0], None).unwrap();
    /// frame.add_atom(&Atom::new("H").unwrap(), [-1.0, 0.0, 0.0], None).unwrap();
    ///
    /// let mut selection = Selection::new("name H").unwrap();
    /// let matches = selection.list(&frame).unwrap();
    ///
    /// assert_eq!(matches.len(), 2);
    /// assert_eq!(matches[0], 0);
    /// assert_eq!(matches[1], 2);
    /// ```
    pub fn list(&mut self, frame: &Frame) -> Result<Vec<u64>> {
        let matches = try!(self.evaluate(frame));
        let mut list = vec![0; matches.len()];
        for (i, m) in matches.iter().enumerate() {
            list[i] = m[0];
        }
        Ok(list)
    }
}

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

    #[test]
    fn clone() {
        let selection = Selection::new("name H").unwrap();

        let copy = selection.clone();
        assert_eq!(selection.size(), Ok(1));
        assert_eq!(copy.size(), Ok(1));
    }

    fn testing_frame() -> Frame {
        let mut topology = Topology::new().unwrap();

        topology.add_atom(&Atom::new("H").unwrap()).unwrap();
        topology.add_atom(&Atom::new("O").unwrap()).unwrap();
        topology.add_atom(&Atom::new("O").unwrap()).unwrap();
        topology.add_atom(&Atom::new("H").unwrap()).unwrap();

        topology.add_bond(0, 1).unwrap();
        topology.add_bond(1, 2).unwrap();
        topology.add_bond(2, 3).unwrap();

        let mut frame = Frame::new().unwrap();
        frame.resize(4).unwrap();
        frame.set_topology(&topology).unwrap();
        return frame;
    }

    mod matches {
        use super::*;

        #[test]
        fn size_of() {
            assert_eq!(::std::mem::size_of::<chfl_match>(), ::std::mem::size_of::<Match>())
        }

        #[test]
        fn index() {
            let m = Match::new(&[1, 2, 3, 4]);
            assert_eq!(m[0], 1);
            assert_eq!(m[1], 2);
            assert_eq!(m[2], 3);
            assert_eq!(m[3], 4);

            let m = Match::new(&[1, 2]);
            assert_eq!(m[0], 1);
            assert_eq!(m[1], 2);
        }

        #[test]
        fn iter() {
            let match_ = Match::new(&[1, 2, 3, 4]);
            assert_eq!(match_.iter().cloned().collect::<Vec<u64>>(), vec![1, 2, 3, 4]);

            let v = vec![1, 2, 3, 4];
            let mut i = 0;
            for &m in &match_ {
                assert_eq!(v[i], m);
                i += 1;
            }
        }

        #[test]
        #[should_panic]
        fn out_of_bound() {
            let m = Match::new(&[1, 2]);
            let _ = m[2];
        }

        #[test]
        #[should_panic]
        fn too_big() {
            let _ = Match::new(&[1, 2, 3, 5, 4]);
        }
    }

    #[test]
    fn size() {
        let selection = Selection::new("name H").unwrap();
        assert_eq!(selection.size(), Ok(1));

        let selection = Selection::new("angles: name(#1) H").unwrap();
        assert_eq!(selection.size(), Ok(3));

        let selection = Selection::new("four: name(#1) H").unwrap();
        assert_eq!(selection.size(), Ok(4));
    }

    #[test]
    fn string() {
        let selection = Selection::new("name H").unwrap();
        assert_eq!(selection.string().unwrap(), "name H");

        let selection = Selection::new("angles: name(#1) H").unwrap();
        assert_eq!(selection.string().unwrap(), "angles: name(#1) H");
    }

    #[test]
    fn evaluate() {
        let frame = testing_frame();

        let mut selection = Selection::new("name H").unwrap();
        let res = selection.evaluate(&frame).unwrap();
        assert_eq!(res, &[Match::new(&[0]), Match::new(&[3])]);

        let mut selection = Selection::new("angles: all").unwrap();
        let res = selection.evaluate(&frame).unwrap();
        for m in &[Match::new(&[0, 1, 2]), Match::new(&[1, 2, 3])] {
            assert!(res.iter().find(|&r| r == m).is_some())
        }
    }

    #[test]
    fn list() {
        let frame = testing_frame();

        let mut selection = Selection::new("name H").unwrap();
        let res = selection.list(&frame).unwrap();
        assert_eq!(res, vec![0, 3]);
    }
}