chemical_elements 0.7.0

A library for representing chemical compositions and generating isotopic patterns
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use crate::abstract_composition::{
    ChemicalComposition as AbstractChemicalComposition, Iter as AbstractIter,
    IterMut as AbstractIterMut,
};
use crate::composition_list::ChemicalCompositionVec;
use crate::composition_map::ChemicalCompositionMap;
use crate::element_specification::ElementSpecification;

pub trait ChemicalCompositionLike<'inner, 'lifespan: 'inner> {
    /// Access a specific element's count, or `0` if that element is absent
    /// from the composition
    fn get(&self, elt_spec: &ElementSpecification<'lifespan>) -> i32;

    /// Set the count for a specific element. This will invalidate the mass cache.
    fn set(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32);

    /// Add some value to the count of the specified element. This will invalidate the
    /// mass cache.
    fn inc(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        let mut val = self.get(&elt_spec);
        val += count;
        self.set(elt_spec, val);
    }

    /*
    # Mass calculation Methods

    [`ChemicalCompositionLike`] has three methods for computing the monoisotopic
    mass of the composition it represents to handle mutability.
    */

    /**
    Get the mass of this chemical composition. If the mass cache
    has been populated, return that instead of repeating the calculation.
    */
    fn mass(&self) -> f64;

    /**
    Get the mass of this chemical composition, and cache it,
    or reuse the cached value. This requires mutability, so this method
    must be called explicitly.
    */
    fn fmass(&mut self) -> f64 {
        self.mass()
    }

    fn is_empty(&self) -> bool;

    fn len(&self) -> usize;

    fn iter(&self) -> AbstractIter<'_, 'lifespan>;

    fn iter_mut(&mut self) -> AbstractIterMut<'_, 'lifespan>;

    fn _mul_by(&mut self, scaler: i32) {
        for (_, v) in self.iter_mut() {
            *v *= scaler;
        }
    }
}

impl<'transient, 'lifespan: 'transient> ChemicalCompositionLike<'transient, 'lifespan>
    for ChemicalCompositionVec<'lifespan>
{
    fn get(&self, elt_spec: &ElementSpecification<'lifespan>) -> i32 {
        self.get(elt_spec)
    }

    fn set(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.set(elt_spec, count)
    }

    fn mass(&self) -> f64 {
        self.mass()
    }

    fn fmass(&mut self) -> f64 {
        self.fmass()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn inc(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.inc(elt_spec, count)
    }

    fn _mul_by(&mut self, scaler: i32) {
        (*self) *= scaler;
    }

    fn iter(&self) -> AbstractIter<'_, 'lifespan> {
        AbstractIter::Vec(self.iter())
    }

    fn iter_mut(&mut self) -> AbstractIterMut<'_, 'lifespan> {
        AbstractIterMut::Vec(self.iter_mut())
    }
}

impl<'transient, 'lifespan: 'transient> ChemicalCompositionLike<'transient, 'lifespan>
    for ChemicalCompositionMap<'lifespan>
{
    fn get(&self, elt_spec: &ElementSpecification<'lifespan>) -> i32 {
        self.get(elt_spec)
    }

    fn set(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.set(elt_spec, count)
    }

    fn mass(&self) -> f64 {
        self.mass()
    }

    fn fmass(&mut self) -> f64 {
        self.fmass()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn inc(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.inc(elt_spec, count)
    }

    fn _mul_by(&mut self, scaler: i32) {
        *self *= scaler;
    }

    fn iter(&self) -> AbstractIter<'_, 'lifespan> {
        AbstractIter::Map(self.iter())
    }

    fn iter_mut(&mut self) -> AbstractIterMut<'_, 'lifespan> {
        AbstractIterMut::Map(self.iter_mut())
    }
}

impl<'transient, 'lifespan: 'transient> ChemicalCompositionLike<'transient, 'lifespan>
    for AbstractChemicalComposition<'lifespan>
{
    fn get(&self, elt_spec: &ElementSpecification<'lifespan>) -> i32 {
        self.get(elt_spec)
    }

    fn set(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.set(elt_spec, count)
    }

    fn mass(&self) -> f64 {
        self.mass()
    }

    fn fmass(&mut self) -> f64 {
        self.fmass()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn inc(&mut self, elt_spec: ElementSpecification<'lifespan>, count: i32) {
        self.inc(elt_spec, count)
    }

    fn _mul_by(&mut self, scaler: i32) {
        match self {
            AbstractChemicalComposition::Vec(c) => {
                *c *= scaler;
            }
            AbstractChemicalComposition::Map(c) => {
                *c *= scaler;
            }
        }
    }

    fn iter(&self) -> AbstractIter<'_, 'lifespan> {
        self.iter()
    }

    fn iter_mut(&mut self) -> AbstractIterMut<'_, 'lifespan> {
        self.iter_mut()
    }
}

macro_rules! impl_from {
    ($frm:ty, $to:ty) => {
        impl<'lifespan> From<$frm> for $to {
            fn from(value: $frm) -> Self {
                let mut inst = Self::default();
                value.iter().for_each(|(k, v)| {
                    inst.set(*k, *v);
                });
                inst
            }
        }
    };
}

impl_from!(
    ChemicalCompositionMap<'lifespan>,
    ChemicalCompositionVec<'lifespan>
);
impl_from!(
    ChemicalCompositionVec<'lifespan>,
    ChemicalCompositionMap<'lifespan>
);
impl_from!(
    AbstractChemicalComposition<'lifespan>,
    ChemicalCompositionVec<'lifespan>
);
impl_from!(
    AbstractChemicalComposition<'lifespan>,
    ChemicalCompositionMap<'lifespan>
);
impl_from!(
    ChemicalCompositionVec<'lifespan>,
    AbstractChemicalComposition<'lifespan>
);
impl_from!(
    ChemicalCompositionMap<'lifespan>,
    AbstractChemicalComposition<'lifespan>
);

macro_rules! impl_arithmetic {
    ($tp:ty) => {
        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            Add<&'inner C> for &$tp
        {
            type Output = $tp;

            #[inline]
            fn add(self, other: &'inner C) -> Self::Output {
                let mut inst = self.clone();
                other.iter().for_each(|(k, v)| {
                    inst.inc(*k, *v);
                });
                return inst;
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            Sub<&'inner C> for &$tp
        {
            type Output = $tp;

            #[inline]
            fn sub(self, other: &'inner C) -> Self::Output {
                let mut inst = self.clone();
                other.iter().for_each(|(k, v)| {
                    inst.inc(*k, -*v);
                });
                return inst;
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            Add<&'inner C> for $tp
        {
            type Output = $tp;

            #[inline]
            fn add(self, other: &'inner C) -> Self::Output {
                let mut inst = self.clone();
                other.iter().for_each(|(k, v)| {
                    inst.inc(*k, *v);
                });
                return inst;
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            Sub<&'inner C> for $tp
        {
            type Output = $tp;

            #[inline]
            fn sub(self, other: &'inner C) -> Self::Output {
                let mut inst = self.clone();
                other.iter().for_each(|(k, v)| {
                    inst.inc(*k, -*v);
                });
                return inst;
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            AddAssign<&'inner C> for &mut $tp
        {
            #[inline]
            fn add_assign(&mut self, other: &'inner C) {
                other.iter().for_each(|(k, v)| {
                    self.inc(*k, *v);
                });
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            SubAssign<&'inner C> for &mut $tp
        {
            #[inline]
            fn sub_assign(&mut self, other: &'inner C) {
                other.iter().for_each(|(k, v)| {
                    self.inc(*k, -*v);
                });
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            AddAssign<&'inner C> for $tp
        {
            #[inline]
            fn add_assign(&mut self, other: &'inner C) {
                other.iter().for_each(|(k, v)| {
                    self.inc(*k, *v);
                });
            }
        }

        impl<'inner, 'lifespan: 'inner, C: ChemicalCompositionLike<'inner, 'lifespan>>
            SubAssign<&'inner C> for $tp
        {
            #[inline]
            fn sub_assign(&mut self, other: &'inner C) {
                other.iter().for_each(|(k, v)| {
                    self.inc(*k, -*v);
                });
            }
        }

        impl<'lifespan> Mul<i32> for &$tp {
            type Output = $tp;

            #[inline]
            fn mul(self, other: i32) -> Self::Output {
                let mut inst = self.clone();
                inst._mul_by(other);
                return inst;
            }
        }

        impl<'lifespan> Mul<i32> for $tp {
            type Output = $tp;

            #[inline]
            fn mul(self, other: i32) -> Self::Output {
                let mut inst = self.clone();
                inst._mul_by(other);
                return inst;
            }
        }

        impl<'lifespan> MulAssign<i32> for $tp {
            #[inline]
            fn mul_assign(&mut self, other: i32) {
                self._mul_by(other);
            }
        }

        impl<'lifespan> MulAssign<i32> for &mut $tp {
            #[inline]
            fn mul_assign(&mut self, other: i32) {
                self._mul_by(other);
            }
        }

        impl<'lifespan> Neg for $tp {
            type Output = $tp;

            #[inline]
            fn neg(mut self) -> Self::Output {
                self._mul_by(-1);
                self
            }
        }

        impl<'lifespan> Neg for &$tp {
            type Output = $tp;

            #[inline]
            fn neg(self) -> Self::Output {
                let mut dup = self.clone();
                dup._mul_by(-1);
                dup
            }
        }
    };
}

impl_arithmetic!(ChemicalCompositionMap<'lifespan>);
impl_arithmetic!(ChemicalCompositionVec<'lifespan>);
impl_arithmetic!(AbstractChemicalComposition<'lifespan>);

impl<'inner, 'lifespan: 'inner> IntoIterator for &'inner ChemicalCompositionMap<'lifespan> {
    type IntoIter = AbstractIter<'inner, 'lifespan>;
    type Item = <AbstractIter<'inner, 'lifespan> as Iterator>::Item;

    fn into_iter(self) -> Self::IntoIter {
        AbstractIter::Map(self.iter())
    }
}

impl<'inner, 'lifespan: 'inner> IntoIterator for &'inner ChemicalCompositionVec<'lifespan> {
    type IntoIter = AbstractIter<'inner, 'lifespan>;
    type Item = <AbstractIter<'inner, 'lifespan> as Iterator>::Item;

    fn into_iter(self) -> Self::IntoIter {
        AbstractIter::Vec(self.iter())
    }
}

impl<'inner, 'lifespan: 'inner> IntoIterator for &'inner AbstractChemicalComposition<'lifespan> {
    type IntoIter = AbstractIter<'inner, 'lifespan>;
    type Item = <AbstractIter<'inner, 'lifespan> as Iterator>::Item;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[allow(unused)]
pub trait ChemicalCompositionBehavior<'inner, 'lifespan: 'inner>:
    ChemicalCompositionLike<'inner, 'lifespan> + Default
where
    &'inner Self: IntoIterator + 'inner,
    &'inner Self: Add<&'inner Self>,
    &'inner Self: Sub<&'inner Self>,
    &'inner mut Self: AddAssign<&'inner Self>,
    &'inner mut Self: SubAssign<&'inner Self>,
    Self: AddAssign<&'inner Self>,
    Self: SubAssign<&'inner Self>,
    Self: MulAssign<i32>,
    &'inner Self: Mul<i32>,
{
}

impl<'inner, 'lifespan: 'inner> ChemicalCompositionBehavior<'inner, 'lifespan>
    for ChemicalCompositionMap<'lifespan>
{
}
impl<'inner, 'lifespan: 'inner> ChemicalCompositionBehavior<'inner, 'lifespan>
    for ChemicalCompositionVec<'lifespan>
{
}
impl<'inner, 'lifespan: 'inner> ChemicalCompositionBehavior<'inner, 'lifespan>
    for AbstractChemicalComposition<'lifespan>
{
}

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

    #[test]
    fn test_process() {
        let comp = AbstractChemicalComposition::parse("C6H12O6").unwrap();
        let mut parts = 0;
        for (_, v) in &comp {
            parts += *v;
        }
        assert_eq!(24, parts)
    }
}