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 
ContainsSameRefs(const OBStereo::Refs & refs1,const OBStereo::Refs & refs2)28   bool OBStereo::ContainsSameRefs(const OBStereo::Refs &refs1, const OBStereo::Refs &refs2)
29   {
30     if (refs1.size() != refs2.size())
31       return false;
32 
33     unsigned int count = 0;
34     for (ConstRefIter i = refs1.begin(); i != refs1.end(); ++i)
35       for (ConstRefIter j = refs2.begin(); j != refs2.end(); ++j)
36         if (*i == *j) {
37           count++;
38           break;
39         }
40 
41     return (count == refs1.size());
42   }
43 
ContainsRef(const OBStereo::Refs & refs,unsigned long id)44   bool OBStereo::ContainsRef(const OBStereo::Refs &refs, unsigned long id)
45   {
46     for (ConstRefIter i = refs.begin(); i != refs.end(); ++i)
47       if (*i == id)
48         return true;
49 
50     return false;
51   }
52 
NumInversions(const OBStereo::Refs & refs)53   int OBStereo::NumInversions(const OBStereo::Refs &refs)
54   {
55     OBStereo::Refs invVec; // the inversion vector
56     OBStereo::ConstRefIter i, j;
57     for (i = refs.begin(); i != refs.end(); ++i) {
58       int e = 0; // ith element
59       // loop over elements to the right
60       for (j = i; j != refs.end(); ++j)
61         // increment e if element to the right is lower
62         if (*j < *i)
63           e++;
64 
65       invVec.push_back(e);
66     }
67 
68     int sum = 0;
69     for (OBStereo::RefIter k = invVec.begin(); k != invVec.end(); ++k)
70       sum += *k;
71 
72     return sum;
73   }
74 
Permutate(OBStereo::Refs & refs,unsigned int i,unsigned int j)75   void OBStereo::Permutate(OBStereo::Refs &refs, unsigned int i, unsigned int j)
76   {
77     if (i >= refs.size())
78       return;
79     if (j >= refs.size())
80       return;
81     unsigned long id = refs.at(i);
82     refs[i] = refs.at(j);
83     refs[j] = id;
84   }
85 
Permutated(const OBStereo::Refs & refs,unsigned int i,unsigned int j)86   OBStereo::Refs OBStereo::Permutated(const OBStereo::Refs &refs, unsigned int i, unsigned int j)
87   {
88     if (i >= refs.size())
89       return refs;
90     if (j >= refs.size())
91       return refs;
92     OBStereo::Refs result(refs);
93     result[i] = refs.at(j);
94     result[j] = refs.at(i);
95     return result;
96   }
97 
98 }
99 
100