/src/openbabel/src/stereo/stereo.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | stereo.cpp - OBStereo |
3 | | |
4 | | Copyright (C) 2009 by Tim Vandermeersch |
5 | | |
6 | | This file is part of the Open Babel project. |
7 | | For more information, see <http://openbabel.org/> |
8 | | |
9 | | This program is free software; you can redistribute it and/or modify |
10 | | it under the terms of the GNU General Public License as published by |
11 | | the Free Software Foundation; either version 2 of the License, or |
12 | | (at your option) any later version. |
13 | | |
14 | | This program is distributed in the hope that it will be useful, |
15 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | GNU General Public License for more details. |
18 | | |
19 | | You should have received a copy of the GNU General Public License |
20 | | along with this program; if not, write to the Free Software |
21 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | | 02110-1301, USA. |
23 | | **********************************************************************/ |
24 | | #include <openbabel/stereo/stereo.h> |
25 | | |
26 | | namespace OpenBabel { |
27 | | |
28 | | bool OBStereo::ContainsSameRefs(const OBStereo::Refs &refs1, const OBStereo::Refs &refs2) |
29 | 0 | { |
30 | 0 | if (refs1.size() != refs2.size()) |
31 | 0 | return false; |
32 | | |
33 | 0 | unsigned int count = 0; |
34 | 0 | for (ConstRefIter i = refs1.begin(); i != refs1.end(); ++i) |
35 | 0 | for (ConstRefIter j = refs2.begin(); j != refs2.end(); ++j) |
36 | 0 | if (*i == *j) { |
37 | 0 | count++; |
38 | 0 | break; |
39 | 0 | } |
40 | |
|
41 | 0 | return (count == refs1.size()); |
42 | 0 | } |
43 | | |
44 | | bool OBStereo::ContainsRef(const OBStereo::Refs &refs, unsigned long id) |
45 | 0 | { |
46 | 0 | for (ConstRefIter i = refs.begin(); i != refs.end(); ++i) |
47 | 0 | if (*i == id) |
48 | 0 | return true; |
49 | | |
50 | 0 | return false; |
51 | 0 | } |
52 | | |
53 | | int OBStereo::NumInversions(const OBStereo::Refs &refs) |
54 | 0 | { |
55 | 0 | OBStereo::Refs invVec; // the inversion vector |
56 | 0 | OBStereo::ConstRefIter i, j; |
57 | 0 | for (i = refs.begin(); i != refs.end(); ++i) { |
58 | 0 | int e = 0; // ith element |
59 | | // loop over elements to the right |
60 | 0 | for (j = i; j != refs.end(); ++j) |
61 | | // increment e if element to the right is lower |
62 | 0 | if (*j < *i) |
63 | 0 | e++; |
64 | |
|
65 | 0 | invVec.push_back(e); |
66 | 0 | } |
67 | |
|
68 | 0 | int sum = 0; |
69 | 0 | for (OBStereo::RefIter k = invVec.begin(); k != invVec.end(); ++k) |
70 | 0 | sum += *k; |
71 | |
|
72 | 0 | return sum; |
73 | 0 | } |
74 | | |
75 | | void OBStereo::Permutate(OBStereo::Refs &refs, unsigned int i, unsigned int j) |
76 | 0 | { |
77 | 0 | if (i >= refs.size()) |
78 | 0 | return; |
79 | 0 | if (j >= refs.size()) |
80 | 0 | return; |
81 | 0 | unsigned long id = refs.at(i); |
82 | 0 | refs[i] = refs.at(j); |
83 | 0 | refs[j] = id; |
84 | 0 | } |
85 | | |
86 | | OBStereo::Refs OBStereo::Permutated(const OBStereo::Refs &refs, unsigned int i, unsigned int j) |
87 | 0 | { |
88 | 0 | if (i >= refs.size()) |
89 | 0 | return refs; |
90 | 0 | if (j >= refs.size()) |
91 | 0 | return refs; |
92 | 0 | OBStereo::Refs result(refs); |
93 | 0 | result[i] = refs.at(j); |
94 | 0 | result[j] = refs.at(i); |
95 | 0 | return result; |
96 | 0 | } |
97 | | |
98 | | } |
99 | | |