Coverage Report

Created: 2023-03-26 06:36

/src/INCHI-1-SRC/INCHI_BASE/src/ichiisot.c
Line
Count
Source
1
/*
2
 * International Chemical Identifier (InChI)
3
 * Version 1
4
 * Software version 1.06
5
 * December 15, 2020
6
 *
7
 * The InChI library and programs are free software developed under the
8
 * auspices of the International Union of Pure and Applied Chemistry (IUPAC).
9
 * Originally developed at NIST.
10
 * Modifications and additions by IUPAC and the InChI Trust.
11
 * Some portions of code were developed/changed by external contributors
12
 * (either contractor or volunteer) which are listed in the file
13
 * 'External-contributors' included in this distribution.
14
 *
15
 * IUPAC/InChI-Trust Licence No.1.0 for the
16
 * International Chemical Identifier (InChI)
17
 * Copyright (C) IUPAC and InChI Trust
18
 *
19
 * This library is free software; you can redistribute it and/or modify it
20
 * under the terms of the IUPAC/InChI Trust InChI Licence No.1.0,
21
 * or any later version.
22
 *
23
 * Please note that this library is distributed WITHOUT ANY WARRANTIES
24
 * whatsoever, whether expressed or implied.
25
 * See the IUPAC/InChI-Trust InChI Licence No.1.0 for more details.
26
 *
27
 * You should have received a copy of the IUPAC/InChI Trust InChI
28
 * Licence No. 1.0 with this library; if not, please e-mail:
29
 *
30
 * info@inchi-trust.org
31
 *
32
 */
33
34
35
#include "mode.h"
36
#include "ichicomn.h"
37
38
/**********************************************************************************/
39
AT_ISO_SORT_KEY make_iso_sort_key( int iso_atw_diff, int num_1H, int num_2H, int num_3H )
40
114k
{
41
114k
    AT_ISO_SORT_KEY iso_sort_key = 0, mult = 1;
42
43
114k
    iso_sort_key += mult * num_1H;
44
114k
    mult *= AT_ISO_SORT_KEY_MULT;
45
114k
    iso_sort_key += mult * num_2H;
46
114k
    mult *= AT_ISO_SORT_KEY_MULT;
47
114k
    iso_sort_key += mult * num_3H;
48
114k
    mult *= AT_ISO_SORT_KEY_MULT;
49
114k
    iso_sort_key += mult * iso_atw_diff;
50
51
114k
    return iso_sort_key;
52
114k
}
53
54
55
56
/****************************************************************************
57
 Set sp_ATOM isotopic sort keys
58
****************************************************************************/
59
int set_atom_iso_sort_keys( int num_at,
60
                            sp_ATOM *at,
61
                            T_GROUP_INFO* t_group_info,
62
                            int *bHasIsotopicInTautomerGroups )
63
110k
{
64
110k
    int             i, num_isotopic = 0, bMergedTgroup;
65
110k
    AT_ISO_SORT_KEY iso_sort_key;
66
110k
    T_GROUP        *t_group =
67
110k
        ( t_group_info &&
68
110k
         t_group_info->t_group &&
69
110k
         t_group_info->num_t_groups > 0 ) ? t_group_info->t_group : NULL;
70
71
110k
    if (bHasIsotopicInTautomerGroups)
72
110k
    {
73
110k
        *bHasIsotopicInTautomerGroups = 0;
74
110k
    }
75
222k
    for (i = 0; i < num_at; i++)
76
112k
    {
77
112k
        bMergedTgroup = ( t_group_info && t_group_info->nIsotopicEndpointAtomNumber && ( at[i].cFlags & AT_FLAG_ISO_H_POINT ) );
78
112k
        if (( !at[i].endpoint || !t_group ) && !bMergedTgroup)
79
112k
        {
80
112k
            iso_sort_key = make_iso_sort_key( at[i].iso_atw_diff, at[i].num_iso_H[0], at[i].num_iso_H[1], at[i].num_iso_H[2] );
81
112k
        }
82
8
        else
83
8
        {
84
             /*  H isotopes go to the tautomer part of the CT (name) */
85
             /*  if (at[i].endpoint && t_group) ... */
86
8
            iso_sort_key = make_iso_sort_key( at[i].iso_atw_diff, 0, 0, 0 );
87
8
            if (bHasIsotopicInTautomerGroups)
88
8
            {
89
8
                *bHasIsotopicInTautomerGroups += ( at[i].num_iso_H[0] || at[i].num_iso_H[1] || at[i].num_iso_H[2] || bMergedTgroup );
90
8
            }
91
8
        }
92
112k
        at[i].iso_sort_key = iso_sort_key;
93
112k
        num_isotopic += ( iso_sort_key != 0 );
94
112k
    }
95
96
110k
    return num_isotopic;
97
110k
}
98
99
100
#ifdef NEVER
101
102
103
/****************************************************************************/
104
int unpack_iso_sort_key( AT_ISO_SORT_KEY iso_sort_key,
105
                         S_CHAR *num_1H,
106
                         S_CHAR *num_2H,
107
                         S_CHAR *num_3H,
108
                         S_CHAR *iso_atw_diff )
109
{
110
    int is_negative;
111
    AT_ISO_SORT_KEY HOnlyAtwPart;
112
    static const AT_ISO_SORT_KEY MultAtwDiff = AT_ISO_SORT_KEY_MULT*AT_ISO_SORT_KEY_MULT*AT_ISO_SORT_KEY_MULT;
113
    if (!iso_sort_key)
114
    {
115
        *num_1H = *num_2H = *num_3H = *iso_atw_diff = 0;
116
        return 0;
117
    }
118
    else
119
    {
120
        if (iso_sort_key < 0)
121
        {
122
            is_negative = 1;
123
            iso_sort_key = -iso_sort_key;
124
            HOnlyAtwPart = MultAtwDiff - iso_sort_key % MultAtwDiff;
125
            iso_sort_key += HOnlyAtwPart;
126
        }
127
        else
128
        {
129
            is_negative = 0;
130
            HOnlyAtwPart = iso_sort_key % MultAtwDiff;
131
            iso_sort_key -= HOnlyAtwPart;
132
        }
133
    }
134
135
    iso_sort_key /= MultAtwDiff;
136
137
    *num_1H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
138
    HOnlyAtwPart /= AT_ISO_SORT_KEY_MULT;
139
    *num_2H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
140
    HOnlyAtwPart /= AT_ISO_SORT_KEY_MULT;
141
    *num_3H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
142
143
    *iso_atw_diff = (S_CHAR) ( is_negative ? -iso_sort_key : iso_sort_key );
144
145
    return 1;
146
}
147
#endif