1 /*
2  * Copyright (c) 2013 John May <jwmay@users.sf.net>
3  *
4  * Contact: cdk-devel@lists.sourceforge.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  * All we ask is that proper credit is given for our work, which includes
11  * - but is not limited to - adding the above copyright notice to the beginning
12  * of your source code files, and to any copyright notice that you may distribute
13  * with programs based on this work.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 U
23  */
24 
25 package org.openscience.cdk.hash.stereo;
26 
27 /**
28  * Calculate the permutation parity on a given array of current values.
29  *
30  * @author John May
31  * @cdk.module hash
32  * @see <a href="http://en.wikipedia.org/wiki/Parity_of_a_permutation">Parity of
33  *      a Permutation, Wikipedia</a>
34  * @cdk.githash
35  */
36 abstract class PermutationParity {
37 
38     /**
39      * Identity parity which always returns 1 (even). This is useful for
40      * configurations which do not require ordering, such as, double bonds with
41      * implicit hydrogens.
42      */
43     public static final PermutationParity IDENTITY = new PermutationParity() {
44 
45                                                        @Override
46                                                        public int parity(long[] current) {
47                                                            return 1;
48                                                        }
49                                                    };
50 
51     /**
52      * Calculate the permutation parity of a permutation on the current values.
53      * The inversion parity counts whether we need to do an odd or even number
54      * of swaps to put the values in sorted order. If the values contain
55      * duplicates then the parity is returned as 0.
56      *
57      * @param current current values of invariants
58      * @return -1, odd number of swaps, +1, even number of swaps, 0, contains
59      *         duplicates
60      */
parity(long[] current)61     abstract int parity(long[] current);
62 
63 }
64