Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : :
7 : : /* #define CHECK_WIN32_VC_HEAP */
8 : :
9 : : #include <stdlib.h>
10 : : #include <string.h>
11 : : #include <limits.h>
12 : : #include <time.h>
13 : :
14 : :
15 : : #include "mode.h"
16 : : #include "ichicano.h"
17 : :
18 : : #include "ichicomn.h"
19 : : #include "ichitime.h"
20 : :
21 : : #include "bcf_s.h"
22 : :
23 : : #define MAX_CELLS 32766
24 : : #define MAX_NODES 32766
25 : : #define MAX_SET_SIZE 32766 /*16384*/
26 : : #define NORMALLY_ALLOWED_MAX_SET_SIZE 2048
27 : : #define INITIAL_SET_SIZE 16
28 : : #define MAX_LAYERS 100
29 : :
30 : : #define SEPARATE_CANON_CALLS 0
31 : :
32 : : #define INCHI_CANON_INFINITY 0x7FFF
33 : :
34 : : #define INCHI_CANON_MIN
35 : :
36 : : #define EMPTY_CT 0
37 : : #define EMPTY_H_NUMBER (INCHI_CANON_INFINITY-1)
38 : : #define BASE_H_NUMBER ((INCHI_CANON_INFINITY-1)/2)
39 : : #define EMPTY_ISO_SORT_KEY LONG_MAX
40 : :
41 : :
42 : : /*
43 : : #define INCHI_CANON_USE_HASH
44 : : */ /* djb-rwth: constant has not been defined? */
45 : : #ifdef INCHI_CANON_USE_HASH
46 : : typedef unsigned long U_INT_32;
47 : : typedef unsigned char U_INT_08;
48 : : typedef U_INT_32 CtHash;
49 : : CtHash hash_mark_bit;
50 : : #endif
51 : :
52 : : /* -- moved to ichi_bns.h --
53 : : typedef U_SHORT bitWord;
54 : : #define BIT_WORD_MASK ((bitWord)~0)
55 : : */
56 : :
57 : : /*bitWord mark_bit; */ /* highest bit in AT_NUMB */
58 : : /*bitWord mask_bit; */ /* ~mark_bit */
59 : :
60 : : static AT_NUMB rank_mark_bit;
61 : : static AT_NUMB rank_mask_bit;
62 : :
63 : :
64 : : typedef AT_NUMB Node;
65 : : typedef NEIGH_LIST Graph;
66 : :
67 : : /*
68 : : typedef struct tagGraph
69 : : {
70 : : int dummy;
71 : : }
72 : : Graph;
73 : : */
74 : :
75 : : typedef struct tagUnorderedPartition
76 : : {
77 : : /* AT_NUMB *next; */ /* links */
78 : : AT_NUMB *equ2; /* mcr */
79 : : } UnorderedPartition;
80 : :
81 : : typedef struct tagCell
82 : : {
83 : : int first; /* index of the first cell element in Partition::AtNumber[] */
84 : : int next; /* next after the last index */
85 : : int prev; /* position of the previously returned cell element */
86 : : } Cell;
87 : :
88 : : #ifdef NEVER /* moved to ichi_bns.h */
89 : : typedef struct tagNodeSet {
90 : : bitWord **bitword;
91 : : int num_set; /* number of sets */
92 : : int len_set; /* number of bitWords in each set */
93 : : } NodeSet;
94 : : #endif
95 : :
96 : : typedef struct tagTransposition
97 : : {
98 : : AT_NUMB *nAtNumb;
99 : : } Transposition;
100 : :
101 : :
102 : : typedef struct tagCTable
103 : : {
104 : : AT_RANK *Ctbl; /* connection table */
105 : : /* Format-atoms: atom_rank[k] neigh_rank[k][1]...neigh_rank[k][n]
106 : : 1) atom_rank[k1] < atom_rank[k2] <=> k1 < k2
107 : : where 2) atom_rank[k] > neigh_rank[k][i], i=1..n
108 : : 3) neigh_rank[k][i] < neigh_rank[k][j] <=> i < j
109 : :
110 : : Format-tgroup: tgroup_rank[k] endpoint_rank[k][1]...endpoint_rank[k][n]
111 : : where 1) tgroup_rank[k1] < tgroup_rank[k2] <=> k1 < k2
112 : : 2) endpoint_rank[k][i] < endpoint_rank[k][j] <=> i < j
113 : :
114 : : Note: tgroup_rank[k] > endpoint_rank[k][j] for all j by construction
115 : : */
116 : :
117 : : int lenCt; /* used length */
118 : : int nLenCTAtOnly; /* to split Ctnl comparison in case of bDigraph != 0 */
119 : : int maxlenCt; /* allocated length of Ctbl */
120 : : int maxPos; /* allocated length of nextCtblPos */
121 : : int maxVert; /* max number of vertices to separate atoms from taut groups */
122 : : int lenPos; /* first unused element of nextCtblPos */
123 : : AT_RANK *nextAtRank; /* rank (k value) after the last node of the Ctbl portion*/
124 : : AT_NUMB *nextCtblPos; /* first unused element of Ctbl */
125 : :
126 : : /* hydrogen atoms fixed in tautomeric representation:
127 : : compare before diff sign inversion: (+) <=> Ct1->() > Ct2->() */
128 : : NUM_H *NumH;
129 : : int lenNumH; /* used length */
130 : : int maxlenNumH; /* n + T_NUM_NO_ISOTOPIC*(n_tg-n) + 1 */
131 : :
132 : : /* hydrogen atoms fixed in non-tautomeric representation only:
133 : : compare before diff sign inversion: (+) <=> Ct1->() > Ct2->() */
134 : : NUM_H *NumHfixed;
135 : : /*int lenNumHfixed; */ /* used length */
136 : : /*int maxlenNumHfixed; */ /* max length = n+1 */
137 : :
138 : : /* isotopic atoms (without tautomeric H) and isotopic tautomeric groups */
139 : : /* note: AT_ISO_SORT_KEY and T_GROUP_ISOWT are identical types: long */
140 : : AT_ISO_SORT_KEY *iso_sort_key;
141 : : int len_iso_sort_key; /* used length */
142 : : int maxlen_iso_sort_key; /* max length = n_tg+1 */
143 : :
144 : : S_CHAR *iso_exchg_atnos;
145 : : int len_iso_exchg_atnos;
146 : : int maxlen_iso_exchg_atnos;
147 : :
148 : : /* isotopic hydrogen atoms fixed in non-tautomeric representation only */
149 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
150 : : AT_ISO_SORT_KEY *iso_sort_key_Hfixed;
151 : : int len_iso_sort_key_Hfixed; /* used length */
152 : : int maxlen_iso_sort_key_Hfixed; /* max length = n+1 */
153 : : #endif
154 : :
155 : : #ifdef INCHI_CANON_USE_HASH
156 : : CtHash *hash;
157 : : #endif
158 : : } ConTable;
159 : :
160 : :
161 : :
162 : : /****************************************************************************/
163 : :
164 : :
165 : : typedef struct tagkLeast
166 : : {
167 : : int k;
168 : : int i;
169 : : } kLeast;
170 : :
171 : :
172 : :
173 : : /************* local prototypes **********************************/
174 : : struct tagINCHI_CLOCK;
175 : : int CanonGraph( struct tagINCHI_CLOCK *ic,
176 : : CANON_GLOBALS *pCG,
177 : : int n,
178 : : int n_tg,
179 : : int n_max,
180 : : int bDigraph,
181 : : Graph *G,
182 : : Partition pi[],
183 : : AT_RANK *nSymmRank,
184 : : AT_RANK *nCanonRank,
185 : : AT_NUMB *nAtomNumberCanon,
186 : : CANON_DATA *pCD,
187 : : CANON_COUNTS *pCC,
188 : : ConTable **pp_zb_rho_inp,
189 : : ConTable **pp_zb_rho_out,
190 : : int LargeMolecules );
191 : :
192 : : void CtPartFill( Graph *G, CANON_DATA *pCD, Partition *p,
193 : : ConTable *Ct, int k, int n, int n_tg, int n_max );
194 : :
195 : : void CtPartClear( ConTable *Ct, int k );
196 : :
197 : : void CtPartINCHI_CANON_INFINITY( ConTable *Ct, S_CHAR *cmp, int k );
198 : :
199 : : int CtPartCompare( ConTable *Ct1, ConTable *Ct2, S_CHAR *cmp,
200 : : kLeast *kLeastForLayer, int k, int bOnlyCommon, int bSplitTautCompare );
201 : :
202 : : int CtFullCompare( ConTable *Ct1, ConTable *Ct2, int bOnlyCommon, int bSplitTautCompare );
203 : :
204 : : void CtPartCopy( ConTable *Ct1 /* to */, ConTable *Ct2 /* from */, int k );
205 : :
206 : : void CtFullCopy( ConTable *Ct1, ConTable *Ct2 );
207 : :
208 : : int CtFullCompareLayers( kLeast *kLeastForLayer );
209 : :
210 : : int CtCompareLayersGetFirstDiff( kLeast *kLeast_rho, int nOneAdditionalLayer,
211 : : int *L_rho, int *I_rho, int *k_rho );
212 : :
213 : : int CtPartCompareLayers( kLeast *kLeast_rho, int L_rho_fix_prev, int nOneAdditionalLayer );
214 : :
215 : : void UpdateCompareLayers( kLeast kLeastForLayer[], int hzz );
216 : :
217 : : int GetOneAdditionalLayer( CANON_DATA *pCD, ConTable *pzb_rho_fix );
218 : :
219 : : void CleanNumH( NUM_H *NumH, int len );
220 : :
221 : : int CleanCt( AT_RANK *Ct, int len );
222 : :
223 : : void CleanIsoSortKeys( AT_ISO_SORT_KEY * isk, int len );
224 : :
225 : : void MergeCleanIsoSortKeys( AT_ISO_SORT_KEY * isk1, AT_ISO_SORT_KEY * isk2, int len );
226 : :
227 : : int UnorderedPartitionJoin( UnorderedPartition *p1, UnorderedPartition *p2, int n );
228 : :
229 : : Node GetUnorderedPartitionMcrNode( UnorderedPartition *p1, Node v );
230 : :
231 : : int nJoin2Mcrs2( AT_RANK *nEqArray, AT_RANK n1, AT_RANK n2 );
232 : :
233 : : AT_RANK nGetMcr2( AT_RANK *nEqArray, AT_RANK n );
234 : :
235 : : int AllNodesAreInSet( NodeSet *cur_nodes, int lcur_nodes, NodeSet *set, int lset );
236 : :
237 : : void NodeSetFromVertices( CANON_GLOBALS *pCG, NodeSet *cur_nodes, int l, Node *v, int num_v );
238 : :
239 : : void CellMakeEmpty( Cell *baseW, int k );
240 : :
241 : : Node CellGetMinNode( Partition *p, Cell *W, Node v, CANON_DATA *pCD );
242 : :
243 : : int CellGetNumberOfNodes( Partition *p, Cell *W );
244 : :
245 : : int CellIntersectWithSet( struct tagCANON_GLOBALS *pCG, Partition *p, Cell *W, NodeSet *Mcr, int l );
246 : :
247 : : int PartitionColorVertex( CANON_GLOBALS *pCG, Graph *G, Partition *p, Node v, int n, int n_tg, int n_max, int bDigraph, int nNumPrevRanks );
248 : :
249 : : void PartitionCopy( Partition *To, Partition *From, int n );
250 : :
251 : : int PartitionSatisfiesLemma_2_25( Partition *p, int n );
252 : :
253 : : void PartitionGetTransposition( Partition *pFrom, Partition *pTo, int n, Transposition *gamma );
254 : :
255 : : void PartitionGetMcrAndFixSet( CANON_GLOBALS *pCG,
256 : : Partition *p,
257 : : NodeSet *Mcr,
258 : : NodeSet *Fix,
259 : : int n,
260 : : int l );
261 : :
262 : : int PartitionGetFirstCell( Partition *p, Cell *baseW, int k, int n );
263 : :
264 : : int PartitionIsDiscrete( Partition *p, int n );
265 : :
266 : : void PartitionFree( Partition *p );
267 : :
268 : : int PartitionCreate( Partition *p, int n );
269 : :
270 : : void UnorderedPartitionMakeDiscrete( UnorderedPartition *p, int n );
271 : :
272 : : void UnorderedPartitionFree( UnorderedPartition *p );
273 : :
274 : : int UnorderedPartitionCreate( UnorderedPartition *p, int n );
275 : :
276 : : void CTableFree( ConTable *Ct );
277 : :
278 : : int CTableCreate( ConTable *Ct, int n, CANON_DATA *pCD );
279 : :
280 : : void TranspositionFree( Transposition *p );
281 : :
282 : : int TranspositionCreate( Transposition *p, int n );
283 : :
284 : : void TranspositionGetMcrAndFixSetAndUnorderedPartition( struct tagCANON_GLOBALS *pCG, Transposition *gamma, NodeSet *McrSet, NodeSet *FixSet, int n, int l, UnorderedPartition *p );
285 : :
286 : : void insertions_sort_NeighList_AT_NUMBERS2( NEIGH_LIST base, AT_RANK *nRank, AT_RANK max_rj );
287 : :
288 : : int WriteGraph( Graph *G, int n, int gnum, char *fname, char *fmode );
289 : :
290 : : int SetInitialRanks2( int num_atoms,
291 : : ATOM_INVARIANT2* pAtomInvariant2,
292 : : AT_RANK *nNewRank,
293 : : AT_RANK *nAtomNumber,
294 : : CANON_GLOBALS *pCG );
295 : :
296 : : void FillOutAtomInvariant2( sp_ATOM* at, int num_atoms, int num_at_tg, ATOM_INVARIANT2* pAtomInvariant,
297 : : int bIgnoreIsotopic, int bHydrogensInRanks, int bHydrogensFixedInRanks,
298 : : int bDigraph, int bTautGroupsOnly, T_GROUP_INFO *t_group_info );
299 : :
300 : : int GetCanonRanking2( int num_atoms, int num_at_tg, int num_max, int bDigraph, sp_ATOM* at,
301 : : AT_RANK **pRankStack, int nNumPrevRanks,
302 : : AT_RANK *nSymmRank, AT_RANK *nCanonRank,
303 : : NEIGH_LIST *NeighList, AT_RANK *nTempRank,
304 : : CANON_STAT* pCS );
305 : :
306 : :
307 : : #if ( SEPARATE_CANON_CALLS == 1 )
308 : :
309 : :
310 : : /* for profiling purposes */
311 : :
312 : :
313 : : /****************************************************************************/
314 : : int CanonGraph01( int n,
315 : : int n_tg,
316 : : int n_max,
317 : : int bDigraph,
318 : : Graph *G,
319 : : Partition pi[],
320 : : AT_RANK *nSymmRank,
321 : : AT_RANK *nCanonRank,
322 : : AT_NUMB *nAtomNumberCanon,
323 : : CANON_DATA *pCD,
324 : : CANON_COUNTS *pCC,
325 : : ConTable **pp_zb_rho_inp,
326 : : ConTable **pp_zb_rho_out,
327 : : int LargeMolecules )
328 : : {
329 : : return
330 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
331 : : nSymmRank, nCanonRank, nAtomNumberCanon,
332 : : pCD, pCC, pp_zb_rho_inp, pp_zb_rho_out,
333 : : LargeMolecules );
334 : : }
335 : :
336 : : /****************************************************************************/
337 : : int CanonGraph02( int n,
338 : : int n_tg,
339 : : int n_max,
340 : : int bDigraph,
341 : : Graph *G,
342 : : Partition pi[],
343 : : AT_RANK *nSymmRank,
344 : : AT_RANK *nCanonRank,
345 : : AT_NUMB *nAtomNumberCanon,
346 : : CANON_DATA *pCD,
347 : : CANON_COUNTS *pCC,
348 : : ConTable **pp_zb_rho_inp,
349 : : ConTable **pp_zb_rho_out,
350 : : int LargeMolecules )
351 : : {
352 : : return
353 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
354 : : nSymmRank, nCanonRank, nAtomNumberCanon,
355 : : pCD, pCC,
356 : : pp_zb_rho_inp, pp_zb_rho_out );
357 : : }
358 : :
359 : :
360 : : /****************************************************************************/
361 : : int CanonGraph03( int n,
362 : : int n_tg,
363 : : int n_max,
364 : : int bDigraph,
365 : : Graph *G, Partition pi[],
366 : : AT_RANK *nSymmRank,
367 : : AT_RANK *nCanonRank,
368 : : AT_NUMB *nAtomNumberCanon,
369 : : CANON_DATA *pCD,
370 : : CANON_COUNTS *pCC,
371 : : ConTable **pp_zb_rho_inp,
372 : : ConTable **pp_zb_rho_out,
373 : : int LargeMolecules )
374 : : {
375 : : return
376 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
377 : : nSymmRank, nCanonRank, nAtomNumberCanon,
378 : : pCD, pCC,
379 : : pp_zb_rho_inp, pp_zb_rho_out );
380 : : }
381 : :
382 : :
383 : : /****************************************************************************/
384 : : int CanonGraph04( int n,
385 : : int n_tg,
386 : : int n_max,
387 : : int bDigraph,
388 : : Graph *G,
389 : : Partition pi[],
390 : : AT_RANK *nSymmRank,
391 : : AT_RANK *nCanonRank,
392 : : AT_NUMB *nAtomNumberCanon,
393 : : CANON_DATA *pCD,
394 : : CANON_COUNTS *pCC,
395 : : ConTable **pp_zb_rho_inp,
396 : : ConTable **pp_zb_rho_out,
397 : : int LargeMolecules )
398 : : {
399 : : return
400 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
401 : : nSymmRank, nCanonRank, nAtomNumberCanon,
402 : : pCD, pCC,
403 : : pp_zb_rho_inp, pp_zb_rho_out );
404 : : }
405 : :
406 : :
407 : : /****************************************************************************/
408 : : int CanonGraph05( int n,
409 : : int n_tg,
410 : : int n_max,
411 : : int bDigraph,
412 : : Graph *G,
413 : : Partition pi[],
414 : : AT_RANK *nSymmRank,
415 : : AT_RANK *nCanonRank,
416 : : AT_NUMB *nAtomNumberCanon,
417 : : CANON_DATA *pCD,
418 : : CANON_COUNTS *pCC,
419 : : ConTable **pp_zb_rho_inp,
420 : : ConTable **pp_zb_rho_out,
421 : : int LargeMolecules )
422 : : {
423 : : return
424 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
425 : : nSymmRank, nCanonRank, nAtomNumberCanon,
426 : : pCD, pCC,
427 : : pp_zb_rho_inp, pp_zb_rho_out );
428 : : }
429 : :
430 : :
431 : : /****************************************************************************/
432 : : int CanonGraph06( int n,
433 : : int n_tg,
434 : : int n_max,
435 : : int bDigraph,
436 : : Graph *G,
437 : : Partition pi[],
438 : : AT_RANK *nSymmRank,
439 : : AT_RANK *nCanonRank,
440 : : AT_NUMB *nAtomNumberCanon,
441 : : CANON_DATA *pCD,
442 : : CANON_COUNTS *pCC,
443 : : ConTable **pp_zb_rho_inp,
444 : : ConTable **pp_zb_rho_out,
445 : : int LargeMolecules )
446 : : {
447 : : return
448 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
449 : : nSymmRank, nCanonRank, nAtomNumberCanon,
450 : : pCD, pCC,
451 : : pp_zb_rho_inp, pp_zb_rho_out );
452 : : }
453 : :
454 : :
455 : : /****************************************************************************/
456 : : int CanonGraph07( int n,
457 : : int n_tg,
458 : : int n_max,
459 : : int bDigraph,
460 : : Graph *G,
461 : : Partition pi[],
462 : : AT_RANK *nSymmRank,
463 : : AT_RANK *nCanonRank,
464 : : AT_NUMB *nAtomNumberCanon,
465 : : CANON_DATA *pCD,
466 : : CANON_COUNTS *pCC,
467 : : ConTable **pp_zb_rho_inp,
468 : : ConTable **pp_zb_rho_out,
469 : : int LargeMolecules )
470 : : {
471 : : return
472 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
473 : : nSymmRank, nCanonRank, nAtomNumberCanon,
474 : : pCD, pCC,
475 : : pp_zb_rho_inp, pp_zb_rho_out );
476 : : }
477 : :
478 : :
479 : : /****************************************************************************/
480 : : int CanonGraph08( int n,
481 : : int n_tg,
482 : : int n_max,
483 : : int bDigraph,
484 : : Graph *G,
485 : : Partition pi[],
486 : : AT_RANK *nSymmRank,
487 : : AT_RANK *nCanonRank,
488 : : AT_NUMB *nAtomNumberCanon,
489 : : CANON_DATA *pCD,
490 : : CANON_COUNTS *pCC,
491 : : ConTable **pp_zb_rho_inp,
492 : : ConTable **pp_zb_rho_out,
493 : : int LargeMolecules )
494 : : {
495 : : return
496 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
497 : : nSymmRank, nCanonRank, nAtomNumberCanon,
498 : : pCD, pCC,
499 : : pp_zb_rho_inp, pp_zb_rho_out );
500 : : }
501 : :
502 : :
503 : : /****************************************************************************/
504 : : int CanonGraph09( int n,
505 : : int n_tg,
506 : : int n_max,
507 : : int bDigraph,
508 : : Graph *G,
509 : : Partition pi[],
510 : : AT_RANK *nSymmRank,
511 : : AT_RANK *nCanonRank,
512 : : AT_NUMB *nAtomNumberCanon,
513 : : CANON_DATA *pCD,
514 : : CANON_COUNTS *pCC,
515 : : ConTable **pp_zb_rho_inp,
516 : : ConTable **pp_zb_rho_out,
517 : : int LargeMolecules )
518 : : {
519 : : return
520 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
521 : : nSymmRank, nCanonRank, nAtomNumberCanon,
522 : : pCD, pCC,
523 : : pp_zb_rho_inp, pp_zb_rho_out );
524 : : }
525 : :
526 : :
527 : : /****************************************************************************/
528 : : int CanonGraph10( int n,
529 : : int n_tg,
530 : : int n_max,
531 : : int bDigraph,
532 : : Graph *G,
533 : : Partition pi[],
534 : : AT_RANK *nSymmRank,
535 : : AT_RANK *nCanonRank,
536 : : AT_NUMB *nAtomNumberCanon,
537 : : CANON_DATA *pCD,
538 : : CANON_COUNTS *pCC,
539 : : ConTable **pp_zb_rho_inp,
540 : : ConTable **pp_zb_rho_out,
541 : : int LargeMolecules )
542 : : {
543 : : return
544 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
545 : : nSymmRank, nCanonRank, nAtomNumberCanon,
546 : : pCD, pCC,
547 : : pp_zb_rho_inp, pp_zb_rho_out );
548 : : }
549 : :
550 : :
551 : : /****************************************************************************/
552 : : int CanonGraph11( int n,
553 : : int n_tg,
554 : : int n_max,
555 : : int bDigraph,
556 : : Graph *G,
557 : : Partition pi[],
558 : : AT_RANK *nSymmRank,
559 : : AT_RANK *nCanonRank,
560 : : AT_NUMB *nAtomNumberCanon,
561 : : CANON_DATA *pCD,
562 : : CANON_COUNTS *pCC,
563 : : ConTable **pp_zb_rho_inp,
564 : : ConTable **pp_zb_rho_out,
565 : : int LargeMolecules )
566 : : {
567 : : return
568 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
569 : : nSymmRank, nCanonRank, nAtomNumberCanon,
570 : : pCD, pCC,
571 : : pp_zb_rho_inp, pp_zb_rho_out );
572 : : }
573 : :
574 : :
575 : : /****************************************************************************/
576 : : int CanonGraph12( int n,
577 : : int n_tg,
578 : : int n_max,
579 : : int bDigraph,
580 : : Graph *G,
581 : : Partition pi[],
582 : : AT_RANK *nSymmRank,
583 : : AT_RANK *nCanonRank,
584 : : AT_NUMB *nAtomNumberCanon,
585 : : CANON_DATA *pCD,
586 : : CANON_COUNTS *pCC,
587 : : ConTable **pp_zb_rho_inp,
588 : : ConTable **pp_zb_rho_out,
589 : : int LargeMolecules )
590 : : {
591 : : return
592 : : CanonGraph( n, n_tg, n_max, bDigraph, G, pi,
593 : : nSymmRank, nCanonRank, nAtomNumberCanon,
594 : : pCD, pCC,
595 : : pp_zb_rho_inp, pp_zb_rho_out, LargeMolecules );
596 : : }
597 : : #else
598 : :
599 : : #define CanonGraph01 CanonGraph
600 : : #define CanonGraph02 CanonGraph
601 : : #define CanonGraph03 CanonGraph
602 : : #define CanonGraph04 CanonGraph
603 : : #define CanonGraph05 CanonGraph
604 : : #define CanonGraph06 CanonGraph
605 : : #define CanonGraph07 CanonGraph
606 : : #define CanonGraph08 CanonGraph
607 : : #define CanonGraph09 CanonGraph
608 : : #define CanonGraph10 CanonGraph
609 : : #define CanonGraph11 CanonGraph
610 : : #define CanonGraph12 CanonGraph
611 : :
612 : : #endif
613 : :
614 : :
615 : : #ifdef INCHI_CANON_USE_HASH
616 : :
617 : :
618 : : static call_fill_crc32_data = 1;
619 : : static const U_INT_32 crc32_data[256];
620 : :
621 : :
622 : : /****************************************************************************/
623 : : void fill_crc32_data( )
624 : : {
625 : : U_INT_32 c;
626 : : int n, k;
627 : :
628 : : for (n = 0; n < 256; n++)
629 : : {
630 : : c = (U_INT_32) n;
631 : : for (k = 0; k < 8; k++)
632 : : {
633 : : c = c & 1 ? 0xEDB88320L ^ ( c >> 1 ) : c >> 1;
634 : : }
635 : : crc32_data[n] = c;
636 : : }
637 : : call_fill_crc32_data = 0;
638 : : }
639 : :
640 : :
641 : : /****************************************************************************/
642 : : unsigned long add2crc32( unsigned long crc32, AT_NUMB n )
643 : : {
644 : : U_INT_08 chr;
645 : : if (call_fill_crc32_data)
646 : : {
647 : : fill_crc32_data( );
648 : : }
649 : : chr = n % 128;
650 : : crc32 = crc32_data[( (int) crc32 ^ (int) chr ) & 0xff] ^ ( crc32 >> 8 );
651 : : chr = n / 128;
652 : : crc32 = crc32_data[( (int) crc32 ^ (int) chr ) & 0xff] ^ ( crc32 >> 8 );
653 : : return crc32;
654 : : }
655 : : #endif
656 : :
657 : :
658 : :
659 : : /****************************************************************************/
660 : 1288 : int TranspositionCreate( Transposition *p, int n )
661 : : {
662 : 1288 : p->nAtNumb = (AT_NUMB*) inchi_calloc( n, sizeof( p->nAtNumb[0] ) );
663 [ + - ]: 1288 : if (p->nAtNumb)
664 : : {
665 : 1288 : return 1;
666 : : }
667 : :
668 : 0 : return 0;
669 : : }
670 : :
671 : :
672 : : /****************************************************************************/
673 : 1288 : void TranspositionFree( Transposition *p )
674 : : {
675 [ + - + - ]: 1288 : if (p && p->nAtNumb)
676 : : {
677 [ + - ]: 1288 : inchi_free( p->nAtNumb );
678 : 1288 : p->nAtNumb = NULL;
679 : : }
680 : 1288 : }
681 : :
682 : :
683 : : /****************************************************************************/
684 : 3864 : int NodeSetCreate( struct tagCANON_GLOBALS *pCG,
685 : : NodeSet *pSet,
686 : : int n,
687 : : int L )
688 : : {
689 : : int i, len;
690 : :
691 : 3864 : len = ( n + pCG->m_num_bit - 1 ) / pCG->m_num_bit;
692 : :
693 : 3864 : pSet->bitword = (bitWord**) inchi_calloc( L, sizeof( pSet->bitword[0] ) );
694 : :
695 [ - + ]: 3864 : if (!pSet->bitword)
696 : : {
697 : 0 : return 0;
698 : : }
699 : 3864 : pSet->bitword[0] = (bitWord*) inchi_calloc( (long long)len * (long long)L, sizeof( pSet->bitword[0][0] ) ); /* djb-rwth: cast operators added */
700 [ - + ]: 3864 : if (!pSet->bitword[0])
701 : : {
702 : : /* Cleanup */
703 [ # # ]: 0 : inchi_free( pSet->bitword );
704 : 0 : pSet->bitword = NULL;
705 : 0 : return 0; /* failed */
706 : : }
707 [ + + ]: 42504 : for (i = 1; i < L; i++)
708 : : {
709 : 38640 : pSet->bitword[i] = pSet->bitword[i - 1] + len;
710 : : }
711 : :
712 : 3864 : pSet->len_set = len;
713 : 3864 : pSet->num_set = L;
714 : :
715 : 3864 : return 1;
716 : : }
717 : :
718 : :
719 : : /****************************************************************************/
720 : 3864 : void NodeSetFree( struct tagCANON_GLOBALS *pCG, NodeSet *pSet )
721 : : {
722 [ + - + - ]: 3864 : if (pSet && pSet->bitword)
723 : : {
724 [ + - ]: 3864 : if (pSet->bitword[0])
725 : : {
726 [ + - ]: 3864 : inchi_free( pSet->bitword[0] );
727 : : }
728 [ + - ]: 3864 : inchi_free( pSet->bitword );
729 : 3864 : pSet->bitword = NULL;
730 : : }
731 : 3864 : }
732 : :
733 : :
734 : : /****************************************************************************/
735 : : /**
736 : : * @brief Grow a node set array so that it holds at least L sets.
737 : : *
738 : : * Capacity is doubled until it reaches L; existing sets are preserved.
739 : : * New sets are left uninitialized, since callers always clear a set before
740 : : * writing it (see PartitionGetMcrAndFixSet() and
741 : : * TranspositionGetMcrAndFixSetAndUnorderedPartition()).
742 : : *
743 : : * @param pSet Node set array created by NodeSetCreate().
744 : : * @param L Required number of sets.
745 : : * @return 1 on success, 0 if out of memory (pSet is then unchanged).
746 : : */
747 : 216 : static int NodeSetReserve( NodeSet *pSet, int L )
748 : : {
749 : 216 : int i, len = pSet->len_set, new_num_set = pSet->num_set;
750 : : bitWord **bitword;
751 : :
752 [ + - ]: 216 : if (L <= pSet->num_set)
753 : : {
754 : 216 : return 1;
755 : : }
756 [ # # ]: 0 : while (new_num_set < L)
757 : : {
758 : 0 : new_num_set *= 2;
759 : : }
760 : 0 : bitword = (bitWord**) inchi_malloc( new_num_set * sizeof( bitword[0] ) );
761 [ # # ]: 0 : if (!bitword)
762 : : {
763 : 0 : return 0;
764 : : }
765 : 0 : bitword[0] = (bitWord*) inchi_malloc( (size_t) len * new_num_set * sizeof( bitword[0][0] ) );
766 [ # # ]: 0 : if (!bitword[0])
767 : : {
768 [ # # ]: 0 : inchi_free( bitword );
769 : 0 : return 0;
770 : : }
771 : 0 : memcpy( bitword[0], pSet->bitword[0], (size_t) len * pSet->num_set * sizeof( bitword[0][0] ) );
772 [ # # ]: 0 : for (i = 1; i < new_num_set; i++)
773 : : {
774 : 0 : bitword[i] = bitword[i - 1] + len;
775 : : }
776 [ # # ]: 0 : inchi_free( pSet->bitword[0] );
777 [ # # ]: 0 : inchi_free( pSet->bitword );
778 : 0 : pSet->bitword = bitword;
779 : 0 : pSet->num_set = new_num_set;
780 : :
781 : 0 : return 1;
782 : : }
783 : :
784 : :
785 : : /****************************************************************************/
786 : 5138 : int CTableCreate( ConTable *Ct, int n, CANON_DATA *pCD )
787 : : {
788 : 5138 : int maxlenCt = pCD->nMaxLenLinearCT + 1; /* add one element for CtPartINCHI_CANON_INFINITY() */
789 [ + + ]: 5138 : int maxlenNumH = pCD->NumH ? ( pCD->maxlenNumH + 1 ) : 0;
790 [ - + ]: 5138 : int maxlenNumHfixed = pCD->NumHfixed ? ( pCD->maxlenNumHfixed + 1 ) : 0;
791 [ + + ]: 5138 : int maxlenIso = pCD->maxlen_iso_sort_key ? ( pCD->maxlen_iso_sort_key + 1 ) : 0;
792 [ - + ]: 5138 : int maxlenIsoExchg = pCD->iso_exchg_atnos ? ( pCD->maxlen_iso_exchg_atnos + 1 ) : 0;
793 : :
794 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
795 : : int maxlenIsoHfixed = pCD->maxlen_iso_sort_key_Hfixed ? ( pCD->maxlen_iso_sort_key_Hfixed + 1 ) : 0;
796 : : #endif
797 : :
798 : 5138 : memset( Ct, 0, sizeof( Ct[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
799 : :
800 : 5138 : Ct->maxVert = n;
801 : :
802 : 5138 : n++;
803 : :
804 : 5138 : Ct->Ctbl = (AT_RANK*) inchi_calloc( maxlenCt, sizeof( Ct->Ctbl[0] ) );
805 : 5138 : Ct->nextCtblPos = (AT_NUMB*) inchi_calloc( n, sizeof( Ct->nextCtblPos[0] ) );
806 : 5138 : Ct->nextAtRank = (AT_RANK*) inchi_calloc( n, sizeof( Ct->nextAtRank[0] ) );
807 [ + + ]: 5138 : if (maxlenNumH)
808 : : {
809 : 1337 : Ct->NumH = (NUM_H *) inchi_calloc( maxlenNumH, sizeof( Ct->NumH[0] ) );
810 : : }
811 [ - + ]: 5138 : if (maxlenNumHfixed)
812 : : {
813 : 0 : Ct->NumHfixed = (NUM_H *) inchi_calloc( maxlenNumHfixed, sizeof( Ct->NumH[0] ) );
814 : : }
815 [ + + ]: 5138 : if (maxlenIso)
816 : : {
817 : 16 : Ct->iso_sort_key = (AT_ISO_SORT_KEY *) inchi_calloc( maxlenIso, sizeof( Ct->iso_sort_key[0] ) );
818 : : }
819 [ - + ]: 5138 : if (maxlenIsoExchg)
820 : : {
821 : 0 : Ct->iso_exchg_atnos = (S_CHAR *) inchi_calloc( maxlenIsoExchg, sizeof( Ct->iso_exchg_atnos[0] ) );
822 : : }
823 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
824 : : if (maxlenIsoHfixed)
825 : : {
826 : : Ct->iso_sort_key_Hfixed = (AT_ISO_SORT_KEY *) inchi_calloc( maxlenIsoHfixed, sizeof( Ct->iso_sort_key_Hfixed[0] ) );
827 : : }
828 : : #endif
829 : : #ifdef INCHI_CANON_USE_HASH
830 : : Ct->hash = (CtHash*) inchi_calloc( n, sizeof( Ct->hash[0] ) );
831 : : #endif
832 : :
833 : 5138 : Ct->lenCt = 0;
834 : 5138 : Ct->nLenCTAtOnly = pCD->nLenCTAtOnly;
835 : 5138 : Ct->maxlenCt = maxlenCt;
836 : 5138 : Ct->lenNumH = 0;
837 : 5138 : Ct->maxlenNumH = maxlenNumH;
838 : 5138 : Ct->len_iso_sort_key = 0;
839 : 5138 : Ct->maxlen_iso_sort_key = maxlenIso;
840 : 5138 : Ct->len_iso_exchg_atnos = 0;
841 : 5138 : Ct->maxlen_iso_exchg_atnos = maxlenIso;
842 : :
843 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
844 : : Ct->len_iso_sort_key_Hfixed = 0;
845 : : Ct->maxlen_iso_sort_key_Hfixed = maxlenIsoHfixed;
846 : : #endif
847 : :
848 : 5138 : Ct->maxPos = n;
849 : 5138 : Ct->lenPos = 0;
850 : : /* djb-rwth: fixing a NULL pointer dereferences */
851 [ + - ]: 5138 : if (Ct->nextAtRank)
852 : 5138 : Ct->nextAtRank[0] = 0;
853 [ + - ]: 5138 : if (Ct->nextCtblPos)
854 : 5138 : Ct->nextCtblPos[0] = 0;
855 : :
856 [ + - + - : 5138 : if (Ct->Ctbl && Ct->nextCtblPos &&
+ + ]
857 [ + - - + ]: 5138 : ( !maxlenNumH || Ct->NumH ) &&
858 [ # # ]: 0 : ( !maxlenNumHfixed || Ct->NumHfixed ))
859 : : {
860 : 5138 : return 1;
861 : : }
862 : :
863 : 0 : return 0;
864 : : }
865 : :
866 : :
867 : : /****************************************************************************/
868 : 5138 : void CTableFree( ConTable *Ct )
869 : : {
870 [ + - ]: 5138 : if (Ct)
871 : : {
872 [ + + ]: 5138 : if (Ct->Ctbl)
873 : : {
874 [ + - ]: 3871 : inchi_free( Ct->Ctbl );
875 : : }
876 [ + - ]: 5138 : if (Ct->nextCtblPos)
877 : : {
878 [ + - ]: 5138 : inchi_free( Ct->nextCtblPos );
879 : : }
880 [ + - ]: 5138 : if (Ct->nextAtRank)
881 : : {
882 [ + - ]: 5138 : inchi_free( Ct->nextAtRank );
883 : : }
884 [ + + ]: 5138 : if (Ct->NumH)
885 : : {
886 [ + - ]: 70 : inchi_free( Ct->NumH );
887 : : }
888 [ - + ]: 5138 : if (Ct->NumHfixed)
889 : : {
890 [ # # ]: 0 : inchi_free( Ct->NumHfixed );
891 : : }
892 [ + + ]: 5138 : if (Ct->iso_sort_key)
893 : : {
894 [ + - ]: 6 : inchi_free( Ct->iso_sort_key );
895 : : }
896 [ - + ]: 5138 : if (Ct->iso_exchg_atnos)
897 : : {
898 [ # # ]: 0 : inchi_free( Ct->iso_exchg_atnos );
899 : : }
900 : :
901 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
902 : : if (Ct->iso_sort_key_Hfixed)
903 : : {
904 : : inchi_free( Ct->iso_sort_key_Hfixed );
905 : : }
906 : : #endif
907 : :
908 : : #ifdef INCHI_CANON_USE_HASH
909 : : if (Ct->hash)
910 : : {
911 : : inchi_free( Ct->hash );
912 : : }
913 : : #endif
914 : :
915 : 5138 : memset( Ct, 0, sizeof( Ct[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
916 : : }
917 : 5138 : }
918 : :
919 : :
920 : : /****************************************************************************/
921 : 2576 : int UnorderedPartitionCreate( UnorderedPartition *p, int n )
922 : : {
923 : 2576 : p->equ2 = (AT_NUMB*) inchi_calloc( n, sizeof( p->equ2[0] ) );
924 : : /* p->next = (AT_NUMB*)inchi_calloc( n, sizeof(p->next[0])); */
925 : :
926 [ + - ]: 2576 : if (p->equ2 /*&& p->next*/)
927 : : {
928 : 2576 : return 1;
929 : : }
930 : :
931 : 0 : return 0;
932 : : }
933 : :
934 : :
935 : : /****************************************************************************/
936 : 2576 : void UnorderedPartitionFree( UnorderedPartition *p )
937 : : {
938 [ + - ]: 2576 : if (p->equ2)
939 : : {
940 [ + - ]: 2576 : inchi_free( p->equ2 );
941 : : }
942 : :
943 : : /* if (p->next) inchi_free( p->next); */
944 : 2576 : p->equ2 = NULL;
945 : : /* p->next = NULL; */
946 : 2576 : }
947 : :
948 : :
949 : : /****************************************************************************/
950 : 1288 : void UnorderedPartitionMakeDiscrete( UnorderedPartition *p, int n )
951 : : {
952 : : int i;
953 [ + + ]: 13554 : for (i = 0; i < n; i++)
954 : : {
955 : 12266 : p->equ2[i] = (AT_NUMB) i;
956 : : /* p->next[i] = INCHI_CANON_INFINITY; */
957 : : }
958 : : INCHI_HEAPCHK
959 : 1288 : }
960 : :
961 : :
962 : : /****************************************************************************/
963 : 2576 : int PartitionCreate( Partition *p, int n )
964 : : {
965 : 2576 : p->AtNumber = (AT_NUMB*) inchi_calloc( n, sizeof( p->AtNumber[0] ) );
966 : 2576 : p->Rank = (AT_RANK*) inchi_calloc( n, sizeof( p->Rank[0] ) );
967 [ + - + - ]: 2576 : if (p->AtNumber && p->Rank)
968 : : {
969 : 2576 : return 1;
970 : : }
971 : 0 : return 0;
972 : : }
973 : :
974 : :
975 : : /****************************************************************************/
976 : 7644 : void PartitionFree( Partition *p )
977 : : {
978 [ + - ]: 7644 : if (p)
979 : : {
980 [ + + ]: 7644 : if (p->AtNumber)
981 : : {
982 [ + - ]: 3853 : inchi_free( p->AtNumber );
983 : 3853 : p->AtNumber = NULL;
984 : : }
985 [ + + ]: 7644 : if (p->Rank)
986 : : {
987 [ + - ]: 3853 : inchi_free( p->Rank );
988 : 3853 : p->Rank = NULL;
989 : : }
990 : : }
991 : 7644 : }
992 : :
993 : :
994 : : /****************************************************************************/
995 : 1609 : int PartitionIsDiscrete( Partition *p, int n )
996 : : {
997 : : int i;
998 : : AT_RANK r;
999 [ + + ]: 18579 : for (i = 0, r = 1; i < n; i++, r++)
1000 : : {
1001 [ + + ]: 17183 : if (r != ( rank_mask_bit & p->Rank[p->AtNumber[i]] ))
1002 : : {
1003 : : INCHI_HEAPCHK
1004 : 213 : return 0;
1005 : : }
1006 : : }
1007 : :
1008 : : INCHI_HEAPCHK
1009 : :
1010 : 1396 : return 1;
1011 : : }
1012 : :
1013 : :
1014 : : /****************************************************************************/
1015 : 213 : int PartitionGetFirstCell( Partition *p, Cell *baseW, int k, int n )
1016 : : {
1017 : : int i;
1018 : : AT_RANK r;
1019 : 213 : Cell *W = baseW + k - 1;
1020 : :
1021 [ + + ]: 213 : i = ( k > 1 ) ? baseW[k - 2].first + 1 : 0;
1022 : :
1023 [ + - ]: 213 : if (i < n)
1024 : : {
1025 : : /* bypass single vertex cells */
1026 [ + - + + ]: 335 : for (r = (AT_RANK) ( i + 1 ); i < n && r == ( rank_mask_bit & p->Rank[(int) p->AtNumber[i]] ); i++, r++)
1027 : : {
1028 : : ;
1029 : : }
1030 : : }
1031 [ + - ]: 213 : if (i < n)
1032 : : {
1033 : 213 : W->first = i;
1034 : 213 : for (r = ( rank_mask_bit & p->Rank[(int) p->AtNumber[i]] ), i++;
1035 [ + + + + ]: 516 : i < n && r == ( rank_mask_bit & p->Rank[(int) p->AtNumber[i]] );
1036 : 303 : i++)
1037 : : {
1038 : : ;
1039 : : }
1040 : 213 : W->next = i;
1041 : :
1042 : : INCHI_HEAPCHK
1043 : :
1044 : 213 : return ( W->next - W->first );
1045 : : }
1046 : :
1047 : 0 : W->first = INCHI_CANON_INFINITY;
1048 : 0 : W->next = 0;
1049 : :
1050 : : INCHI_HEAPCHK
1051 : :
1052 : 0 : return 0;
1053 : : }
1054 : :
1055 : :
1056 : : /****************************************************************************/
1057 : 273 : void CellMakeEmpty( Cell *baseW, int k )
1058 : : {
1059 : 273 : k--;
1060 : 273 : baseW[k].first = INCHI_CANON_INFINITY;
1061 : 273 : baseW[k].next = 0;
1062 : 273 : baseW[k].prev = -1;
1063 : :
1064 : : INCHI_HEAPCHK
1065 : 273 : }
1066 : :
1067 : :
1068 : : /****************************************************************************/
1069 : 0 : void NodeSetFromVertices( CANON_GLOBALS *pCG, NodeSet *cur_nodes, int l, Node *v, int num_v )
1070 : : {
1071 : 0 : bitWord *Bits = cur_nodes->bitword[l - 1];
1072 : 0 : int len = cur_nodes->len_set * sizeof( bitWord );
1073 : : int i, j;
1074 : :
1075 : 0 : memset( Bits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
1076 : :
1077 [ # # ]: 0 : for (i = 0; i < num_v; i++)
1078 : : {
1079 : 0 : j = (int) v[i] - 1;
1080 : 0 : Bits[j / pCG->m_num_bit] |= pCG->m_bBit[j % pCG->m_num_bit];
1081 : : }
1082 : :
1083 : : INCHI_HEAPCHK
1084 : 0 : }
1085 : :
1086 : :
1087 : : /****************************************************************************/
1088 : 0 : int AllNodesAreInSet( NodeSet *cur_nodes, int lcur_nodes, NodeSet *set, int lset )
1089 : : {
1090 : : int i;
1091 : 0 : int n = cur_nodes->len_set;
1092 : :
1093 : 0 : bitWord *BitsNode = cur_nodes->bitword[lcur_nodes - 1];
1094 : 0 : bitWord *BitsSet = set->bitword[lset - 1];
1095 : :
1096 : : /* find any BitsNode[i] bit not in BitsSet[i] */
1097 [ # # ]: 0 : for (i = 0; i < n; i++)
1098 : : {
1099 [ # # ]: 0 : if (BitsNode[i] & ~BitsSet[i])
1100 : : {
1101 : :
1102 : : INCHI_HEAPCHK
1103 : :
1104 : 0 : return 0;
1105 : : }
1106 : : }
1107 : :
1108 : : INCHI_HEAPCHK
1109 : :
1110 : 0 : return 1;
1111 : : }
1112 : :
1113 : :
1114 : : /****************************************************************************/
1115 : 0 : void PartitionGetMcrAndFixSet( CANON_GLOBALS *pCG,
1116 : : Partition *p,
1117 : : NodeSet *Mcr,
1118 : : NodeSet *Fix,
1119 : : int n,
1120 : : int l )
1121 : : {
1122 : : int i, j1, j2;
1123 : : AT_RANK r, r1;
1124 : 0 : bitWord *McrBits = Mcr->bitword[l - 1];
1125 : 0 : bitWord *FixBits = Fix->bitword[l - 1];
1126 : 0 : int len = Mcr->len_set * sizeof( bitWord );
1127 : :
1128 : 0 : memset( McrBits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
1129 : 0 : memset( FixBits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
1130 [ # # ]: 0 : for (i = 0, r = 1; i < n; i++, r++)
1131 : : {
1132 [ # # ]: 0 : if (r == ( r1 = ( rank_mask_bit&p->Rank[j1 = (int) p->AtNumber[i]] ) ))
1133 : : {
1134 : 0 : FixBits[j1 / pCG->m_num_bit] |= pCG->m_bBit[j1 % pCG->m_num_bit];
1135 : 0 : McrBits[j1 / pCG->m_num_bit] |= pCG->m_bBit[j1 % pCG->m_num_bit];
1136 : : }
1137 : : else
1138 : : {
1139 [ # # # # ]: 0 : for (r = r1; i + 1 < n && r == ( rank_mask_bit&p->Rank[j2 = (int) p->AtNumber[i + 1]] ); i++)
1140 : : {
1141 [ # # ]: 0 : if (j1 > j2)
1142 : : {
1143 : 0 : j1 = j2;
1144 : : }
1145 : : }
1146 : 0 : McrBits[j1 / pCG->m_num_bit] |= pCG->m_bBit[j1 % pCG->m_num_bit];
1147 : : }
1148 : : }
1149 : :
1150 : : INCHI_HEAPCHK
1151 : 0 : }
1152 : :
1153 : :
1154 : : /************* used in ichi_bns.c ********************************/
1155 : 0 : void NodeSetFromRadEndpoints( CANON_GLOBALS *pCG,
1156 : : NodeSet *cur_nodes,
1157 : : int k, /*Node *v*/
1158 : : Vertex RadEndpoints[],
1159 : : int num_v )
1160 : : {
1161 : 0 : bitWord *Bits = cur_nodes->bitword[k];
1162 : 0 : int len = cur_nodes->len_set * sizeof( bitWord );
1163 : : int i, j;
1164 : :
1165 : 0 : memset( Bits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
1166 : :
1167 [ # # ]: 0 : for (i = 1; i < num_v; i += 2)
1168 : : {
1169 : 0 : j = (int) RadEndpoints[i];
1170 : 0 : Bits[j / pCG->m_num_bit] |= pCG->m_bBit[j % pCG->m_num_bit];
1171 : : }
1172 : 0 : }
1173 : :
1174 : :
1175 : : /************* used in ichi_bns.c ********************************/
1176 : 0 : void RemoveFromNodeSet( CANON_GLOBALS *pCG,
1177 : : NodeSet *cur_nodes, int k, Vertex v[], int num_v )
1178 : : {
1179 [ # # ]: 0 : if (cur_nodes->bitword)
1180 : : {
1181 : 0 : bitWord *Bits = cur_nodes->bitword[k];
1182 : : /*int len = cur_nodes->len_set*sizeof(bitWord);*/
1183 : : int i, j;
1184 : :
1185 [ # # ]: 0 : for (i = 0; i < num_v; i++)
1186 : : {
1187 : 0 : j = (int) v[i];
1188 : 0 : Bits[j / pCG->m_num_bit] &= ~pCG->m_bBit[j % pCG->m_num_bit];
1189 : : }
1190 : : }
1191 : 0 : }
1192 : :
1193 : :
1194 : : /************* used in ichi_bns.c ********************************/
1195 : 0 : int DoNodeSetsIntersect( NodeSet *cur_nodes, int k1, int k2 )
1196 : : {
1197 [ # # ]: 0 : if (cur_nodes->bitword)
1198 : : {
1199 : 0 : bitWord *Bits1 = cur_nodes->bitword[k1];
1200 : 0 : bitWord *Bits2 = cur_nodes->bitword[k2];
1201 : 0 : int len = cur_nodes->len_set;
1202 : : int i;
1203 : :
1204 [ # # ]: 0 : for (i = 0; i < len; i++)
1205 : : {
1206 [ # # ]: 0 : if (Bits1[i] & Bits2[i])
1207 : : {
1208 : 0 : return 1;
1209 : : }
1210 : : }
1211 : : }
1212 : :
1213 : 0 : return 0;
1214 : : }
1215 : :
1216 : :
1217 : : /************* used in ichi_bns.c ********************************/
1218 : 0 : int IsNodeSetEmpty( NodeSet *cur_nodes, int k )
1219 : : {
1220 [ # # ]: 0 : if (cur_nodes->bitword)
1221 : : {
1222 : 0 : bitWord *Bits = cur_nodes->bitword[k];
1223 : 0 : int len = cur_nodes->len_set;
1224 : : int i;
1225 : :
1226 [ # # ]: 0 : for (i = 0; i < len; i++)
1227 : : {
1228 [ # # ]: 0 : if (Bits[i])
1229 : : {
1230 : 0 : return 0;
1231 : : }
1232 : : }
1233 : : }
1234 : :
1235 : 0 : return 1;
1236 : : }
1237 : :
1238 : :
1239 : : /************* used in ichi_bns.c ********************************/
1240 : 0 : void AddNodeSet2ToNodeSet1( NodeSet *cur_nodes, int k1, int k2 )
1241 : : {
1242 [ # # ]: 0 : if (cur_nodes->bitword)
1243 : : {
1244 : 0 : bitWord *Bits1 = cur_nodes->bitword[k1];
1245 : 0 : bitWord *Bits2 = cur_nodes->bitword[k2];
1246 : 0 : int len = cur_nodes->len_set;
1247 : : int i;
1248 : :
1249 [ # # ]: 0 : for (i = 0; i < len; i++)
1250 : : {
1251 : 0 : Bits1[i] |= Bits2[i];
1252 : : }
1253 : : }
1254 : 0 : }
1255 : :
1256 : :
1257 : : /************* used in ichi_bns.c ********************************/
1258 : 0 : int AddNodesToRadEndpoints( CANON_GLOBALS *pCG,
1259 : : NodeSet *cur_nodes,
1260 : : int k,
1261 : : Vertex RadEndpoints[],
1262 : : Vertex vRad,
1263 : : int nStart, int nLen )
1264 : : {
1265 : 0 : int n = nStart;
1266 [ # # ]: 0 : if (cur_nodes->bitword)
1267 : : {
1268 : 0 : bitWord *Bits = cur_nodes->bitword[k];
1269 : 0 : int len = cur_nodes->len_set;
1270 : : int i, j;
1271 : : Vertex v;
1272 : :
1273 [ # # ]: 0 : for (i = 0, v = 0; i < len; i++)
1274 : : {
1275 [ # # ]: 0 : if (Bits[i])
1276 : : {
1277 [ # # ]: 0 : for (j = 0; j < pCG->m_num_bit; j++, v++)
1278 : : {
1279 [ # # ]: 0 : if (Bits[i] & pCG->m_bBit[j])
1280 : : {
1281 [ # # ]: 0 : if (n >= nLen)
1282 : : {
1283 : 0 : return -1; /* overflow */
1284 : : }
1285 : 0 : RadEndpoints[n++] = vRad;
1286 : 0 : RadEndpoints[n++] = v;
1287 : : }
1288 : : }
1289 : : }
1290 : : else
1291 : : {
1292 : 0 : v += pCG->m_num_bit;
1293 : : }
1294 : : }
1295 : : }
1296 : :
1297 : 0 : return n;
1298 : : }
1299 : :
1300 : :
1301 : : /****************************************************************************/
1302 : 108 : void PartitionGetTransposition( Partition *pFrom,
1303 : : Partition *pTo,
1304 : : int n,
1305 : : Transposition *gamma )
1306 : : {
1307 : : int i;
1308 : :
1309 [ + + ]: 4236 : for (i = 0; i < n; i++)
1310 : : {
1311 : 4128 : gamma->nAtNumb[(int) pFrom->AtNumber[i]] = pTo->AtNumber[i];
1312 : : }
1313 : :
1314 : : INCHI_HEAPCHK
1315 : 108 : }
1316 : :
1317 : :
1318 : : /****************************************************************************
1319 : : Get minimal set (class) representative and partially compress the
1320 : : partitioning mcr = minimal class representative.
1321 : : ****************************************************************************/
1322 : 28793 : AT_RANK nGetMcr2( AT_RANK *nEqArray, AT_RANK n )
1323 : : {
1324 : : AT_RANK n1, n2, mcr; /* recursive version is much shorter. */
1325 : :
1326 : : INCHI_HEAPCHK
1327 : :
1328 : 28793 : n1 = nEqArray[(int) n];
1329 : :
1330 [ + + ]: 28793 : if (n == n1)
1331 : : {
1332 : 25121 : return n;
1333 : : }
1334 : :
1335 : : /* 1st pass: find mcr */
1336 [ + + ]: 3737 : while (n1 != ( n2 = nEqArray[(int) n1] ))
1337 : : {
1338 : 65 : n1 = n2;
1339 : : }
1340 : :
1341 : : /* 2nd pass: copy mcr to each element of the set starting from nEqArray[n] */
1342 : 3672 : mcr = n1;
1343 : 3672 : n1 = n;
1344 [ + + ]: 3737 : while ( /*n1*/ mcr != ( n2 = nEqArray[(int) n1] ))
1345 : : {
1346 : 65 : nEqArray[(int) n1] = mcr;
1347 : 65 : n1 = n2;
1348 : : }
1349 : :
1350 : : INCHI_HEAPCHK
1351 : :
1352 : 3672 : return ( mcr );
1353 : : }
1354 : :
1355 : :
1356 : : /****************************************************************************
1357 : : Join 2 sets (classes) that have members n1 and n2
1358 : : ****************************************************************************/
1359 : 1729 : int nJoin2Mcrs2( AT_RANK *nEqArray, AT_RANK n1, AT_RANK n2 )
1360 : : {
1361 : 1729 : n1 = nGetMcr2( nEqArray, n1 );
1362 : 1729 : n2 = nGetMcr2( nEqArray, n2 );
1363 : :
1364 [ + + ]: 1729 : if (n1 < n2)
1365 : : {
1366 : 2 : nEqArray[n2] = n1;
1367 : :
1368 : : INCHI_HEAPCHK
1369 : :
1370 : 2 : return 1; /* a change has been made */
1371 : : }
1372 : :
1373 [ + + ]: 1727 : if (n2 < n1)
1374 : : {
1375 : 1704 : nEqArray[n1] = n2;
1376 : :
1377 : : INCHI_HEAPCHK
1378 : :
1379 : 1704 : return 1; /* a change has been made */
1380 : : }
1381 : :
1382 : : INCHI_HEAPCHK
1383 : :
1384 : 23 : return 0; /* no changes */
1385 : : }
1386 : :
1387 : :
1388 : : /****************************************************************************/
1389 : 25335 : Node GetUnorderedPartitionMcrNode( UnorderedPartition *p1, Node v )
1390 : : {
1391 : 25335 : Node ret = (Node) ( 1 + nGetMcr2( p1->equ2, (AT_RANK) ( v - 1 ) ) );
1392 : :
1393 : : INCHI_HEAPCHK
1394 : :
1395 : 25335 : return ret;
1396 : : }
1397 : :
1398 : :
1399 : : /****************************************************************************
1400 : : Change p2 to (p2 v p1)
1401 : : ****************************************************************************/
1402 : 108 : int UnorderedPartitionJoin( UnorderedPartition *p1,
1403 : : UnorderedPartition *p2,
1404 : : int n )
1405 : : {
1406 : : int i, j;
1407 : 108 : int nNumChanges = 0;
1408 [ + + ]: 4236 : for (i = 0; i < n; i++)
1409 : : {
1410 [ + + - + ]: 4128 : if (( j = (int) p1->equ2[i] ) == i || p2->equ2[(int) i] == p2->equ2[(int) j])
1411 : : {
1412 : 2399 : continue;
1413 : : }
1414 : 1729 : nNumChanges += nJoin2Mcrs2( p2->equ2, (AT_NUMB) i, (AT_NUMB) j );
1415 : : }
1416 : :
1417 : : INCHI_HEAPCHK
1418 : :
1419 : 108 : return nNumChanges;
1420 : : }
1421 : :
1422 : :
1423 : : /****************************************************************************/
1424 : 209 : int PartitionSatisfiesLemma_2_25( Partition *p, int n )
1425 : : {
1426 : 209 : int nPartitionSize = 0;
1427 : 209 : int nNumNonTrivialCells = 0;
1428 : : AT_RANK r;
1429 : : int i, num;
1430 : :
1431 [ + + ]: 8482 : for (i = num = 0, r = 1; i < n; i++, r++)
1432 : : {
1433 [ + + ]: 8273 : if (( rank_mask_bit & p->Rank[(int) p->AtNumber[i]] ) == r)
1434 : : {
1435 : 4647 : nPartitionSize++;
1436 [ + + ]: 4647 : if (num)
1437 : : {
1438 : : /* num+1 = cell size > 1 */
1439 : 3453 : nNumNonTrivialCells++;
1440 : 3453 : num = 0;
1441 : : }
1442 : : }
1443 : : else
1444 : : {
1445 : 3626 : num++;
1446 : : }
1447 : : }
1448 : :
1449 : : /* check Lemma_2_25 conditions */
1450 [ + + ]: 209 : if (n <= nPartitionSize + 4 ||
1451 [ + + ]: 42 : n == nPartitionSize + nNumNonTrivialCells ||
1452 [ - + ]: 28 : n == nPartitionSize + nNumNonTrivialCells + 1)
1453 : : {
1454 : 181 : return 1;
1455 : : }
1456 : :
1457 : 28 : return 0;
1458 : : }
1459 : :
1460 : :
1461 : : /****************************************************************************/
1462 : 1667 : void PartitionCopy( Partition *To, Partition *From, int n )
1463 : : {
1464 : : int i;
1465 : 1667 : memcpy(To->AtNumber, From->AtNumber, n * sizeof(To->AtNumber[0]));
1466 : 1667 : memcpy(To->Rank, From->Rank, n * sizeof(To->AtNumber[0]));
1467 : :
1468 [ + + ]: 35890 : for (i = 0; i < n; i++)
1469 : : {
1470 : 34223 : To->Rank[i] &= rank_mask_bit;
1471 : : }
1472 : :
1473 : : INCHI_HEAPCHK
1474 : 1667 : }
1475 : :
1476 : :
1477 : : /****************************************************************************
1478 : : Makes new equitable partition (p+1) out of p;
1479 : : first reduce the rank of vertex v
1480 : : ***************************************************************************/
1481 : 324 : int PartitionColorVertex( CANON_GLOBALS *pCG,
1482 : : Graph *G,
1483 : : Partition *p,
1484 : : Node v,
1485 : : int n,
1486 : : int n_tg,
1487 : : int n_max,
1488 : : int bDigraph,
1489 : : int nNumPrevRanks )
1490 : : {
1491 : : int nNumNewRanks, i, j;
1492 : 324 : long lNumNeighListIter = 0;
1493 : : AT_RANK rv, r;
1494 : : AT_NUMB s, sv;
1495 : :
1496 [ + + ]: 972 : for (i = 1; i <= 2; i++)
1497 : : {
1498 [ + + ]: 648 : if (!p[i].AtNumber)
1499 : : {
1500 : 131 : p[i].AtNumber = (AT_NUMB *) inchi_malloc( n_max * sizeof( p[0].AtNumber[0] ) );
1501 : : }
1502 [ + + ]: 648 : if (!p[i].Rank)
1503 : : {
1504 : 131 : p[i].Rank = (AT_RANK *) inchi_malloc( n_max * sizeof( p[0].Rank[0] ) );
1505 : : }
1506 [ + - - + ]: 648 : if (!p[i].AtNumber || !p[i].Rank)
1507 : : {
1508 : :
1509 : : INCHI_HEAPCHK
1510 : :
1511 : 0 : return CT_OUT_OF_RAM;
1512 : : }
1513 : : }
1514 : :
1515 : 324 : PartitionCopy( p + 1, p, n_tg );
1516 : :
1517 : 324 : sv = v - 1; /* atom number we are looking for */
1518 [ - + ]: 324 : if (sv >= (AT_NUMB) n_tg)
1519 : : {
1520 : :
1521 : : INCHI_HEAPCHK
1522 : :
1523 : 0 : return CT_CANON_ERR; /* !!! severe program error: sv not found !!! */
1524 : : }
1525 : :
1526 : 324 : rv = p[1].Rank[(int) sv]; /* rank of this atom */
1527 : :
1528 : : /* second, locate sv among all vertices that have same rank as v */
1529 : 324 : s = n_max + 1; /* always greater than sv; this initialization is needed only to keep the compiler happy */
1530 [ + - + - : 696 : for (j = (int) rv - 1; 0 <= j && rv == ( p[1].Rank[(int) ( s = p[1].AtNumber[j] )] ) && s != sv; j--) /* djb-rwth: removing redundant code */
+ + ]
1531 : : {
1532 : : ;
1533 : : }
1534 : :
1535 [ - + ]: 324 : if (s != sv)
1536 : : {
1537 : : INCHI_HEAPCHK
1538 : 0 : return CT_CANON_ERR; /* !!! severe program error: sv not found !!! */
1539 : : }
1540 : :
1541 : : /* shift preceding atom numbers to the right to fill the gap after removing sv */
1542 : 324 : r = rv - 1; /* initialization only to keep compiler happy */
1543 [ + + + + ]: 445 : for (i = j--; 0 <= j && rv == ( r = p[1].Rank[(int) ( s = p[1].AtNumber[j] )] ); i = j, j--)
1544 : : {
1545 : 121 : p[1].AtNumber[i] = s;
1546 : : }
1547 : :
1548 [ + + ]: 324 : r = ( i > 0 ) ? ( r + 1 ) : 1; /* new reduced rank = (next lower rank)+1 or 1 */
1549 : : /* insert sv and adjust its rank */
1550 : 324 : p[1].AtNumber[i] = sv;
1551 : 324 : p[1].Rank[(int) sv] = r;
1552 : :
1553 : :
1554 : : /* make equitable partition */
1555 [ + + ]: 324 : if (bDigraph)
1556 : : {
1557 : : /*
1558 : : nNumNewRanks = DifferentiateRanks2( pCG, n_tg, G,
1559 : : nNumPrevRanks+1, p[1].Rank, p[2].Rank,
1560 : : p[1].AtNumber, &lNumNeighListIter, 1 );
1561 : : */
1562 : 73 : nNumNewRanks = DifferentiateRanks4( pCG, n_tg, G,
1563 : 73 : nNumPrevRanks + 1, p[1].Rank, p[2].Rank /* temp array */,
1564 : 73 : p[1].AtNumber, (AT_RANK) n, &lNumNeighListIter );
1565 : : }
1566 : : else
1567 : : {
1568 : : /*
1569 : : nNumNewRanks = DifferentiateRanks2( pCG, n_tg, G,
1570 : : nNumPrevRanks+1, p[1].Rank, p[2].Rank,
1571 : : p[1].AtNumber, &lNumNeighListIter, 1 );
1572 : : */
1573 : 251 : nNumNewRanks = DifferentiateRanks3( pCG, n_tg, G,
1574 : 251 : nNumPrevRanks + 1, p[1].Rank, p[2].Rank /* temp array */,
1575 : 251 : p[1].AtNumber, &lNumNeighListIter );
1576 : : }
1577 : : INCHI_HEAPCHK
1578 : :
1579 : 324 : return nNumNewRanks;
1580 : : }
1581 : :
1582 : :
1583 : : /****************************************************************************/
1584 : : typedef struct tagNodeValues
1585 : : {
1586 : : NUM_H NumH;
1587 : : AT_ISO_SORT_KEY iso_sort_key;
1588 : : NUM_H NumHfixed;
1589 : :
1590 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
1591 : : AT_ISO_SORT_KEY iso_sort_key_Hfixed;
1592 : : #endif
1593 : :
1594 : : AT_NUMB nAtNumber;
1595 : : } NV;
1596 : :
1597 : :
1598 : :
1599 : : /****************************************************************************
1600 : : Return min node > vPrev or INCHI_CANON_INFINITY if not found
1601 : : Input: v = previous atom number + 1 or 0 on first call
1602 : : ****************************************************************************/
1603 : 586 : Node CellGetMinNode( Partition *p, Cell *W, Node v, CANON_DATA *pCD )
1604 : : {
1605 : : AT_NUMB i;
1606 : 586 : AT_NUMB uCurAtNumb, uMinAtNumb = INCHI_CANON_INFINITY;
1607 : :
1608 : : /* in case of emty cell: (W->first=INCHI_CANON_INFINITY) > (W->next=0); returns INCHI_CANON_INFINITY */
1609 [ - + ]: 586 : if (W->first > W->next)
1610 : : {
1611 : 0 : return INCHI_CANON_INFINITY;
1612 : : }
1613 : :
1614 : : #if ( USE_AUX_RANKING == 1 )
1615 [ + - + + ]: 586 : if (pCD && pCD->nAuxRank)
1616 : 125 : {
1617 : : AT_RANK uMinAuxRank, uCurAuxRank;
1618 : : int nCurAtNumb;
1619 : :
1620 : : #if ( USE_AUX_RANKING_ALL == 1 )
1621 : : AT_RANK uInpAuxRank;
1622 : : int nInpAtNumb, nMinAtNumb;
1623 : : #endif
1624 : :
1625 [ + - ]: 125 : for (i = W->first; i < W->next; i++)
1626 : : {
1627 : 125 : uCurAtNumb = p->AtNumber[(int) i];
1628 [ + - ]: 125 : if (!( p->Rank[(int) uCurAtNumb] & rank_mark_bit ))
1629 : : {
1630 : 125 : break; /* found the first unmarked yet node */
1631 : : }
1632 : : }
1633 [ - + ]: 125 : if (i == W->next)
1634 : : {
1635 : 0 : return INCHI_CANON_INFINITY;
1636 : : }
1637 : :
1638 : : #if ( USE_AUX_RANKING_ALL == 1 )
1639 : : /*==== vertex ordering definition ===
1640 : : * vertex v1 < v2 <=> (AuxRank(v1)==AuxRank(v2) && AtNumb(v1) < AtNumb(v2)) || (AuxRank(v1) < AuxRank(v2))
1641 : : * vertex v1 > v2 <=> (AuxRank(v1)==AuxRank(v2) && AtNumb(v1) > AtNumb(v2)) || (AuxRank(v1) > AuxRank(v2))
1642 : : * vertex v1 = v2 <=> (AuxRank(v1)==AuxRank(v2) && AtNumb(v1) == AtNumb(v2))
1643 : : */
1644 : :
1645 : : /* set initial vMin so that vMin > any vertex */
1646 : 125 : uMinAuxRank = INCHI_CANON_INFINITY;
1647 : 125 : nMinAtNumb = INCHI_CANON_INFINITY;
1648 : : /* set vInp */
1649 [ + + ]: 125 : if (v)
1650 : : {
1651 : 52 : nInpAtNumb = (int) v - 1; /* previous vertex */
1652 : 52 : uInpAuxRank = pCD->nAuxRank[nInpAtNumb];
1653 : : }
1654 : : else
1655 : : {
1656 : 73 : nInpAtNumb = -1; /* less than any vertex */
1657 : 73 : uInpAuxRank = 0;
1658 : : }
1659 : : /* find vMin = min { vCur : (vCur > vInp) && (vCur in W) } */
1660 [ + + ]: 421 : for (; i < W->next; i++)
1661 : : {
1662 : 296 : nCurAtNumb = (int) p->AtNumber[(int) i];
1663 [ + - ]: 296 : if (!( p->Rank[nCurAtNumb] & rank_mark_bit ))
1664 : : {
1665 : : /* vertex nCurAtNumb is not marked, find whether it fits the conditions */
1666 : 296 : uCurAuxRank = pCD->nAuxRank[nCurAtNumb];
1667 [ + + + + : 296 : if ((uCurAuxRank == uInpAuxRank && nCurAtNumb > nInpAtNumb) || uCurAuxRank > uInpAuxRank) /* djb-rwth: addressing LLVM warning */
+ + ]
1668 : : {
1669 : : /* here vCur > vInp */
1670 [ + + - + ]: 204 : if (uCurAuxRank == uMinAuxRank && nCurAtNumb < nMinAtNumb)
1671 : : {
1672 : : /* vCur < vMin (1) */
1673 : 0 : nMinAtNumb = nCurAtNumb;
1674 : : }
1675 [ + + ]: 204 : else if (uCurAuxRank < uMinAuxRank)
1676 : : {
1677 : : /* vCur < vMin (2) */
1678 : 109 : uMinAuxRank = uCurAuxRank;
1679 : 109 : nMinAtNumb = nCurAtNumb;
1680 : : }
1681 : : }
1682 : : }
1683 : : }
1684 : :
1685 [ + + ]: 125 : uMinAtNumb = ( nMinAtNumb == INCHI_CANON_INFINITY ) ? INCHI_CANON_INFINITY : (AT_NUMB) nMinAtNumb;
1686 : :
1687 : : #else
1688 : :
1689 : : if (v)
1690 : : {
1691 : : nCurAtNumb = (int) v - 1;
1692 : : /* any valid found node must have nAuxRank == uMinAuxRank */
1693 : : uMinAuxRank = pCD->nAuxRank[nCurAtNumb];
1694 : : }
1695 : : else
1696 : : {
1697 : : /* any valid found node must have minimal uMinAuxRank from pCD->nAuxRank[] */
1698 : : uMinAuxRank = INCHI_CANON_INFINITY; /* undefined */
1699 : : }
1700 : :
1701 : : for (; i < W->next; i++)
1702 : : {
1703 : : uCurAtNumb = p->AtNumber[(int) i];
1704 : : nCurAtNumb = (int) uCurAtNumb;
1705 : : if (uCurAtNumb >= v && !( p->Rank[nCurAtNumb] & rank_mark_bit ))
1706 : : {
1707 : : uCurAuxRank = pCD->nAuxRank[nCurAtNumb];
1708 : : if (v)
1709 : : {
1710 : : /* get next node */
1711 : : /* find node with smallest uCurAtNumb among nodes with aux. ranks equal to uMinAuxRank */
1712 : : if (uCurAuxRank == uMinAuxRank && uCurAtNumb < uMinAtNumb)
1713 : : {
1714 : : uMinAtNumb = uCurAtNumb;
1715 : : }
1716 : : }
1717 : : else
1718 : : {
1719 : : /* get first node */
1720 : : /* find node with smallest smallest uCurAtNumb among nodes with smallest aux. ranks */
1721 : : if (uMinAuxRank > uCurAuxRank)
1722 : : {
1723 : : uMinAuxRank = uCurAuxRank;
1724 : : uMinAtNumb = uCurAtNumb;
1725 : : }
1726 : : else
1727 : : {
1728 : : if (uMinAuxRank == uCurAuxRank && uCurAtNumb < uMinAtNumb)
1729 : : {
1730 : : uMinAtNumb = uCurAtNumb;
1731 : : }
1732 : : }
1733 : : }
1734 : : }
1735 : : }
1736 : : #endif
1737 : : }
1738 : :
1739 : : else
1740 : : #endif /* } USE_AUX_RANKING */
1741 : :
1742 : : {
1743 [ + + ]: 1775 : for (i = W->first; i < W->next; i++)
1744 : : {
1745 : 1314 : uCurAtNumb = p->AtNumber[(int) i];
1746 [ + + + - : 1314 : if (uCurAtNumb >= v && !( p->Rank[(int) uCurAtNumb] & rank_mark_bit ) && uCurAtNumb < uMinAtNumb)
+ + ]
1747 : : {
1748 : 377 : uMinAtNumb = uCurAtNumb;
1749 : : }
1750 : : }
1751 : : }
1752 : :
1753 [ + + ]: 586 : if (uMinAtNumb != INCHI_CANON_INFINITY)
1754 : : {
1755 : 480 : uMinAtNumb++;
1756 : : }
1757 : :
1758 : : INCHI_HEAPCHK
1759 : :
1760 : 586 : return uMinAtNumb;
1761 : : }
1762 : :
1763 : :
1764 : : /****************************************************************************/
1765 : 106 : int CellGetNumberOfNodes( Partition *p, Cell *W )
1766 : : {
1767 : 106 : int first = W->first;
1768 : 106 : int next = W->next;
1769 : : int i, num;
1770 [ + + ]: 373 : for (i = first, num = 0; i < next; i++)
1771 : : {
1772 [ + - ]: 267 : if (!( rank_mark_bit & p->Rank[(int) p->AtNumber[i]] ))
1773 : : {
1774 : 267 : num++;
1775 : : }
1776 : : }
1777 : :
1778 : : INCHI_HEAPCHK
1779 : :
1780 : 106 : return num;
1781 : : }
1782 : :
1783 : :
1784 : : /****************************************************************************/
1785 : 0 : int CellIntersectWithSet( CANON_GLOBALS *pCG,
1786 : : Partition *p, Cell *W, NodeSet *Mcr, int l )
1787 : : {
1788 : 0 : bitWord *McrBits = Mcr->bitword[l - 1];
1789 : 0 : int first = W->first;
1790 : 0 : int next = W->next;
1791 : : int i, j, k;
1792 : :
1793 [ # # ]: 0 : if (first >= next)
1794 : : {
1795 : : /* for testing only */
1796 : 0 : return 0;
1797 : : }
1798 [ # # ]: 0 : for (i = first, k = 0; i < next; i++)
1799 : : {
1800 : 0 : j = (int) p->AtNumber[i];
1801 [ # # ]: 0 : if (!( McrBits[j / pCG->m_num_bit] & pCG->m_bBit[j % pCG->m_num_bit] ))
1802 : : {
1803 : : /* BC: reading uninit memory ???-not examined yet */
1804 : 0 : k += !( p->Rank[j] & rank_mark_bit ); /* for testing only */
1805 : 0 : p->Rank[j] |= rank_mark_bit;
1806 : : }
1807 : : }
1808 : : INCHI_HEAPCHK
1809 : :
1810 : 0 : return k;
1811 : : }
1812 : :
1813 : :
1814 : : /****************************************************************************/
1815 : 55 : void CtPartClear( ConTable *Ct, int k )
1816 : : {
1817 : : int start;
1818 : : int len;
1819 : : /* connection table */
1820 [ - + ]: 55 : start = k > 1 ? Ct->nextCtblPos[k - 1] : 0;
1821 : 55 : len = Ct->lenCt - start;
1822 [ - + ]: 55 : if (len > 0)
1823 : : {
1824 : 0 : memset( Ct->Ctbl + start, 0, ( (long long)Ct->lenCt - (long long)start ) * sizeof( Ct->Ctbl[0] ) ); /* djb-rwth: cast operators added; memset_s C11/Annex K variant? */
1825 : : }
1826 : 55 : Ct->lenCt = start;
1827 : 55 : Ct->lenPos = k;
1828 : :
1829 : : INCHI_HEAPCHK
1830 : 55 : }
1831 : :
1832 : :
1833 : : /****************************************************************************
1834 : : Sort neighbors according to ranks in ascending order
1835 : : ****************************************************************************/
1836 : 19226 : void insertions_sort_NeighList_AT_NUMBERS2( NEIGH_LIST base,
1837 : : AT_RANK *nRank,
1838 : : AT_RANK max_rj )
1839 : : {
1840 : : AT_NUMB *i, *j, *pk, tmp, rj;
1841 : 19226 : int k, num = (int) *base++;
1842 [ + + ]: 37279 : for (k = 1, pk = base; k < num; k++, pk++)
1843 : : {
1844 : 18053 : i = pk;
1845 : 18053 : j = i + 1;
1846 : 18053 : rj = ( rank_mask_bit & nRank[(int) *j] );
1847 [ + + ]: 18053 : if (rj < max_rj)
1848 : : {
1849 [ + + + + ]: 900 : while (j > base && rj < ( rank_mask_bit & nRank[(int) *i] ))
1850 : : {
1851 : 182 : tmp = *i;
1852 : 182 : *i = *j;
1853 : 182 : *j = tmp;
1854 : 182 : j = i--;
1855 : : }
1856 : : }
1857 : : }
1858 : :
1859 : : INCHI_HEAPCHK
1860 : 19226 : }
1861 : :
1862 : :
1863 : : /****************************************************************************
1864 : : may need previous Lambda
1865 : : ****************************************************************************/
1866 : 1557 : void CtPartFill( Graph *G,
1867 : : CANON_DATA *pCD,
1868 : : Partition *p,
1869 : : ConTable *Ct,
1870 : : int k,
1871 : : int n,
1872 : : int n_tg,
1873 : : int n_max
1874 : : )
1875 : : {
1876 : : /* k = (new index in Ct->nextAtRank[] and Ct->nextCtblPos[]) + 1 */
1877 : :
1878 : : int startCtbl;
1879 : : int startAtOrd;
1880 : : AT_RANK r, rj, nn, j, rj_prev; /* djb-rwth: ignoring LLVM warning as the variable is used */
1881 : : int i, m, an_sao;
1882 : :
1883 : : #ifdef INCHI_CANON_USE_HASH
1884 : : CtHash hash = 0;
1885 : : #endif
1886 : :
1887 : : /* djb-rwth: removing redundant code */
1888 : :
1889 : : INCHI_HEAPCHK
1890 : :
1891 : 1557 : k--;
1892 [ + - + + ]: 1557 : if (Ct && k) /* djb-rwth: fixing oss-fuzz issue #69612 */
1893 : : {
1894 : 210 : startCtbl = Ct->nextCtblPos[k - 1];
1895 : 210 : startAtOrd = Ct->nextAtRank[k - 1] - 1; /* here p->Rank[p->AtNumber[r-1]] = r */
1896 : : }
1897 : : else
1898 : : {
1899 : 1347 : startCtbl = 0;
1900 : 1347 : startAtOrd = 0;
1901 : : }
1902 : :
1903 : : /******* Well-defined (by fixed ranks) part of the connection table ************/
1904 : : /* djb-rwth: fixing oss-fuzz issue #69612 */
1905 [ - + ]: 1557 : if (startAtOrd < 0)
1906 : : {
1907 : 0 : return;
1908 : : }
1909 : : /* djb-rwth: fixing oss-fuzz issue #391043585 */
1910 [ + - + - ]: 1557 : if (startAtOrd < n_max && Ct)
1911 : : {
1912 : 1557 : an_sao = (int)p->AtNumber[startAtOrd];
1913 : 1557 : r = (rank_mask_bit & p->Rank[an_sao]);
1914 : :
1915 [ + + + + ]: 20783 : for (i = startAtOrd; i < n_tg && r == (rank_mask_bit & p->Rank[m = (int)p->AtNumber[i]]); i++, r++)
1916 : : {
1917 : 19226 : Ct->Ctbl[startCtbl++] = r;
1918 : 19226 : insertions_sort_NeighList_AT_NUMBERS2(G[m], p->Rank, r);
1919 : 19226 : nn = G[m][0]; /* number of neighbors */
1920 : 19226 : rj_prev = 0; /* debug only */ /* djb-rwth: ignoring LLVM warning as the variable is used */
1921 : :
1922 : : #ifdef INCHI_CANON_USE_HASH
1923 : : hash = add2crc32(hash, (AT_NUMB)(r + n));
1924 : : #endif
1925 : :
1926 [ + + + + ]: 37389 : for (j = 1; j <= nn && (rj = (rank_mask_bit & p->Rank[(int)G[m][j]])) < r; j++)
1927 : : {
1928 : 18163 : Ct->Ctbl[startCtbl++] = rj;
1929 : :
1930 : : #ifdef INCHI_CANON_USE_HASH
1931 : : hash = add2crc32(hash, rj);
1932 : : #endif
1933 : :
1934 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
1935 : : /* debug only */
1936 : : if (rj < rj_prev)
1937 : : {
1938 : : int stop = 1; /* <BRKPT> */
1939 : : }
1940 : : #endif
1941 : :
1942 : 18163 : rj_prev = rj; /* djb-rwth: ignoring LLVM warning as the variable is used */
1943 : : }
1944 : : }
1945 : : }
1946 : : else
1947 : : {
1948 : 0 : return;
1949 : : }
1950 : :
1951 : : INCHI_HEAPCHK
1952 : :
1953 : : /****************** Well-defined part of base hydrogen atoms *******************/
1954 [ + - ]: 1557 : if (Ct)
1955 : : {
1956 [ + + + - ]: 1557 : if (pCD->NumH && Ct->NumH)
1957 : : {
1958 : 86 : nn = inchi_min(n, i);
1959 [ + + ]: 6231 : for (j = startAtOrd; j < nn; j++)
1960 : : {
1961 : : /* atoms */
1962 : 6145 : Ct->NumH[j] = pCD->NumH[p->AtNumber[j]];
1963 : : }
1964 [ + + ]: 138 : for (; j < i; j++)
1965 : : {
1966 : : /* t-groups */
1967 : 52 : int data_pos = n + T_NUM_NO_ISOTOPIC * ((int)p->AtNumber[j] - n);
1968 [ + + ]: 156 : for (m = 0; m < T_NUM_NO_ISOTOPIC; m++)
1969 : : {
1970 : 104 : Ct->NumH[nn++] = pCD->NumH[data_pos++];
1971 : : }
1972 : : }
1973 : 86 : Ct->lenNumH = nn;
1974 : : }
1975 : : else
1976 : : {
1977 : 1471 : Ct->lenNumH = 0;
1978 : : }
1979 : :
1980 : : INCHI_HEAPCHK
1981 : :
1982 : : /****************** Well-defined part of fixed hydrogen atoms *******************/
1983 [ - + - - ]: 1557 : if (pCD->NumHfixed && Ct->NumHfixed)
1984 : : {
1985 : 0 : nn = inchi_min(n, i);
1986 [ # # ]: 0 : for (j = startAtOrd; j < nn; j++)
1987 : : {
1988 : 0 : Ct->NumHfixed[j] = pCD->NumHfixed[p->AtNumber[j]];
1989 : :
1990 : : INCHI_HEAPCHK
1991 : : }
1992 : : /* Ct->lenNumHfixed = nn; */
1993 : : }
1994 : : else
1995 : : {
1996 : : ;/* Ct->lenNumHfixed = 0; */
1997 : : }
1998 : :
1999 : : INCHI_HEAPCHK
2000 : :
2001 : : /****************** Well-defined part of isotopic keys ***************************/
2002 [ + + + - ]: 1557 : if (pCD->iso_sort_key && Ct->iso_sort_key)
2003 : : {
2004 [ + + ]: 6006 : for (j = startAtOrd; j < i; j++)
2005 : : {
2006 : 6000 : Ct->iso_sort_key[j] = pCD->iso_sort_key[p->AtNumber[j]];
2007 : : }
2008 : 6 : Ct->len_iso_sort_key = i;
2009 : : }
2010 : : else
2011 : : {
2012 : 1551 : Ct->len_iso_sort_key = 0;
2013 : : }
2014 : :
2015 : : INCHI_HEAPCHK
2016 : :
2017 : : /****************** Well-defined part of isotopic iso_exchg_atnos ***************************/
2018 [ - + - - ]: 1557 : if (pCD->iso_exchg_atnos && Ct->iso_exchg_atnos)
2019 : : {
2020 [ # # ]: 0 : for (j = startAtOrd; j < i; j++)
2021 : : {
2022 : 0 : Ct->iso_exchg_atnos[j] = pCD->iso_exchg_atnos[p->AtNumber[j]];
2023 : : }
2024 : 0 : Ct->len_iso_exchg_atnos = i;
2025 : : }
2026 : : else
2027 : : {
2028 : 1557 : Ct->len_iso_exchg_atnos = 0;
2029 : : }
2030 : :
2031 : : INCHI_HEAPCHK
2032 : :
2033 : : /******** Well-defined part of isotopic keys for fixed hydrogen atoms ************/
2034 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
2035 : : if (pCD->iso_sort_key_Hfixed && Ct->iso_sort_key_Hfixed)
2036 : : {
2037 : : nn = inchi_min(n, i);
2038 : : for (j = startAtOrd; j < nn; j++)
2039 : : {
2040 : : Ct->iso_sort_key_Hfixed[j] = pCD->iso_sort_key_Hfixed[p->AtNumber[j]];
2041 : : }
2042 : : Ct->len_iso_sort_key_Hfixed = nn;
2043 : : }
2044 : : else
2045 : : {
2046 : : Ct->len_iso_sort_key_Hfixed = 0;
2047 : : }
2048 : : #endif
2049 : :
2050 : : INCHI_HEAPCHK
2051 : :
2052 : 1557 : Ct->lenCt = startCtbl; /* not aways increases */
2053 : 1557 : Ct->nextCtblPos[k] = startCtbl;
2054 : 1557 : Ct->nextAtRank[k] = r;
2055 : 1557 : Ct->lenPos = k + 1;
2056 : :
2057 : : /* The rest of the CTable */
2058 : :
2059 : : #ifdef INCHI_CANON_USE_HASH
2060 : : while (i < n)
2061 : : {
2062 : : r = (rank_mask_bit & p->Rank[m = (int)p->AtNumber[i]]);
2063 : : hash = add2crc32(hash, (AT_NUMB)(r + n));
2064 : : r++;
2065 : : insertions_sort_NeighList_AT_NUMBERS2(G[m], p->Rank, r);
2066 : : nn = G[m][0];
2067 : : rj_prev = 0; /* debug only */
2068 : : for (j = 1; j <= nn && (rj = (rank_mask_bit & p->Rank[(int)G[m][j]])) < r; j++)
2069 : : {
2070 : : hash = add2crc32(hash, rj);
2071 : : }
2072 : : i++;
2073 : : }
2074 : : Ct->hash[k] = hash;
2075 : : #endif
2076 : : }
2077 : : INCHI_HEAPCHK
2078 : : }
2079 : :
2080 : :
2081 : : /****************************************************************************/
2082 : 1343 : void CtPartINCHI_CANON_INFINITY( ConTable *Ct, S_CHAR *cmp, int k )
2083 : : {
2084 : : int startCtbl;
2085 : : /*int startAtOrd;*/
2086 : 1343 : k--;
2087 [ + - ]: 1343 : if (k)
2088 : : {
2089 : 1343 : startCtbl = Ct->nextCtblPos[k - 1];
2090 : : /*startAtOrd = Ct->nextAtRank[k-1]-1;*/ /* here p->Rank[p->AtNumber[r-1]] = r */
2091 [ - + ]: 1343 : if (cmp)
2092 : : {
2093 : 0 : memset( cmp, 0, k * sizeof( cmp[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
2094 : : }
2095 : : }
2096 : : else
2097 : : {
2098 : 0 : startCtbl = 0;
2099 : : /*startAtOrd = 0;*/
2100 : : }
2101 [ + - + - ]: 1343 : if (!startCtbl || Ct->Ctbl[startCtbl - 1] != EMPTY_CT)
2102 : : {
2103 : 1343 : Ct->Ctbl[startCtbl] = EMPTY_CT;
2104 : : }
2105 : :
2106 : : INCHI_HEAPCHK
2107 : 1343 : }
2108 : :
2109 : :
2110 : : /****************************************************************************
2111 : : Return value:
2112 : : -1 <=> *Lambda1 < *Lambda2
2113 : : 0 <=> *Lambda1 = *Lambda2
2114 : : +1 <=> *Lambda1 > *Lambda2
2115 : :
2116 : : Input: k+1 = value of level at which the comparison is executed
2117 : : (that is, in the calling program k(caller) = k+1)
2118 : :
2119 : : Stars (*) below mark the differences:
2120 : :
2121 : : bSplitTautCompare != 0 => directed graph; compare:
2122 : : non-tautomeric part of CT in layer 0; (*)
2123 : : non-tautomeric H in layer 1; (*)
2124 : : tautomeric part of CT & H in layer 2; (*)
2125 : : fixed H in layer 3;
2126 : : isotopic atoms, non-taut
2127 : : H & t-groups in layer 4;
2128 : : fixed isotopic H in layer 5; <- move to layer 4
2129 : :
2130 : : bSplitTautCompare == 0 => undirected graph; compare:
2131 : : full CT in Layer 0; (*)
2132 : : taut and non-taut H in Layer 1; (*)
2133 : : * nothing * in layer 2; (*)
2134 : : fixed H in layer 3;
2135 : : isotopic atoms, non-taut
2136 : : H & t-groups in layer 4;
2137 : : fixed isotopic H in layer 5; <- move to layer 4
2138 : :
2139 : : ****************************************************************************/
2140 : 509 : int CtPartCompare( ConTable *Ct1,
2141 : : ConTable *Ct2,
2142 : : S_CHAR *cmp,
2143 : : kLeast *kLeastForLayer,
2144 : : int k,
2145 : : int bOnlyCommon,
2146 : : int bSplitTautCompare )
2147 : : {
2148 : : int startCt1, endCt1, startCt2, endCt2; /*endCt,*/
2149 : : int startAt1, endAt1, startAt2, endAt2; /*endCt,*/
2150 : 509 : int midCt /* end of atoms only Ct */, midNumH = 0 /* end of atoms only NumH */, maxVert;
2151 : : int diff, i, k1, k2, lenNumH, /*mid_iso_sort_key,*/ midAt; /* djb-rwth: ignoring LLVM warning: variables used */
2152 : 509 : int nLayer = 0;
2153 : :
2154 : 509 : k--;
2155 : 509 : i = -1;
2156 : :
2157 : : /* set kLeastForLayer[nLayer].k = (k+1) or -(k+1)
2158 : : kLeastForLayer[nLayer].i = iDiff
2159 : : if all the conditions are met:
2160 : : 1) kLeastForLayer[nLayer].k = 0
2161 : : 2) diff==0 for all layers < nLayer
2162 : :
2163 : : sign:
2164 : : if the final diff < 0 then kLeastForLayer[nLayer].k = -(k+1) else
2165 : : if the final diff > 0 then kLeastForLayer[nLayer].k = +(k+1)
2166 : :
2167 : : k+1 instead of k takes into account k--; statememt above)
2168 : :
2169 : : meaning:
2170 : : ========
2171 : : abs(kLeastForLayer[nLayer].k) is the greatest level k at which
2172 : : difference at layer nLayer are zeroes of hidden by differences in smaller nLayer.
2173 : :
2174 : : "Hidden by difference in smaller level" means that nLayer of comparison
2175 : : has not been reached because the difference was discovered at a previous layer.
2176 : :
2177 : :
2178 : : Lambda vs zf_zeta comparison
2179 : : =============================================
2180 : : accept only diff == 0
2181 : :
2182 : : Lambda vs pzb_rho and pzb_rho_fix comparison
2183 : : =============================================
2184 : : Maintain kLeastForLayer[] and kLeastForLayerFix[]
2185 : :
2186 : : The algorithm provides that pzb_rho(m-1) < pzb_rho(m) <= pzb_rho_fix
2187 : :
2188 : : Definition: pzb_rho(m-1) < pzb_rho(m) means that
2189 : : -----------------------------------------------
2190 : : pzb_rho(m-1)[nLayerCurr] == pzb_rho(m)[nLayerCurr] for nLayerCurr = 0..nLayerDiff-1
2191 : : pzb_rho(m-1)[nLayerDiff] < pzb_rho(m)[nLayerDiff]
2192 : :
2193 : : Definition: pzb_rho(m-1)[nLayerDiff] < pzb_rho(m)[nLayerDiff] means that
2194 : : -------------------------------------------------------------------------
2195 : : pzb_rho(m-1)[nLayerDiff][i] == pzb_rho(m)[nLayerDiff][i] for i=0..iDdiff-1
2196 : : pzb_rho(m-1)[nLayerDiff][iDdiff] < pzb_rho(m)[nLayerDiff][iDdiff]
2197 : :
2198 : : This defines nLayerDiff(pzb1, pzb2) where pszb1 = pzb_rho(a), pzb2=pzb_rho(b) (a<b) or pzb_rho_fix
2199 : : and iDdiff (pzb1, pzb2).
2200 : : In case pzb_rho(m)[nLayerCurr] == pzb_rho_fix[nLayerCurr] for all non-NULL nLayerCurr in pzb_rho_fix,
2201 : : nLayerDiff(pzb_rho(m), pzb_rho_fix) = the first layer in pzb_rho(m) not present in pzb_rho_fix
2202 : : iDdiff (pzb_rho(m), pzb_rho_fix) = -1
2203 : : Case when such a layer does not exist means program error
2204 : :
2205 : : Suppose L_rho = nLayerDiff(Lambda, pzb_rho(m))
2206 : : L_fix = nLayerDiff(Lambda, pzb_rho_fix)
2207 : : I_rho = iDdiff (Lambda, pzb_rho(m))
2208 : : I_fix = iDdiff (Lambda, pzb_rho_fix)
2209 : : kLeastForLayer determined from Lambda vs pzb_rho(m) comparison
2210 : : Then:
2211 : :
2212 : : 1. Comparison Lambda vs pzb_rho_fix before reaching discrete partition
2213 : : ----------------------------------------------------------------------
2214 : : a) 0 < abs(kLeastForLayerFix[L_fix].k) <= k-1 (* in this case I_fix >= 0 *) &&
2215 : : ((L_fix < L_rho) || (L_fix == L_rho && I_fix < I_rho))
2216 : : =>
2217 : : qzb_rho_fix = kLeastForLayerFix[L_fix].k if prevoiusly qzb_rho_fix == 0
2218 : :
2219 : : b) otherwise do not change qzb_rho_fix, except the following:
2220 : :
2221 : : c) Special case L_rho == L_fix && I_rho == I_fix. Let L=L_rho, I = I_rho.
2222 : :
2223 : : Compare 3 valirs: Lambda[L][I], pzb_rho(m)[L][I], pzb_rho_fix[L][I]
2224 : : The algorithm provides pzb_rho(m)[L][I] < pzb_rho_fix[L][I]
2225 : : (pzb_rho(m)[L][I]==pzb_rho_fix[L][I] <=> pzb_rho(m)[L][I]==pzb_rho_fix[L][I]
2226 : : is impossible by construction)
2227 : : There are 3 possibilities:
2228 : : c1) Lambda[L][I] < pzb_rho(m)[L][I] < pzb_rho_fix[L][I] <=>
2229 : : kLeastForLayer[L].k < 0 && kLeastForLayerFix[L].k < 0
2230 : : => qzb_rho := kLeastForLayer[L].k, reject too small Lambda
2231 : : c2) pzb_rho(m)[L][I] < Lambda[L][I] < pzb_rho_fix[L][I]
2232 : : kLeastForLayer[L].k > 0 && kLeastForLayerFix[L].k < 0
2233 : : => qzb_rho := kLeastForLayer[L].k, accept Lambda, rho:=nu
2234 : : c3) pzb_rho(m)[L][I] < pzb_rho_fix[L][I] < Lambda[L][I]
2235 : : kLeastForLayer[L].k > 0 && kLeastForLayerFix[L].k > 0
2236 : : => qzb_rho_fix := kLeastForLayerFix[L].k, reject too big Lambda
2237 : :
2238 : : Case
2239 : : kLeastForLayer[L].k < 0 && kLeastForLayerFix[L].k > 0 is impossible
2240 : : because it means
2241 : : pzb_rho_fix < Lambda < pzb_rho(m) <=> pzb_rho_fix < pzb_rho(m)
2242 : :
2243 : :
2244 : : Case (c3) occurs in case of (a)
2245 : : Case (c1)
2246 : :
2247 : : 2. Comparison Lambda vs pzb_rho before reaching discrete partition
2248 : : ----------------------------------------------------------------------
2249 : : a) (L_rho < L_fix) || (L_rho == L_fix && I_rho < I_fix) =>
2250 : :
2251 : : Lambda differs from pzb_rho(m) in the part of pzb_rho(m) that will never change
2252 : : qzb_rho = kLeastForLayer[L_rho].k; reject Labmda or accept pzb_rho(m+1):=Labmda
2253 : :
2254 : : b) (L_rho == L_fix && I_rho > I_fix) && kLeastForLayer[L_rho].k < 0
2255 : : Lambda < pzb_rho(m), therefore
2256 : : qzb_rho = kLeastForLayer[L_rho].k; reject Labmda
2257 : :
2258 : : c) (L_rho > L_fix) =>
2259 : : qzb_rho := 0 because more significant difference may be discovered
2260 : : in layer < L_rho later. The final comparison may be needed at the
2261 : : level of discrete partition.
2262 : :
2263 : :
2264 : : */
2265 : :
2266 [ - + ]: 509 : if (cmp)
2267 : : {
2268 [ # # # # ]: 0 : for (i = 0; i <= k && !cmp[i]; i++)
2269 : : {
2270 : : ;
2271 : : }
2272 [ # # ]: 0 : if (i < k)
2273 : : {
2274 : 0 : cmp[k] = cmp[i];
2275 : 0 : return (int) cmp[i];
2276 : : }
2277 : : }
2278 : :
2279 : 509 : k1 = Ct1->lenPos - 1; /* djb-rwth: ignoring LLVM warning: variable used */
2280 : 509 : k2 = Ct2->lenPos - 1; /* djb-rwth: ignoring LLVM warning: variable used */
2281 : :
2282 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
2283 : : if (k > k1 || k > k2)
2284 : : {
2285 : : int stop = 1;
2286 : : }
2287 : : #endif
2288 : :
2289 : 509 : diff = 0; /* djb-rwth: ignoring LLVM warning: variable used */
2290 : :
2291 [ + + ]: 509 : if (k)
2292 : : {
2293 : 375 : startCt1 = Ct1->nextCtblPos[k - 1];
2294 : 375 : startCt2 = Ct2->nextCtblPos[k - 1];
2295 : 375 : startAt1 = Ct1->nextAtRank[k - 1] - 1;
2296 : 375 : startAt2 = Ct2->nextAtRank[k - 1] - 1;
2297 : : }
2298 : : else
2299 : : {
2300 : 134 : startCt1 = startCt2 = 0;
2301 : 134 : startAt1 = startAt2 = 0;
2302 : : }
2303 : :
2304 : 509 : endCt1 = Ct1->nextCtblPos[k];
2305 : 509 : endCt2 = Ct2->nextCtblPos[k];
2306 : 509 : endAt1 = (int) Ct1->nextAtRank[k] - 1;
2307 : 509 : endAt2 = (int) Ct2->nextAtRank[k] - 1;
2308 : :
2309 : 509 : maxVert = inchi_min( Ct1->maxVert, Ct2->maxVert );
2310 : :
2311 : : #ifdef INCHI_CANON_USE_HASH
2312 : : if (!diff)
2313 : : {
2314 : : if (Ct1->hash[k] > Ct2->hash[k])
2315 : : diff = 1;
2316 : : else
2317 : : if (Ct1->hash[k] < Ct2->hash[k])
2318 : : diff = -1;
2319 : : }
2320 : : if (diff)
2321 : : {
2322 : : goto done;
2323 : : }
2324 : : #endif
2325 : :
2326 : : /************************** lengths **************************************************/
2327 [ - + ]: 509 : if ((diff = -( startCt1 - startCt2 ))) /* djb-rwth: addressing LLVM warning */
2328 : : {
2329 : : /* comparing two INCHI_CANON_INFINITY terminations */
2330 [ # # ]: 0 : if (bOnlyCommon &&
2331 [ # # # # ]: 0 : startCt1 >= Ct1->nLenCTAtOnly && startCt2 >= Ct2->nLenCTAtOnly &&
2332 [ # # # # ]: 0 : Ct1->Ctbl[startCt1] == EMPTY_CT && Ct2->Ctbl[startCt2] == EMPTY_CT)
2333 : : {
2334 : 0 : return 0;
2335 : : }
2336 [ # # ]: 0 : if (bOnlyCommon)
2337 : : {
2338 : 0 : startCt1 = inchi_min( startCt1, startCt2 ); /* djb-rwth: removing redundant code */
2339 : 0 : startAt1 = startAt2 = inchi_min( startAt1, startAt2 );
2340 [ # # ]: 0 : if (Ct1->lenCt == Ct2->lenCt)
2341 : : {
2342 : 0 : endCt1 = endCt2 = inchi_max( endCt1, endCt2 );
2343 : 0 : endAt1 = endAt2 = inchi_max( endAt1, endAt2 );
2344 : : }
2345 : :
2346 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
2347 : : else
2348 : : {
2349 : : int stop = 1;
2350 : : }
2351 : : #endif
2352 : : }
2353 : : else
2354 : : /* comparing (taut tail) vs INCHI_CANON_INFINITY termination -- ??? */
2355 [ # # ]: 0 : if (startCt1 > startCt2 &&
2356 [ # # ]: 0 : Ct1->maxVert > Ct2->maxVert &&
2357 [ # # ]: 0 : startAt2 == Ct2->maxVert)
2358 : : {
2359 : 0 : return 0;
2360 : : }
2361 : : else
2362 : : {
2363 : 0 : goto done;
2364 : : }
2365 : : }
2366 : :
2367 : 509 : lenNumH = Ct1->lenNumH;
2368 : : /* djb-rwth: removing redundant code */
2369 : :
2370 [ + + ]: 509 : if ((diff = -( endCt1 - endCt2 ))) /* djb-rwth: addressing LLVM warning */
2371 : : {
2372 : : /* negative sign reproduces results for NSC=28393 */
2373 [ + - ]: 24 : if (bOnlyCommon)
2374 : : {
2375 : 24 : endCt1 = inchi_min( endCt1, endCt2 ); /* djb-rwth: removing redundant code */
2376 : 24 : endAt1 = endAt2 = inchi_min( endAt1, endAt2 );
2377 : 24 : lenNumH = inchi_min( Ct1->lenNumH, Ct2->lenNumH );
2378 : : /* djb-rwth: removing redundant code */
2379 : : }
2380 : : else
2381 : : {
2382 : : /* take care of case when comparing tautomeric vs non-tautomeric:
2383 : : since (taut)->maxVert > (non-taut)->maxVert, --???
2384 : : (taut)->maxlenCt > (non-taut)->maxlenCt --!!!
2385 : : compare up to min out of the two, ignoring INCHI_CANON_INFINITY in the last position */
2386 [ # # # # ]: 0 : if (endCt1 > endCt2 && Ct1->maxlenCt > Ct2->maxlenCt)
2387 : : {
2388 [ # # ]: 0 : if (endAt2 == Ct2->maxVert + 1)
2389 : : {
2390 : : /* remove INCHI_CANON_INFINITY termination of the shorter CT */
2391 : : /* should never happen */
2392 : 0 : endAt2--;
2393 : 0 : lenNumH = endAt1 = endAt2; /* djb-rwth: removing redundant code */
2394 : 0 : endCt2--;
2395 : 0 : endCt1 = endCt2;
2396 : 0 : diff = 0; /* djb-rwth: ignoring LLVM warning: value used? */
2397 : : }
2398 [ # # ]: 0 : else if (endAt2 == Ct2->maxVert)
2399 : : {
2400 : : /* remove INCHI_CANON_INFINITY termination of CT */
2401 : 0 : lenNumH = endAt1 = endAt2; /* djb-rwth: removing redundant code */
2402 : 0 : endCt1 = endCt2;
2403 : 0 : diff = 0; /* djb-rwth: ignoring LLVM warning: value used? */
2404 : : }
2405 : : else
2406 : : {
2407 : 0 : goto done;
2408 : : }
2409 : : }
2410 : : else
2411 : : {
2412 : 0 : goto done;
2413 : : }
2414 : : }
2415 : : }
2416 : :
2417 [ + + ]: 509 : if (bSplitTautCompare)
2418 : : {
2419 : 175 : midCt = inchi_min( Ct1->nLenCTAtOnly, Ct2->nLenCTAtOnly );
2420 [ + + ]: 175 : if (midCt > endCt1)
2421 : : {
2422 : 101 : midCt = endCt1;
2423 : : }
2424 : 175 : midAt = inchi_min( maxVert, endAt1 );
2425 : : }
2426 : : else
2427 : : {
2428 : 334 : midCt = endCt1;
2429 : 334 : midAt = endAt1;
2430 : : }
2431 : :
2432 : :
2433 : : /*endCt = min(endCt1, endCt2);*/
2434 : : /*************************************************************************/
2435 : : /************ layer 0: connection table without tautomeric groups ********/
2436 : : /*************************************************************************/
2437 : :
2438 [ + + + - ]: 40629 : for (i = startCt1; i < midCt && Ct1->Ctbl[i] == Ct2->Ctbl[i]; i++)
2439 : : /*for ( i = startCt1; i < endCt && !(diff = (int)Ct1->Ctbl[i] - (int)Ct2->Ctbl[i]); i ++ )*/
2440 : : {
2441 : : ;
2442 : : }
2443 : :
2444 [ - + ]: 509 : if (i < midCt)
2445 : : {
2446 : 0 : diff = (int) Ct1->Ctbl[i] - (int) Ct2->Ctbl[i];
2447 : 0 : goto done;
2448 : : }
2449 : :
2450 : : /*************************************************************************/
2451 : : /******** layer 1 NumH: H atoms without tautomeric H *********************/
2452 : : /*************************************************************************/
2453 : :
2454 : 509 : nLayer++;
2455 : :
2456 : : /*============= check limits for consistency ==========*/
2457 [ - + ]: 509 : if ((diff = -( startAt1 - startAt2 ))) /* djb-rwth: addressing LLVM warning */
2458 : : {
2459 : 0 : goto done; /* should not happen */
2460 : : }
2461 [ - + ]: 509 : if ((diff = -( endAt1 - endAt2 ))) /* djb-rwth: addressing LLVM warning */
2462 : : {
2463 : 0 : goto done; /* should not happen */
2464 : : }
2465 : :
2466 : : /*============= comparison =============================*/
2467 [ + + + - ]: 509 : if (Ct1->NumH && Ct2->NumH)
2468 : : {
2469 [ + + ]: 175 : if (endAt1 < maxVert)
2470 : : {
2471 : 101 : midNumH = lenNumH = endAt1;
2472 : : }
2473 [ + - ]: 74 : else if (bSplitTautCompare)
2474 : : {
2475 : 74 : midNumH = maxVert;
2476 : : }
2477 : : else
2478 : : {
2479 : 0 : midNumH = lenNumH;
2480 : : }
2481 : :
2482 : : /* lenNumH = (endAt2 >= maxVert)? lenNumH : endAt2; */
2483 : : /* endAt1 = (endAt2 == n)? lenNumH : endAt2; */
2484 : :
2485 [ + + + - ]: 12499 : for (i = startAt1; i < midNumH && Ct1->NumH[i] == Ct2->NumH[i]; i++)
2486 : : {
2487 : : ;
2488 : : }
2489 [ - + ]: 175 : if (i < midNumH)
2490 : : {
2491 : 0 : diff = (int) Ct1->NumH[i] - (int) Ct2->NumH[i];
2492 : 0 : goto done;
2493 : : }
2494 : : }
2495 : :
2496 : :
2497 : : /*************************************************************************/
2498 : : /************** layer 2: tautomeric part of CT and tautomeric H **********/
2499 : : /*************************************************************************/
2500 : 509 : nLayer++;
2501 [ + + + - ]: 785 : for (i = midCt; i < endCt1 && Ct1->Ctbl[i] == Ct2->Ctbl[i]; i++)
2502 : : {
2503 : : ; /* compare tautomeric groups part of CT */
2504 : : }
2505 [ - + ]: 509 : if (i < endCt1)
2506 : : {
2507 : 0 : diff = (int) Ct1->Ctbl[i] - (int) Ct2->Ctbl[i];
2508 : 0 : goto done;
2509 : : }
2510 [ + + + - ]: 509 : if (Ct1->NumH && Ct2->NumH)
2511 : : {
2512 [ + + + - ]: 343 : for (i = midNumH; i < lenNumH && Ct1->NumH[i] == Ct2->NumH[i]; i++)
2513 : : {
2514 : : ; /* compare tautomeric H */
2515 : : }
2516 [ - + ]: 175 : if (i < lenNumH)
2517 : : {
2518 : 0 : diff = (int) Ct1->NumH[i] - (int) Ct2->NumH[i];
2519 : 0 : i += endCt1 - midCt;
2520 : 0 : goto done;
2521 : : }
2522 : : }
2523 : :
2524 : :
2525 : : /*************************************************************************/
2526 : : /************** layer 3: Fixed H atoms ***********************************/
2527 : : /*************************************************************************/
2528 : 509 : nLayer++;
2529 [ - + - - ]: 509 : if (Ct1->NumHfixed && Ct2->NumHfixed)
2530 : : {
2531 [ # # # # ]: 0 : for (i = startAt1; i < midAt && Ct1->NumHfixed[i] == Ct2->NumHfixed[i]; i++)
2532 : : {
2533 : : ;
2534 : : }
2535 [ # # ]: 0 : if (i < midAt)
2536 : : {
2537 : 0 : diff = (int) Ct1->NumHfixed[i] - (int) Ct2->NumHfixed[i];
2538 : 0 : goto done;
2539 : : }
2540 : : }
2541 : :
2542 : :
2543 : : /*************************************************************************/
2544 : : /************** layer 4: isotopic atoms H, incl. tautomeric **************/
2545 : : /*************************************************************************/
2546 : 509 : nLayer++;
2547 [ + + + + ]: 509 : if (Ct1->iso_sort_key && Ct2->iso_sort_key)
2548 : : {
2549 [ + - + + ]: 3998 : for (i = startAt1; i < endAt1 && Ct1->iso_sort_key[i] == Ct2->iso_sort_key[i]; i++)
2550 : : ;
2551 [ + - ]: 6 : if (i < endAt1)
2552 : : {
2553 [ + - ]: 6 : diff = Ct1->iso_sort_key[i] > Ct2->iso_sort_key[i] ? 1 : -1;
2554 : 6 : goto done;
2555 : : }
2556 : : }
2557 [ + - - - ]: 503 : if (Ct1->iso_exchg_atnos && Ct2->len_iso_exchg_atnos)
2558 : : {
2559 [ # # # # ]: 0 : for (i = startAt1; i < endAt1 && Ct1->iso_exchg_atnos[i] == Ct2->iso_exchg_atnos[i]; i++)
2560 : : {
2561 : : ;
2562 : : }
2563 [ # # ]: 0 : if (i < endAt1)
2564 : : {
2565 [ # # ]: 0 : diff = Ct1->iso_exchg_atnos[i] > Ct2->iso_exchg_atnos[i] ? 1 : -1;
2566 : 0 : goto done;
2567 : : }
2568 : : }
2569 : :
2570 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
2571 : :
2572 : : /*************************************************************************/
2573 : : /************** layer 6: Fixed isotopic H atoms **************************/
2574 : : /*************************************************************************/
2575 : : nLayer++;
2576 : : if (Ct1->iso_sort_key_Hfixed && Ct2->iso_sort_key_Hfixed)
2577 : : {
2578 : : for (i = startAt1; i < midAt && Ct1->iso_sort_key_Hfixed[i] == Ct2->iso_sort_key_Hfixed[i]; i++)
2579 : : {
2580 : : ;
2581 : : }
2582 : : if (i < midAt)
2583 : : {
2584 : : diff = Ct1->iso_sort_key_Hfixed[i] > Ct2->iso_sort_key_Hfixed[i] ? 1 : -1;
2585 : : goto done;
2586 : : }
2587 : : }
2588 : : #endif
2589 : :
2590 : :
2591 : :
2592 : 503 : done:
2593 : :
2594 : : #ifdef INCHI_CANON_MIN
2595 : 509 : diff = -diff;
2596 : : #endif
2597 : :
2598 [ + + ]: 509 : if (diff)
2599 : : {
2600 [ - + ]: 6 : diff = ( diff > 0 ) ? ( nLayer + 1 ) : -( nLayer + 1 ); /* return the discovered difference layer number >= 1 */
2601 [ + + ]: 6 : if (kLeastForLayer)
2602 : : {
2603 : :
2604 : : #if ( bRELEASE_VERSION != 1 )
2605 : : if (abs( kLeastForLayer[nLayer].k ) > k + 1)
2606 : : { /* for debug only */
2607 : : int stop = 1; /* <BRKPT> */
2608 : : }
2609 : : #endif
2610 : :
2611 [ + - ]: 3 : if (!kLeastForLayer[nLayer].k)
2612 : : {
2613 [ - + ]: 3 : kLeastForLayer[nLayer].k = ( diff > 0 ) ? ( k + 1 ) : -( k + 1 );
2614 : 3 : kLeastForLayer[nLayer].i = i;
2615 : : }
2616 [ + - ]: 3 : if (nLayer /* && !bOnlyCommon */)
2617 : : {
2618 : 3 : diff = 0;
2619 : : }
2620 : : }
2621 : : }
2622 : :
2623 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
2624 : : else
2625 : : {
2626 : : int stop = 1; /* for debug only */
2627 : : }
2628 : : #endif
2629 : :
2630 [ - + ]: 509 : if (cmp)
2631 : : {
2632 [ # # # # ]: 0 : cmp[k] = ( diff > 0 ) ? 1 : ( diff < 0 ) ? -1 : 0;
2633 : : }
2634 : :
2635 : 509 : return diff;
2636 : : }
2637 : :
2638 : :
2639 : : /**************************************************************************************************************/
2640 : 137 : int CtFullCompare( ConTable *Ct1,
2641 : : ConTable *Ct2,
2642 : : int bOnlyCommon,
2643 : : int bSplitTautCompare )
2644 : : {
2645 : : int startCt1, endCt1, endCt2; /*endCt,*/
2646 : : int startAt1, endAt1, endAt2; /*endCt,*/
2647 : : int midCt /* end of atoms only in Ctbl */,
2648 : 137 : midNumH = 0 /* end of atoms only NumH */,
2649 : : midAt /* end of atoms only */; /* djb-rwth: ignoring LLVM warning: variable used */
2650 : 137 : int diff = 0, i, k1, k2, lenNumH1, lenNumH2, maxVert /* min num atoms */;
2651 : : int len_iso_sort_key1, len_iso_sort_key2 /*, mid_iso_sort_key*/;
2652 : 137 : int nLayer = 0;
2653 : :
2654 : 137 : k1 = Ct1->lenPos - 1;
2655 : 137 : k2 = Ct2->lenPos - 1;
2656 : :
2657 : : /* djb-rwth: removing redundant code */
2658 : :
2659 : 137 : startCt1 = 0; /* djb-rwth: removing redundant code */
2660 : 137 : startAt1 = 0; /* djb-rwth: removing redundant code */
2661 : :
2662 : 137 : endCt1 = Ct1->nextCtblPos[k1];
2663 : 137 : endCt2 = Ct2->nextCtblPos[k2];
2664 : 137 : endAt1 = (int) Ct1->nextAtRank[k1] - 1;
2665 : 137 : endAt2 = (int) Ct2->nextAtRank[k2] - 1;
2666 : :
2667 : 137 : maxVert = inchi_min( Ct1->maxVert, Ct2->maxVert );
2668 : :
2669 [ + + ]: 137 : if (bOnlyCommon)
2670 : : {
2671 : 29 : endCt1 = inchi_min( endCt1, endCt2 );
2672 : 29 : endCt1 = endCt2 = inchi_min( endCt1, Ct1->lenCt );
2673 : 29 : endAt1 = inchi_min( endAt1, endAt2 ); /* djb-rwth: removing redundant code */
2674 : :
2675 [ + + + - ]: 29 : if (Ct1->Ctbl[endCt1] == EMPTY_CT || Ct2->Ctbl[endCt1] == EMPTY_CT) /* djb-rwth: redundant conditions removed */
2676 : : {
2677 : 29 : endCt1 = endCt2 = endCt1 - 1;
2678 : : }
2679 : : /* djb-rwth: removing redundant code */
2680 : 29 : lenNumH1 =
2681 : 29 : lenNumH2 = inchi_min( Ct1->lenNumH, Ct2->lenNumH );
2682 : :
2683 : : /* djb-rwth: removing redundant code */
2684 : 29 : len_iso_sort_key1 =
2685 : 29 : len_iso_sort_key2 = inchi_min( Ct1->len_iso_sort_key, Ct1->len_iso_sort_key );
2686 : : }
2687 : : else
2688 : : {
2689 [ - + ]: 108 : if (Ct1->Ctbl[endCt1 - 1] == EMPTY_CT)
2690 : : {
2691 : 0 : endCt1--;
2692 : : }
2693 [ - + ]: 108 : if (Ct2->Ctbl[endCt2 - 1] == EMPTY_CT)
2694 : : {
2695 : 0 : endCt2--;
2696 : : }
2697 : 108 : lenNumH1 = Ct1->lenNumH;
2698 : 108 : lenNumH2 = Ct2->lenNumH;
2699 : : /* djb-rwth: removing redundant code */
2700 : :
2701 : 108 : len_iso_sort_key1 = Ct1->len_iso_sort_key;
2702 : 108 : len_iso_sort_key2 = Ct2->len_iso_sort_key;
2703 : : /* djb-rwth: removing redundant code */
2704 : : }
2705 : :
2706 [ - + ]: 137 : if ((diff = -( endCt1 - endCt2 ))) /* djb-rwth: addressing LLVM warning */
2707 : : {
2708 : : /* negative sign reproduces results for NSC=28393 */
2709 : 0 : goto done;
2710 : : }
2711 : :
2712 [ + + ]: 137 : if (bSplitTautCompare)
2713 : : {
2714 : 48 : midCt = inchi_min( Ct1->nLenCTAtOnly, Ct2->nLenCTAtOnly );
2715 [ + + ]: 48 : if (midCt > endCt1)
2716 : : {
2717 : 29 : midCt = endCt1;
2718 : : }
2719 : 48 : midAt = inchi_min( maxVert, endAt1 ); /* djb-rwth: ignoring LLVM warning: variable used? */
2720 : : }
2721 : : else
2722 : : {
2723 : 89 : midCt = endCt1;
2724 : 89 : midAt = endAt1; /* djb-rwth: ignoring LLVM warning: variable used? */
2725 : : }
2726 : :
2727 : :
2728 : : /*************************************************************************/
2729 : : /************ layer 0: connection table without tautomeric groups ********/
2730 : : /*************************************************************************/
2731 [ + + + - ]: 20439 : for (i = startCt1; i < midCt && Ct1->Ctbl[i] == Ct2->Ctbl[i]; i++)
2732 : : {
2733 : : ;
2734 : : }
2735 [ - + ]: 137 : if (i < midCt)
2736 : : {
2737 : 0 : diff = (int) Ct1->Ctbl[i] - (int) Ct2->Ctbl[i];
2738 : 0 : goto done;
2739 : : }
2740 : :
2741 : :
2742 : : /*************************************************************************/
2743 : : /************* layer 1: H atoms without tautomeric H *********************/
2744 : : /*************************************************************************/
2745 : 137 : nLayer++;
2746 [ + + + - ]: 137 : if (Ct1->NumH && Ct2->NumH)
2747 : : {
2748 [ - + ]: 48 : if ((diff = -( lenNumH1 - lenNumH2 ))) /* djb-rwth: addressing LLVM warning */
2749 : : {
2750 : : /* negative sign reproduces results for NSC=28393 */
2751 : 0 : goto done;
2752 : : }
2753 [ - + ]: 48 : if (endAt1 < maxVert)
2754 : : {
2755 : 0 : midNumH = lenNumH1 = endAt1;
2756 : : }
2757 [ + - ]: 48 : else if (bSplitTautCompare)
2758 : : {
2759 : 48 : midNumH = maxVert;
2760 : : }
2761 : : else
2762 : : {
2763 : 0 : midNumH = lenNumH1;
2764 : : }
2765 : :
2766 [ + + + - ]: 6281 : for (i = startAt1; i < midNumH && Ct1->NumH[i] == Ct2->NumH[i]; i++)
2767 : : {
2768 : : ;
2769 : : }
2770 [ - + ]: 48 : if (i < midNumH)
2771 : : {
2772 : 0 : diff = (int) Ct1->NumH[i] - (int) Ct2->NumH[i];
2773 : 0 : goto done;
2774 : : }
2775 : : }
2776 : :
2777 : :
2778 : : /*************************************************************************/
2779 : : /************** layer 2: tautomeric part of CT and tautomeric H **********/
2780 : : /*************************************************************************/
2781 : 137 : nLayer++;
2782 [ + + + - ]: 275 : for (i = midCt; i < endCt1 && Ct1->Ctbl[i] == Ct2->Ctbl[i]; i++)
2783 : : {
2784 : : ; /* compare tautomeric groups part of CT */
2785 : : }
2786 [ - + ]: 137 : if (i < endCt1)
2787 : : {
2788 : 0 : diff = (int) Ct1->Ctbl[i] - (int) Ct2->Ctbl[i];
2789 : 0 : goto done;
2790 : : }
2791 [ + + + - ]: 137 : if (Ct1->NumH && Ct2->NumH)
2792 : : {
2793 [ + + + - ]: 132 : for (i = midNumH; i < lenNumH1 && Ct1->NumH[i] == Ct2->NumH[i]; i++)
2794 : : {
2795 : : ; /* compare tautomeric H */
2796 : : }
2797 [ - + ]: 48 : if (i < lenNumH1)
2798 : : {
2799 : 0 : diff = (int) Ct1->NumH[i] - (int) Ct2->NumH[i];
2800 : 0 : goto done;
2801 : : }
2802 : : }
2803 : :
2804 : : /*************************************************************************/
2805 : : /************** layer 3: Fixed H atoms ***********************************/
2806 : : /*************************************************************************/
2807 : 137 : nLayer++;
2808 [ - + - - ]: 137 : if (Ct1->NumHfixed && Ct2->NumHfixed)
2809 : : {
2810 [ # # # # ]: 0 : for (i = startAt1; i < endAt1 && Ct1->NumHfixed[i] == Ct2->NumHfixed[i]; i++)
2811 : : {
2812 : : ;
2813 : : }
2814 [ # # ]: 0 : if (i < endAt1)
2815 : : {
2816 : 0 : diff = (int) Ct1->NumHfixed[i] - (int) Ct2->NumHfixed[i];
2817 : 0 : goto done;
2818 : : }
2819 : : }
2820 : :
2821 : :
2822 : : /*************************************************************************/
2823 : : /************** layer 4: isotopic atoms, H and isotopic taut H ***********/
2824 : : /*************************************************************************/
2825 : 137 : nLayer++;
2826 [ + + + - ]: 137 : if (Ct1->iso_sort_key && Ct2->iso_sort_key)
2827 : : {
2828 [ # # ]: 0 : if ((diff = -( len_iso_sort_key1 - len_iso_sort_key2 ))) /* djb-rwth: addressing LLVM warning */
2829 : : {
2830 : : /* negative sign reproduces results for NSC=28393 */
2831 : 0 : goto done;
2832 : : }
2833 [ # # # # ]: 0 : for (i = startAt1; i < endAt1 && Ct1->iso_sort_key[i] == Ct2->iso_sort_key[i]; i++)
2834 : : {
2835 : : ;
2836 : : }
2837 [ # # ]: 0 : if (i < endAt1)
2838 : : {
2839 [ # # ]: 0 : diff = Ct1->iso_sort_key[i] > Ct2->iso_sort_key[i] ? 1 : -1;
2840 : 0 : goto done;
2841 : : }
2842 : : }
2843 : :
2844 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
2845 : :
2846 : :
2847 : : /*************************************************************************/
2848 : : /************** layer 6: Fixed isotopic H atoms **************************/
2849 : : /*************************************************************************/
2850 : : nLayer++;
2851 : : if (Ct1->iso_sort_key_Hfixed && Ct2->iso_sort_key_Hfixed)
2852 : : {
2853 : : for (i = startAt1; i < midAt && Ct1->iso_sort_key_Hfixed[i] == Ct2->iso_sort_key_Hfixed[i]; i++)
2854 : : ;
2855 : : if (i < midAt)
2856 : : {
2857 : : diff = Ct1->iso_sort_key_Hfixed[i] > Ct2->iso_sort_key_Hfixed[i] ? 1 : -1;
2858 : : goto done;
2859 : : }
2860 : : }
2861 : : #endif
2862 : :
2863 : :
2864 : 137 : done:
2865 : :
2866 : : #ifdef INCHI_CANON_MIN
2867 : 137 : diff = -diff;
2868 : : #endif
2869 : :
2870 [ - + ]: 137 : if (diff)
2871 : : {
2872 [ # # ]: 0 : diff = ( diff > 0 ) ? ( nLayer + 1 ) : -( nLayer + 1 ); /* return the discovered difference layer number >= 1 */
2873 : : }
2874 : :
2875 : 137 : return diff;
2876 : : }
2877 : :
2878 : :
2879 : : /****************************************************************************/
2880 : 0 : int CtFullCompareLayers( kLeast *kLeastForLayer )
2881 : : {
2882 : : int iLayer;
2883 : : /* check for the rejection condition: Lambda > zb_rho_fix */
2884 [ # # ]: 0 : for (iLayer = 0; iLayer < MAX_LAYERS; iLayer++)
2885 : : {
2886 [ # # ]: 0 : if (kLeastForLayer[iLayer].k)
2887 : : {
2888 [ # # ]: 0 : return ( kLeastForLayer[iLayer].k > 0 ) ? ( iLayer + 1 ) : -( iLayer + 1 );
2889 : : }
2890 : : }
2891 : :
2892 : 0 : return 0;
2893 : : }
2894 : :
2895 : :
2896 : : /****************************************************************************/
2897 : 277 : int CtCompareLayersGetFirstDiff( kLeast *kLeast_rho,
2898 : : int nOneAdditionalLayer,
2899 : : int *L_rho,
2900 : : int *I_rho,
2901 : : int *k_rho )
2902 : : {
2903 : : int iLayer;
2904 [ + - ]: 277 : if (kLeast_rho)
2905 : : {
2906 [ + + ]: 27689 : for (iLayer = 0; iLayer < MAX_LAYERS; iLayer++)
2907 : : {
2908 [ + + ]: 27415 : if (kLeast_rho[iLayer].k)
2909 : : {
2910 : 3 : *L_rho = iLayer;
2911 : 3 : *I_rho = kLeast_rho[iLayer].i;
2912 : 3 : *k_rho = kLeast_rho[iLayer].k;
2913 : 3 : break;
2914 : : }
2915 : : }
2916 [ + + ]: 277 : if (iLayer == MAX_LAYERS)
2917 : : {
2918 [ + + ]: 274 : if (nOneAdditionalLayer)
2919 : : {
2920 : 59 : *L_rho = nOneAdditionalLayer; /* ??? subtract 1 ??? */
2921 : 59 : *I_rho = -1;
2922 : 59 : *k_rho = 0;
2923 : 59 : return 0; /* difference may be in the first additional layer */
2924 : : }
2925 : : else
2926 : : {
2927 : 215 : *L_rho = INCHI_CANON_INFINITY;
2928 : 215 : *I_rho = -1;
2929 : 215 : *k_rho = 0;
2930 : 215 : return 0; /* no difference found */
2931 : : }
2932 : : }
2933 : : else
2934 : : {
2935 : 3 : return 1; /* difference in a real layer */
2936 : : }
2937 : : }
2938 : : else
2939 : : {
2940 : 0 : return -1; /* no input, should not happen */
2941 : : }
2942 : : }
2943 : :
2944 : :
2945 : : /****************************************************************************/
2946 : 269 : int CtPartCompareLayers( kLeast *kLeast_rho,
2947 : : int L_rho_fix_prev,
2948 : : int nOneAdditionalLayer )
2949 : : {
2950 : : int L_rho, I_rho, k_rho;
2951 [ + + ]: 269 : if (0 < CtCompareLayersGetFirstDiff( kLeast_rho, nOneAdditionalLayer, &L_rho, &I_rho, &k_rho ) &&
2952 : : /* differences has been found in a real layer or all real layers are identical */
2953 [ + - ]: 3 : L_rho <= L_rho_fix_prev)
2954 : : {
2955 : : /* in this layer pzb_rho == pzb_rho_fix or in the previous real layer */
2956 [ - + ]: 3 : return k_rho > 0 ? ( L_rho + 1 ) : -( L_rho + 1 );
2957 : : }
2958 : :
2959 : 266 : return 0;
2960 : : }
2961 : :
2962 : :
2963 : : /****************************************************************************/
2964 : 133 : void UpdateCompareLayers( kLeast kLeastForLayer[], int hzz )
2965 : : {
2966 : : int i;
2967 [ + - ]: 133 : if (kLeastForLayer)
2968 : : {
2969 [ + + ]: 13433 : for (i = 0; i < MAX_LAYERS; i++)
2970 : : {
2971 [ - + ]: 13300 : if (abs( kLeastForLayer[i].k ) >= hzz)
2972 : : {
2973 : 0 : kLeastForLayer[i].k = 0;
2974 : 0 : kLeastForLayer[i].i = 0;
2975 : : }
2976 : : }
2977 : : }
2978 : 133 : }
2979 : :
2980 : :
2981 : : /****************************************************************************/
2982 : 1523 : void CtPartCopy( ConTable *Ct1 /* to */,
2983 : : ConTable *Ct2 /* from */,
2984 : : int k )
2985 : : {
2986 : : int startCt1, startCt2, endCt2;
2987 : 1523 : int len2, len2H, len2Hfixed = 0, len2iso_sort_key, len2iso_exchg_atnos, i;
2988 : : int startAt1, startAt2, endAt2; /*endCt,*/
2989 : :
2990 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
2991 : : int len2iso_sort_key_Hfixed;
2992 : : #endif
2993 : :
2994 : 1523 : k--;
2995 : :
2996 [ + + ]: 1523 : if (k)
2997 : : {
2998 : 139 : startCt1 = Ct1->nextCtblPos[k - 1];
2999 : 139 : startCt2 = Ct2->nextCtblPos[k - 1];
3000 : 139 : startAt1 = Ct1->nextAtRank[k - 1] - 1;
3001 : 139 : startAt2 = Ct2->nextAtRank[k - 1] - 1;
3002 : : }
3003 : : else
3004 : : {
3005 : 1384 : startCt1 = startCt2 = 0;
3006 : 1384 : startAt1 = startAt2 = 0;
3007 : : }
3008 : :
3009 : : /* djb-rwth: removing redundant code */
3010 : 1523 : endCt2 = Ct2->nextCtblPos[k];
3011 : : /* djb-rwth: removing redundant code */
3012 : 1523 : endAt2 = (int) Ct2->nextAtRank[k] - 1;
3013 : :
3014 : 1523 : len2 = endCt2 - startCt2;
3015 : : /* len = min(len1, len2); */
3016 : :
3017 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
3018 : : if (startCt1 != startCt2 || startAt1 != startAt2)
3019 : : {
3020 : : int stop = 1;
3021 : : }
3022 : : #endif
3023 : :
3024 : : /* copy connection table: Ctbl */
3025 [ + + ]: 48553 : for (i = 0; i < len2; i++)
3026 : : {
3027 : 47030 : Ct1->Ctbl[startCt1 + i] = Ct2->Ctbl[startCt2 + i];
3028 : : }
3029 : :
3030 : : /* copy number of H: NumH */
3031 : 1523 : len2H = 0;
3032 [ + + + + ]: 1523 : if (Ct1->NumH && Ct2->NumH)
3033 : : {
3034 : 51 : len2H = endAt2 - startAt2;
3035 [ + + ]: 51 : if (endAt2 > Ct2->maxVert)
3036 : : {
3037 : 10 : len2H = Ct2->lenNumH - startAt2;
3038 : : }
3039 [ + + ]: 8170 : for (i = 0; i < len2H; i++)
3040 : : {
3041 : 8119 : Ct1->NumH[startAt1 + i] = Ct2->NumH[startAt2 + i];
3042 : : }
3043 : : }
3044 : :
3045 : : /* copy number of fixed H */
3046 : : /* djb-rwth: removing redundant code */
3047 [ - + - - ]: 1523 : if (Ct1->NumHfixed && Ct2->NumHfixed)
3048 : : {
3049 : 0 : len2Hfixed = endAt2 - startAt2;
3050 [ # # ]: 0 : for (i = 0; i < len2Hfixed; i++)
3051 : : {
3052 : 0 : Ct1->NumHfixed[startAt1 + i] = Ct2->NumHfixed[startAt2 + i];
3053 : : }
3054 : : }
3055 : :
3056 : : /* copy isotopic keys */
3057 : 1523 : len2iso_sort_key = 0;
3058 [ + + + + ]: 1523 : if (Ct1->iso_sort_key && Ct2->iso_sort_key)
3059 : : {
3060 : 6 : len2iso_sort_key = endAt2 - startAt2;
3061 [ + + ]: 6006 : for (i = 0; i < len2iso_sort_key; i++)
3062 : : {
3063 : 6000 : Ct1->iso_sort_key[startAt1 + i] = Ct2->iso_sort_key[startAt2 + i];
3064 : : }
3065 : : }
3066 : :
3067 : 1523 : len2iso_exchg_atnos = 0;
3068 [ - + - - ]: 1523 : if (Ct1->iso_exchg_atnos && Ct2->iso_exchg_atnos)
3069 : : {
3070 : 0 : len2iso_exchg_atnos = endAt2 - startAt2;
3071 [ # # ]: 0 : for (i = 0; i < len2iso_exchg_atnos; i++)
3072 : : {
3073 : 0 : Ct1->iso_exchg_atnos[startAt1 + i] = Ct2->iso_exchg_atnos[startAt2 + i];
3074 : : }
3075 : : }
3076 : :
3077 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
3078 : : len2iso_sort_key_Hfixed = 0;
3079 : : if (Ct1->iso_sort_key_Hfixed && Ct2->iso_sort_key_Hfixed)
3080 : : {
3081 : : len2iso_sort_key_Hfixed = endAt2 - startAt2;
3082 : : for (i = 0; i < len2iso_sort_key; i++)
3083 : : {
3084 : : Ct1->iso_sort_key_Hfixed[startAt1 + i] = Ct2->iso_sort_key_Hfixed[startAt2 + i];
3085 : : }
3086 : : }
3087 : : #endif
3088 : :
3089 : 1523 : Ct1->lenCt = startCt1 + len2;
3090 : 1523 : Ct1->nextCtblPos[k] = startCt1 + len2;
3091 : 1523 : Ct1->nextAtRank[k] = Ct2->nextAtRank[k];
3092 [ + + ]: 1523 : if (len2H)
3093 : : {
3094 : 51 : Ct1->lenNumH = startAt1 + len2H;
3095 : : }
3096 : :
3097 : : /*
3098 : : if ( len2Hfixed )
3099 : : {
3100 : : Ct1->lenNumHfixed = startAt1 + len2Hfixed;
3101 : : }
3102 : : */
3103 : :
3104 [ + + ]: 1523 : if (len2iso_sort_key)
3105 : : {
3106 : 6 : Ct1->len_iso_sort_key = startAt1 + len2iso_sort_key;
3107 : : }
3108 [ - + ]: 1523 : if (len2iso_exchg_atnos)
3109 : : {
3110 : 0 : Ct1->len_iso_exchg_atnos = startAt1 + len2iso_exchg_atnos;
3111 : : }
3112 : :
3113 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
3114 : : if (len2iso_sort_key_Hfixed)
3115 : : {
3116 : : Ct1->len_iso_sort_key_Hfixed = startAt1 + len2iso_sort_key_Hfixed;
3117 : : }
3118 : : #endif
3119 : :
3120 : : #ifdef INCHI_CANON_USE_HASH
3121 : : Ct1->hash[k] = Ct2->hash[k];
3122 : : #endif
3123 : :
3124 : 1523 : Ct1->lenPos = k + 1;
3125 : : INCHI_HEAPCHK
3126 : 1523 : }
3127 : :
3128 : :
3129 : : /****************************************************************************/
3130 : 1274 : void CtFullCopy( ConTable *Ct1, ConTable *Ct2 )
3131 : : {
3132 : : /* Ct1 does not have INCHI_CANON_INFINITY termination */
3133 : : int k;
3134 [ + + ]: 2585 : for (k = 0; k < Ct2->lenPos; k++)
3135 : : {
3136 : 1311 : CtPartCopy( Ct1 /* to */, Ct2 /* from */, k + 1 );
3137 : : }
3138 : 1274 : }
3139 : :
3140 : :
3141 : : /****************************************************************************/
3142 : 108 : void TranspositionGetMcrAndFixSetAndUnorderedPartition( CANON_GLOBALS *pCG,
3143 : : Transposition *gamma,
3144 : : NodeSet *McrSet,
3145 : : NodeSet *FixSet,
3146 : : int n,
3147 : : int l,
3148 : : UnorderedPartition *p )
3149 : : {
3150 : : int i, j, k, mcr;
3151 : : AT_RANK next;
3152 : 108 : bitWord *McrBits = McrSet->bitword[l - 1];
3153 : 108 : bitWord *FixBits = FixSet->bitword[l - 1];
3154 : 108 : int len = McrSet->len_set * sizeof( bitWord );
3155 : :
3156 : 108 : memset( McrBits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
3157 : 108 : memset( FixBits, 0, len ); /* djb-rwth: memset_s C11/Annex K variant? */
3158 [ + + ]: 4236 : for (i = 0; i < n; i++)
3159 : : {
3160 : 4128 : p->equ2[i] = INCHI_CANON_INFINITY; /* for debug only */
3161 : : }
3162 : :
3163 [ + + ]: 4236 : for (i = 0; i < n; i++)
3164 : : {
3165 : 4128 : j = (int) ( next = gamma->nAtNumb[i] );
3166 : :
3167 [ + + ]: 4128 : if (j == i)
3168 : : {
3169 : 670 : FixBits[i / pCG->m_num_bit] |= pCG->m_bBit[i % pCG->m_num_bit];
3170 : 670 : McrBits[i / pCG->m_num_bit] |= pCG->m_bBit[i % pCG->m_num_bit];
3171 : : /* p->next[i] = INCHI_CANON_INFINITY; */ /* no link to same orbit points */
3172 : 670 : p->equ2[i] = next; /* fixed point */
3173 : : }
3174 [ + + ]: 3458 : else if (!( rank_mark_bit & next ))
3175 : : {
3176 : 1729 : gamma->nAtNumb[i] |= rank_mark_bit;
3177 : 1729 : mcr = inchi_min( j, i );
3178 : : /* djb-rwth: removing redundant code */
3179 : : /* mark all nodes in the cycle to ignore later; find mcr */
3180 [ + + ]: 3458 : while (!( rank_mark_bit & ( next = gamma->nAtNumb[j] ) ))
3181 : : {
3182 : 1729 : gamma->nAtNumb[j] |= rank_mark_bit;
3183 : 1729 : j = (int) next;
3184 [ - + ]: 1729 : if (mcr > j)
3185 : : {
3186 : 0 : mcr = j;
3187 : : }
3188 : : /* djb-rwth: removing redundant code */
3189 : : }
3190 : 1729 : McrBits[mcr / pCG->m_num_bit] |= pCG->m_bBit[mcr % pCG->m_num_bit]; /* save mcr */
3191 : : /* fill out the unordered partition, the mcr first, other in the cycle after that */
3192 : 1729 : p->equ2[mcr] = mcr;
3193 [ + + ]: 3458 : for (k = mcr; mcr != ( j = (int) ( rank_mask_bit & gamma->nAtNumb[k] ) ); k = j)
3194 : : {
3195 : 1729 : p->equ2[j] = mcr;
3196 : : }
3197 : : }
3198 : : }
3199 : :
3200 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
3201 : : /* for debug only */
3202 : : for (i = 0; i < n; i++)
3203 : : {
3204 : : if (p->equ2[i] >= n)
3205 : : {
3206 : : int stop = 1;
3207 : : }
3208 : : }
3209 : : #endif
3210 : :
3211 : : /* remove the marks */
3212 [ + + ]: 4236 : for (i = 0; i < n; i++)
3213 : : {
3214 : 4128 : gamma->nAtNumb[i] &= rank_mask_bit;
3215 : : }
3216 : :
3217 : : INCHI_HEAPCHK
3218 : 108 : }
3219 : :
3220 : :
3221 : : /****************************************************************************/
3222 : 1288 : int SetBitCreate( CANON_GLOBALS *pCG )
3223 : : {
3224 : 1288 : bitWord b1 = 1, b2;
3225 : : AT_NUMB n1, n2;
3226 : : #ifdef INCHI_CANON_USE_HASH
3227 : : CtHash h1, h2;
3228 : : #endif
3229 : : int i;
3230 : :
3231 [ + + ]: 1288 : if (pCG->m_bBitInitialized)
3232 : : {
3233 : : INCHI_HEAPCHK
3234 : 1115 : return 0; /* already created */
3235 : : }
3236 : :
3237 : : /* djb-rwth: removing redundant code */
3238 : 173 : pCG->m_num_bit = 1;
3239 [ + + ]: 2768 : for (b1 = 1, pCG->m_num_bit = 1; b1 < ( b2 = (bitWord) ( ( b1 << 1 )& BIT_WORD_MASK ) ); b1 = b2, pCG->m_num_bit++)
3240 : : {
3241 : : ;
3242 : : }
3243 : 173 : pCG->m_bBit = (bitWord*) inchi_calloc( pCG->m_num_bit, sizeof( bitWord ) );
3244 [ - + ]: 173 : if (!pCG->m_bBit)
3245 : : {
3246 : : INCHI_HEAPCHK
3247 : 0 : return -1; /* failed */
3248 : : }
3249 [ + + ]: 2941 : for (i = 0, b1 = 1; i < pCG->m_num_bit; i++, b1 <<= 1)
3250 : : {
3251 : 2768 : pCG->m_bBit[i] = b1;
3252 : : }
3253 : :
3254 [ + + ]: 2768 : for (n1 = 1; n1 < ( n2 = (AT_RANK) ( ( n1 << 1 )& AT_RANK_MASK ) ); n1 = n2)
3255 : : {
3256 : : ;
3257 : : }
3258 : 173 : rank_mark_bit = n1;
3259 : 173 : rank_mask_bit = ~n1;
3260 : :
3261 : : #ifdef INCHI_CANON_USE_HASH
3262 : : for (h1 = 1; h1 < ( h2 = ( h1 << 1 ) ); h1 = h2)
3263 : : {
3264 : : ;
3265 : : }
3266 : : hash_mark_bit = h1;
3267 : : #endif
3268 : 173 : pCG->m_bBitInitialized = 1;
3269 : : INCHI_HEAPCHK
3270 : :
3271 : 173 : return 1;
3272 : : }
3273 : :
3274 : :
3275 : : /****************************************************************************/
3276 : 208 : int SetBitFree( CANON_GLOBALS *pCG )
3277 : : {
3278 [ + + ]: 208 : if (pCG->m_bBit)
3279 : : {
3280 [ + - ]: 173 : inchi_free( pCG->m_bBit );
3281 : 173 : pCG->m_bBit = NULL;
3282 : : INCHI_HEAPCHK
3283 : 173 : return 1; /* success */
3284 : : }
3285 : :
3286 : : INCHI_HEAPCHK
3287 : :
3288 : 35 : return 0; /* already destroyed */
3289 : : }
3290 : :
3291 : :
3292 : :
3293 : : #ifdef NEVER /* { how to renumber a graph */
3294 : : /****************************************************************************/
3295 : : void RenumberTheGraph( int n,
3296 : : NEIGH_LIST *NeighList,
3297 : : AT_NUMB *old2new,
3298 : : AT_NUMB *new2old,
3299 : : S_CHAR *mark,
3300 : : int bDone )
3301 : : {
3302 : : int i, k, j;
3303 : : NEIGH_LIST nl;
3304 : :
3305 : : /* renumber neighbors */
3306 : : for (i = 0; i < n; i++)
3307 : : {
3308 : : for (j = 1; j <= NeighList[i][0]; j++)
3309 : : {
3310 : : NeighList[i][j] = old2new[NeighList[i][j]];
3311 : : }
3312 : : }
3313 : :
3314 : : /* rearrange NeighList in situ using new2old[] */
3315 : : for (k = 0; k < n; k++)
3316 : : {
3317 : : if (mark[k] & bDone)
3318 : : continue;
3319 : : if (k == ( j = new2old[k] ))
3320 : : {
3321 : : mark[k] |= bDone;
3322 : : continue;
3323 : : }
3324 : : /* transposition cycle */
3325 : : i = k;
3326 : : nl = NeighList[k];
3327 : : do
3328 : : {
3329 : : NeighList[i] = NeighList[j];
3330 : : mark[i] |= bDone;
3331 : : i = j;
3332 : : }
3333 : : while (k != ( j = new2old[i] ));
3334 : : NeighList[i] = nl;
3335 : : mark[i] |= bDone;
3336 : : }
3337 : :
3338 : : #ifdef NEVER
3339 : : /* rearrange NeighList in situ using old2new[] */
3340 : : s = 0;
3341 : : for (k = 0; k < n; k++)
3342 : : {
3343 : : if (mark[k] & bDone)
3344 : : continue;
3345 : : if (k == ( j = old2new[k] ))
3346 : : {
3347 : : mark[k] |= bDone;
3348 : : continue;
3349 : : }
3350 : : /* transposition cycle */
3351 : : i = k;
3352 : : /* NeighList[j] goes to ith position */
3353 : : nl2[s] = NeighList[j];
3354 : : s ^= 1;
3355 : : do
3356 : : {
3357 : : nl2[s] = NeighList[i];
3358 : : NeighList[i] = nl2[s ^= 1];
3359 : : mark[i] |= bDone;
3360 : : i = j;
3361 : : j = old2new[i];
3362 : : }
3363 : : while (k != ( j = old2new[i] ));
3364 : : NeighList[j] = nl2[s ^= 1];
3365 : : mark[j] |= bDone;
3366 : : }
3367 : : #endif
3368 : :
3369 : : }
3370 : :
3371 : :
3372 : : /****************************************************************************/
3373 : : void RearrangeAtRankArray( int n,
3374 : : AT_RANK *nRank,
3375 : : AT_NUMB *new2old,
3376 : : S_CHAR *mark,
3377 : : int bDone )
3378 : : {
3379 : : int i, k, j;
3380 : : AT_RANK r;
3381 : : /* rearrange the array in situ using new2old[] */
3382 : : for (k = 0; k < n; k++)
3383 : : {
3384 : : if (mark[k] & bDone)
3385 : : {
3386 : : continue;
3387 : : }
3388 : : if (k == ( j = new2old[k] ))
3389 : : {
3390 : : mark[k] |= bDone;
3391 : : continue;
3392 : : }
3393 : : /* transposition cycle */
3394 : : i = k;
3395 : : r = nRank[k];
3396 : : do
3397 : : {
3398 : : nRank[i] = nRank[j];
3399 : : mark[i] |= bDone;
3400 : : i = j;
3401 : : }
3402 : : while (k != ( j = new2old[i] ));
3403 : : nRank[i] = r;
3404 : : mark[i] |= bDone;
3405 : : }
3406 : :
3407 : : }
3408 : :
3409 : :
3410 : : /****************************************************************************/
3411 : : void RenumberAtNumbArray( int n, AT_NUMB *nAtNumb, AT_NUMB *old2new )
3412 : : {
3413 : : int i;
3414 : : for (i = 0; i < n; i++)
3415 : : {
3416 : : nAtNumb[i] = old2new[nAtNumb[i]];
3417 : : }
3418 : : }
3419 : :
3420 : :
3421 : : /****************************************************************************/
3422 : : int GetCanonRanking2( int num_atoms,
3423 : : int num_at_tg,
3424 : : int num_max,
3425 : : int bDigraph,
3426 : : sp_ATOM* at,
3427 : : AT_RANK **pRankStack,
3428 : : int nNumPrevRanks,
3429 : : AT_RANK *nSymmRank,
3430 : : AT_RANK *nCanonRank,
3431 : : NEIGH_LIST *NeighList,
3432 : : AT_RANK *nTempRank,
3433 : : CANON_STAT* pCS )
3434 : : {
3435 : : void *pzb_rho = NULL;
3436 : : int ret, cmp1 = 0, cmp2 = 0;
3437 : :
3438 : : int i, j, k, n;
3439 : : AT_NUMB *old2new = NULL, *new2old = NULL, m, r1, r2;
3440 : : S_CHAR *mark = NULL;
3441 : :
3442 : : int nMaxLenCt = pCS->nMaxLenLinearCT;
3443 : : AT_RANK *pCt = pCS->LinearCT;
3444 : : int nLenCt = pCS->nLenLinearCT;
3445 : : AT_RANK *pCt0 = pCS->LinearCT;
3446 : : int nLenCt0 = pCS->nLenLinearCT;
3447 : :
3448 : :
3449 : : CANON_DATA CanonData;
3450 : : CANON_DATA *pCD = &CanonData;
3451 : : CANON_COUNTS CanonCounts;
3452 : : CANON_COUNTS *pCC = &CanonCounts;
3453 : : memset( pCD, 0, sizeof( pCD[0] ) );
3454 : : memset( pCC, 0, sizeof( pCC[0] ) );
3455 : : /* pointers */
3456 : : pCD->LinearCT = pCS->LinearCT;
3457 : : /* variables - unchanged */
3458 : : pCD->ulTimeOutTime = pCS->ulTimeOutTime;
3459 : : pCD->nMaxLenLinearCT = pCS->nMaxLenLinearCT;
3460 : : /* return values & input/output */
3461 : : pCD->nLenLinearCT = pCS->nLenLinearCT;
3462 : :
3463 : : pCC->lNumBreakTies = pCS->lNumBreakTies;
3464 : : pCC->lNumDecreasedCT = pCS->lNumDecreasedCT;
3465 : : pCC->lNumRejectedCT = pCS->lNumRejectedCT;
3466 : : pCC->lNumEqualCT = pCS->lNumEqualCT;
3467 : : pCC->lNumTotCT = pCS->lNumTotCT;
3468 : :
3469 : : ret = CanonGraph( ic, pCG, num_atoms, num_at_tg, num_max, bDigraph,
3470 : : NeighList, (Partition *) pRankStack, nSymmRank,
3471 : : nCanonRank, pCS->nPrevAtomNumber, pCD, pCC, NULL,
3472 : : &pzb_rho, LargeMolecules );
3473 : :
3474 : : pCS->nLenLinearCT = pCD->nLenLinearCT;
3475 : : pCS->lNumBreakTies = pCC->lNumBreakTies;
3476 : : pCS->lNumDecreasedCT = pCC->lNumDecreasedCT;
3477 : : pCS->lNumRejectedCT = pCC->lNumRejectedCT;
3478 : : pCS->lNumEqualCT = pCC->lNumEqualCT;
3479 : : pCS->lNumTotCT = pCC->lNumTotCT;
3480 : :
3481 : :
3482 : : /* save the connection table for comparison with the 2nd one */
3483 : : pCt0 = (AT_RANK*) inchi_calloc( nMaxLenCt, sizeof( pCt0[0] ) );
3484 : : memcpy( pCt0, pCS->LinearCT, nMaxLenCt * sizeof( pCt0[0] ) );
3485 : : nLenCt0 = pCS->nLenLinearCT;
3486 : :
3487 : : /**********************************************************/
3488 : : /* rearrange numbering to make canon. numbering the first */
3489 : : /**********************************************************/
3490 : : n = num_at_tg;
3491 : : /* 1. get transpositions */
3492 : : old2new = (AT_NUMB*) inchi_calloc( n, sizeof( old2new[0] ) );
3493 : : new2old = (AT_NUMB*) inchi_calloc( n, sizeof( new2old[0] ) );
3494 : : mark = (S_CHAR *) inchi_calloc( n, sizeof( mark[0] ) );
3495 : : for (i = 0; i < n; i++)
3496 : : {
3497 : : /* forward transposition: at[i] -> at[old2new[i]] position */
3498 : : old2new[i] = m = nCanonRank[i] - 1;
3499 : : /* forward transposition: at[new2old[i]] -> at[i] position */
3500 : : new2old[m] = i;
3501 : : }
3502 : : /* rearrange input data according to the new numbering */
3503 : : RenumberTheGraph( n, NeighList, old2new, new2old, mark, 1 );
3504 : : RearrangeAtRankArray( n, pRankStack[0], new2old, mark, 2 );
3505 : : RenumberAtNumbArray( n, pRankStack[1], old2new );
3506 : : /* make sure the atom numbers are sorted */
3507 : : for (i = k = 0, r1 = pRankStack[0][pRankStack[1][i]]; i < n; r1 = r2)
3508 : : {
3509 : : for (j = i++; i < n && r1 == ( r2 = pRankStack[0][pRankStack[1][i]] ); i++)
3510 : : {
3511 : : ;
3512 : : }
3513 : : if (i - j > 1)
3514 : : {
3515 : : k += insertions_sort_AT_RANK( pRankStack[1] + j, i - j );
3516 : : }
3517 : : }
3518 : :
3519 : : ret = CanonGraph( ic, pCG, num_atoms, num_at_tg, num_max,
3520 : : bDigraph, NeighList, (Partition *) pRankStack,
3521 : : nSymmRank, nCanonRank,
3522 : : NULL, LargeMolecules );
3523 : :
3524 : : pCS->nLenLinearCT = pCD->nLenLinearCT;
3525 : : pCS->lNumBreakTies = pCC->lNumBreakTies;
3526 : : pCS->lNumDecreasedCT = pCC->lNumDecreasedCT;
3527 : : pCS->lNumRejectedCT = pCC->lNumRejectedCT;
3528 : : pCS->lNumEqualCT = pCC->lNumEqualCT;
3529 : : pCS->lNumTotCT = pCC->lNumTotCT;
3530 : :
3531 : :
3532 : : /* compare the connection tables */
3533 : : cmp1 = nLenCt0 - pCS->nLenLinearCT;
3534 : : cmp2 = memcmp( pCt0, pCS->LinearCT, pCS->nLenLinearCT * sizeof( pCt0[0] ) );
3535 : :
3536 : : #ifdef _DEBUG
3537 : : if (cmp1 || cmp2)
3538 : : {
3539 : : int stop = 1;
3540 : : }
3541 : : #endif
3542 : :
3543 : : /**********************************************************/
3544 : : /* rearrange numbering back to the original numbering */
3545 : : /**********************************************************/
3546 : :
3547 : : /* restore the input data to its original numbering */
3548 : :
3549 : : RenumberTheGraph( n, NeighList, new2old, old2new, mark, 4 );
3550 : :
3551 : : RearrangeAtRankArray( n, pRankStack[0], old2new, mark, 8 );
3552 : :
3553 : : RenumberAtNumbArray( n, pRankStack[1], new2old );
3554 : :
3555 : : /* rearrange the output data to the original numbering */
3556 : :
3557 : : RearrangeAtRankArray( n, nCanonRank, old2new, mark, 16 );
3558 : : RenumberAtNumbArray( n, pCS->nPrevAtomNumber, new2old );
3559 : : RearrangeAtRankArray( n, nSymmRank, old2new, mark, 32 );
3560 : :
3561 : : /* free memory */
3562 : : CTableFree( pzb_rho );
3563 : : if (pzb_rho)
3564 : : {
3565 : : inchi_free( pzb_rho );
3566 : : }
3567 : :
3568 : : inchi_free( old2new );
3569 : : inchi_free( new2old );
3570 : : inchi_free( mark );
3571 : : inchi_free( pCt0 );
3572 : :
3573 : : return ret;
3574 : : }
3575 : :
3576 : : #endif /* } */
3577 : :
3578 : :
3579 : : #define QZFIX_OK(X) ((X)<=0)
3580 : :
3581 : :
3582 : : /****************************************************************************/
3583 : 1288 : int GetOneAdditionalLayer( CANON_DATA *pCD, ConTable *pzb_rho_fix )
3584 : : {
3585 : 1288 : int nLastLayer = -1, nNumLast = 0, nLayer = 0;
3586 : :
3587 [ + - + + ]: 1288 : if (!pCD || !pzb_rho_fix)
3588 : : {
3589 : 1267 : return 0;
3590 : : }
3591 : :
3592 : 21 : nLayer++; /* 1 */
3593 [ + - - + ]: 21 : if (pCD->NumH && !pzb_rho_fix->NumH)
3594 : : {
3595 : 0 : nLastLayer = nLayer;
3596 : 0 : nNumLast++;
3597 : : }
3598 : :
3599 : 21 : nLayer++; /* 2 */
3600 [ + + + - ]: 21 : if (pCD->nLenCTAtOnly < pCD->nLenLinearCT && pzb_rho_fix->nLenCTAtOnly == pzb_rho_fix->lenCt)
3601 : : {
3602 : 5 : nLastLayer = nLayer;
3603 : 5 : nNumLast++;
3604 : : }
3605 : :
3606 : 21 : nLayer++; /* 3 */
3607 [ - + - - ]: 21 : if (pCD->NumHfixed && !pzb_rho_fix->NumHfixed)
3608 : : {
3609 : 0 : nLastLayer = nLayer;
3610 : 0 : nNumLast++;
3611 : : }
3612 : :
3613 : 21 : nLayer++; /* 4 */
3614 [ + + + - ]: 21 : if (pCD->iso_sort_key && !pzb_rho_fix->iso_sort_key)
3615 : : {
3616 : 3 : nLastLayer = nLayer;
3617 : 3 : nNumLast++;
3618 : : }
3619 : :
3620 : : /*
3621 : : nLayer ++; // 5
3622 : : if ( pCD->nLenCTAtOnly < pCD->nLenLinearCT && pCD->iso_sort_key &&
3623 : : (pzb_rho_fix->nLenCTAtOnly == pzb_rho_fix->lenCt || !pzb_rho_fix->iso_sort_key ) )
3624 : : {
3625 : : nLastLayer = nLayer;
3626 : : nNumLast ++;
3627 : : }
3628 : : */
3629 : :
3630 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
3631 : :
3632 : : nLayer++; /* 6 */
3633 : : if (pCD->iso_sort_key_Hfixed && !pzb_rho_fix->iso_sort_key_Hfixed)
3634 : : {
3635 : : nLastLayer = nLayer;
3636 : : nNumLast++;
3637 : : }
3638 : : #endif
3639 [ + + ]: 21 : if (1 == nNumLast)
3640 : : {
3641 : 8 : return nLastLayer;
3642 : : }
3643 : :
3644 : 13 : return 0;
3645 : : }
3646 : :
3647 : :
3648 : : /*#define QZFIX_OK(X) (!(X))*/
3649 : :
3650 : :
3651 : :
3652 : : /********************** CanonGraph *************************************
3653 : : * A naive implementation of graph canonical numbering algorithm *
3654 : : * from "Practical Graph Isomorphism" by Brendan D. McKay, *
3655 : : * Congressus Numerantium, Vol. 30 (1981), pp. 45 - 87. *
3656 : : * Note: Several typos fixed, added chem. struct. specifics *
3657 : : ***********************************************************************/
3658 : :
3659 : : /* on entry: pi[0] is equitable */
3660 : : /*******************************************************10/21/2003******
3661 : : * Later add optimization: if Aut(G) <= Aut(G0) due to some additional *
3662 : : * layer of coloring applied to G and the following is known about G0: * *
3663 : : * *
3664 : : * 0) canonical numbering of G should be same as that of G0 *
3665 : : * *
3666 : : * 1) canonical numbering as v= v0[n] {vertex number v from *
3667 : : * G0 canonical number n) *
3668 : : * 2) orbits of Aut(G0) as UnorderedPartition theta0 *
3669 : : * *
3670 : : * then when choosing next v[i] for refining the partition consider *
3671 : : * only vertices from the Aut(G0) orbit of V(i), that is, only such *
3672 : : * v[i] that: *
3673 : : * *
3674 : : * GetUnorderedPartitionMcrNode( &theta0, v[i] ) == *
3675 : : * GetUnorderedPartitionMcrNode( &theta0, v0[i] ) *
3676 : : ***********************************************************************/
3677 : :
3678 : :
3679 : 1288 : int CanonGraph( INCHI_CLOCK *ic,
3680 : : CANON_GLOBALS *pCG,
3681 : : int n,
3682 : : int n_tg,
3683 : : int n_max,
3684 : : int bDigraph,
3685 : : Graph *G,
3686 : : Partition pi[],
3687 : : AT_RANK *nSymmRank,
3688 : : AT_RANK *nCanonRank,
3689 : : AT_NUMB *nAtomNumberCanon,
3690 : : CANON_DATA *pCD,
3691 : : CANON_COUNTS *pCC,
3692 : : ConTable **pp_zb_rho_inp,
3693 : : ConTable **pp_zb_rho_out,
3694 : : int LargeMolecules )
3695 : : {
3696 : :
3697 : : /* bDigraph != 0
3698 : : means consider edges from atoms to t-groups
3699 : : as directed, that is, do not include
3700 : : t-group ranks in comparing neighbors
3701 : : when refining partition
3702 : : */
3703 : :
3704 : : /* Always set
3705 : : lab = true
3706 : : dig = true
3707 : : */
3708 : :
3709 : : /* in the comments:
3710 : : m = |zeta|
3711 : : r = |rho|
3712 : :
3713 : : m < n or r < n in case pi[k] in P (i.e. satisfies Lemma 2.25)
3714 : :
3715 : : Just after passing point B:
3716 : : ===========================
3717 : : K = k-1
3718 : : wi = v[i], i = 1..K
3719 : : Gamma(0) = Gamma = Aut(G)pi
3720 : : Gamma(i) = Gamma(w1,w2,...,wi) pointwise stabilizer for i=1..K
3721 : : zeta is a terminal node =>
3722 : : the coarsest equitable partition that fixes w1,...,wK is discrete =>
3723 : : Gamma(K)=1
3724 : : At point A only:
3725 : : index = |Gamma(k-1)|/|Gamma(k)|
3726 : : At points A and B:
3727 : : size = |Gamma(k-1)|
3728 : : theta = theta(Gamma(k-1));
3729 : : Gamma(k-1) = <Y>, where Y is the set of all automprhisms output up
3730 : : to the present stage (in Step 10 = L10 )
3731 : : |Y| <= n - |theta|
3732 : : */
3733 : :
3734 : 1288 : AT_RANK *pCt = pCD->LinearCT;
3735 : : /*int nMaxLenCt = pCD->nMaxLenLinearCT;*/
3736 : 1288 : int *nLenCt = &pCD->nLenLinearCT;
3737 : 1288 : CANON_DATA *pCD1 = pCD;
3738 : :
3739 : 1288 : int i, k, k2, index, l, ok, ret = 0, res;
3740 : : int t_Lemma; /* hh: if pi[k-1] satisfies Lemma 2-25 then
3741 : : t_Lemma = min{i| i=1..k && pi[i-1] satisfies Lemma 2-25}*/
3742 : : /* otherwise t_Lemma = k --> here this is always the case */
3743 : : int t_eq_zeta; /* ht: min{i|i=1..m && all terminal modes descended from or equal
3744 : : to zeta(i) have been shown to be equivalent}. */
3745 : : int h_zeta; /* h: the longest common ancestor of zeta and nu is nu(h_zeta) */
3746 : : int h_rho; /* hb: the longest common ancestor of rho and nu is nu(h_rho) */
3747 : : int hz_rho; /* hzb: max{i|i=1..min(k,r) && Lambda(G,pi,nu(i)) == Lambda(G,pi,rho(i))} */
3748 : : int hz_zeta; /* hzf: max{i|i=1..min(k,m) && Lambda(G,pi,nu(i)) == Lambda(G,pi,zeta(i))} */
3749 : : int qzb_rho; /* Ct(Lambda[k]) - Ct(rho[k]) */
3750 : : double size; /* |Aut(G)| */
3751 : :
3752 : :
3753 : 1288 : int nNumLayers = ( NULL != pCD->NumH ) + ( NULL != pCD->NumHfixed ) +
3754 : : /* (bDigraph && pCD->nLenLinearCT > pCD->nLenCTAtOnly)*/ /* ??? tautomeric */
3755 : 1288 : ( NULL != pCD->iso_sort_key )
3756 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
3757 : : + ( NULL != pCD->iso_sort_key_Hfixed )
3758 : : #endif
3759 : : ;
3760 : :
3761 : :
3762 [ + + - + ]: 1288 : int dig = ( bDigraph || nNumLayers );
3763 [ + + - + ]: 1288 : int bSplitTautCompare = ( bDigraph || nNumLayers ); /* compare taut. H and tgroups connections after H */
3764 : : /* digraph: 1=>do not use Lemma 2.25, 0 => use */
3765 : 1288 : int lab = 1; /* label: 1=>find canonical numbering;
3766 : : 0=>do not find canonical numbering, do not use rho */
3767 : : int r; /* |rho| */
3768 : 1288 : int bZetaEqRho = lab;
3769 : : int bZetaIsomorph;
3770 : :
3771 : : /*const int L = MAX_SET_SIZE;*/
3772 : 1288 : int L_curr_max_set_size = MAX_SET_SIZE;
3773 : :
3774 : : UnorderedPartition theta, theta_from_gamma;
3775 : :
3776 : : Cell *W; /* W[i] is the first non-trivial cell of pi[i+1] */
3777 : : Node *v; /* v[i] is in W[i] to create T(G,pi,nu[i+1]) */
3778 : : Node tvc, tvh;
3779 : 1288 : S_CHAR *e, *qzb = NULL; /* qzb = NULL always */
3780 : :
3781 : : /* current node CT */
3782 : : ConTable Lambda;
3783 : : /* first leaf CT */
3784 : : ConTable zf_zeta; /* Ct for zeta, the first discovered terminal node */
3785 : : /* best leaf/node CT: find the greatest pzb_rho possibly subject to pzb_rho[k] <= pzb_rho_fix[k] condition */
3786 : 1288 : ConTable *pzb_rho = NULL; /* Ct for rho, the best discovered terminal node */
3787 : : /* fixed input CT: for all k pzb_rho[k] <= pzb_rho_fix[k]; at the end pzb_rho == pzb_rho_fix */
3788 [ + + + - ]: 1288 : ConTable *pzb_rho_fix = ( pp_zb_rho_inp && *pp_zb_rho_inp ) ? *pp_zb_rho_inp : NULL;
3789 : :
3790 : : NodeSet Omega; /* MAX_SET_SIZE */
3791 : : NodeSet Phi; /* MAX_SET_SIZE */
3792 : : NodeSet cur_nodes; /* 1 each */
3793 : :
3794 : : Transposition gamma;
3795 : :
3796 : : Partition zeta; /* the first discovered terminal node */
3797 : : Partition rho; /* the best discovered terminal node */
3798 : :
3799 : 1288 : int nNumFoundGenerators = 0;
3800 : 1288 : int qzb_rho_fix = 0;
3801 : 1288 : int hzb_rho_fix = 0;
3802 : 1288 : int bRhoIsDiscrete = 1;
3803 : : kLeast kLeast_rho[MAX_LAYERS];
3804 : : kLeast kLeast_rho_fix[MAX_LAYERS];
3805 : : int nOneAdditionalLayer;
3806 : 1288 : int pzb_rho_fix_reached = 0;
3807 : 1288 : int L_rho_fix_prev = 0, I_rho_fix_prev = -1, k_rho_fix_prev = 0;
3808 : :
3809 [ + + ]: 1288 : if (!LargeMolecules)
3810 : : {
3811 : 278 : L_curr_max_set_size = NORMALLY_ALLOWED_MAX_SET_SIZE;
3812 : : }
3813 : :
3814 : :
3815 : : /* Note: Layered comparison should be consistent, especially in layer numbers.
3816 : : Layered comparison is implemented in:
3817 : : CtFullCompare()
3818 : : CtPartCompare()
3819 : : GetOneAdditionalLayer()
3820 : :
3821 : : The partial comparison results in kLeast[] are used in
3822 : : CtFullCompareLayers()
3823 : : CtPartCompareLayers()
3824 : : CtCompareLayersGetFirstDiff()
3825 : : UpdateCompareLayers()
3826 : : */
3827 : :
3828 : :
3829 : 1288 : nOneAdditionalLayer = GetOneAdditionalLayer( pCD1, pzb_rho_fix );
3830 : :
3831 : :
3832 : : /* next 2 lines for debug only */
3833 : : /* num_g++; */
3834 : : /* WriteGraph( G, n_tg, num_g, "V:\\IChI_v10\\Gordon-Graphs\\hard\\k06g08v312-alt.dre", "a+" ); */
3835 : :
3836 : : /* memory allocation */
3837 : :
3838 [ - + ]: 1288 : if (0 > SetBitCreate( pCG ))
3839 : : {
3840 : 0 : return -1;
3841 : : }
3842 [ + + - + ]: 1288 : if (pzb_rho_fix && pzb_rho_fix->nLenCTAtOnly != pCD->nLenCTAtOnly)
3843 : : {
3844 : : /* consistency check */
3845 : 0 : return -2;
3846 : : }
3847 : 1288 : ok = 1;
3848 : :
3849 : 1288 : ok &= UnorderedPartitionCreate( &theta, n_tg );
3850 : 1288 : ok &= UnorderedPartitionCreate( &theta_from_gamma, n_tg );
3851 : :
3852 : : /* djb-rwth: fixing dereferencing NULL pointer and buffer overflows */
3853 : 1288 : W = (Cell*) inchi_calloc( n_tg, sizeof( W[0] ) );
3854 : 1288 : v = (Node*) inchi_calloc( n_tg, sizeof( v[0] ) );
3855 : 1288 : e = (S_CHAR*) inchi_calloc( n_tg, sizeof( e[0] ) );
3856 : :
3857 [ + - + - : 1288 : if (!W || !v || !e)
- + ]
3858 : : {
3859 : 0 : ok &= 0;
3860 : : }
3861 : :
3862 : : /*
3863 : : ok &= ( NULL != ( W = (Cell*) inchi_calloc( n_tg, sizeof( W[0] ) ) ) );
3864 : : ok &= ( NULL != ( v = (Node*) inchi_calloc( n_tg, sizeof( v[0] ) ) ) );
3865 : : ok &= ( NULL != ( e = (S_CHAR*) inchi_calloc( n_tg, sizeof( e[0] ) ) ) );
3866 : : */
3867 : : /*
3868 : : ok &= (NULL != (v = (Node*)inchi_calloc( n_tg, sizeof(W[0]))));
3869 : : ok &= (NULL != (e = (S_CHAR*)inchi_calloc( n_tg, sizeof(W[0]))));
3870 : : */
3871 : :
3872 : : /* ok &= (NULL != (qzb = (S_CHAR*)inchi_calloc( n_tg, sizeof(W[0])))); */
3873 : 1288 : ok &= CTableCreate( &Lambda, n, pCD );
3874 : 1288 : ok &= CTableCreate( &zf_zeta, n, pCD );
3875 [ + - + - ]: 2576 : ok &= ( ( pzb_rho = (ConTable *) inchi_calloc( 1, sizeof( *pzb_rho ) ) ) &&
3876 : 1288 : CTableCreate( pzb_rho, n, pCD ) );
3877 : :
3878 : : /* Omega and Phi grow on demand up to L_curr_max_set_size sets (see NodeSetReserve) */
3879 : 1288 : ok &= NodeSetCreate( pCG, &Omega, n_tg, inchi_min( INITIAL_SET_SIZE, L_curr_max_set_size ) );
3880 : 1288 : ok &= NodeSetCreate( pCG, &Phi, n_tg, inchi_min( INITIAL_SET_SIZE, L_curr_max_set_size ) );
3881 : 1288 : ok &= NodeSetCreate( pCG, &cur_nodes, n_tg, 1 );
3882 : :
3883 : 1288 : ok &= PartitionCreate( &zeta, n_tg );
3884 : 1288 : ok &= PartitionCreate( &rho, n_tg );
3885 : 1288 : ok &= TranspositionCreate( &gamma, n_tg );
3886 : :
3887 : : INCHI_HEAPCHK
3888 : :
3889 : : /*L1:*/
3890 : 1288 : k = 1;
3891 : 1288 : size = 1.0;
3892 : 1288 : hz_rho = index = l = 0; /* djb-rwth: removing redundant code */
3893 : :
3894 [ - + ]: 1288 : if (!ok)
3895 : : {
3896 : 0 : goto exit_function; /* initialization failed */
3897 : : }
3898 : :
3899 : 1288 : UnorderedPartitionMakeDiscrete( &theta, n_tg );
3900 : 1288 : t_Lemma = 2;
3901 : :
3902 : 1288 : pCC->lNumBreakTies = 0;
3903 : 1288 : pCC->lNumDecreasedCT = 0;
3904 : 1288 : pCC->lNumRejectedCT = 0;
3905 : 1288 : pCC->lNumEqualCT = 1;
3906 : 1288 : pCC->lNumTotCT = 0;
3907 : : /* djb-rwth: removing redundant code */
3908 : :
3909 : 1288 : hzb_rho_fix = 1;
3910 : :
3911 : 1288 : memset( kLeast_rho, 0, sizeof( kLeast_rho ) ); /* djb-rwth: memset_s C11/Annex K variant? */
3912 : 1288 : memset( kLeast_rho_fix, 0, sizeof( kLeast_rho_fix ) ); /* djb-rwth: memset_s C11/Annex K variant? */
3913 : :
3914 [ + + ]: 1288 : if (PartitionIsDiscrete( &pi[k - 1], n_tg ))
3915 : : {
3916 : : /* added the following 3 lines to the original to create Ct */
3917 : 1233 : PartitionCopy( &rho, &pi[k - 1], n_tg );
3918 : 1233 : CtPartFill( G, pCD, &pi[k - 1], pzb_rho, 1, n, n_tg, n_max );
3919 : 1233 : CtPartINCHI_CANON_INFINITY( pzb_rho, qzb, 2 );
3920 : 1233 : pCC->lNumTotCT++;
3921 : : /* djb-rwth: removing redundant code */
3922 : : /* goto L18; */
3923 : 1233 : goto exit_function;
3924 : : }
3925 : :
3926 [ + + + + ]: 55 : if (!dig && PartitionSatisfiesLemma_2_25( &pi[0], n ))
3927 : : {
3928 : 41 : t_Lemma = 1;
3929 : : }
3930 : :
3931 : : /*
3932 : : PartitionGetFirstCell( &pi[k-1], &W[k-1], k, n );
3933 : : v[k-1] = CellGetMinNode( &pi[k-1], &W[k-1], 0, pCD1 );
3934 : : CtPartClear( &Lambda, 1 );
3935 : : e[k-1] = 0;
3936 : : */
3937 : 55 : CtPartClear( &Lambda, 1 );
3938 : : INCHI_HEAPCHK
3939 : :
3940 : : /* L2: reach the first leaf and save it in zeta and rho */
3941 [ + - ]: 106 : while (k)
3942 : : {
3943 : : /* the two next lines intentionally switched */
3944 : : /* Create equitable partition in pi[k] */
3945 : 106 : PartitionGetFirstCell( &pi[k - 1], W, k, n );
3946 : 106 : v[k - 1] = CellGetMinNode( &pi[k - 1], &W[k - 1], 0, pCD1 );
3947 : 106 : e[k - 1] = 0;
3948 [ + + + + ]: 106 : if (dig || !PartitionSatisfiesLemma_2_25( &pi[k - 1], n ))
3949 : : {
3950 : 33 : t_Lemma = k + 1;
3951 : : }
3952 : : /* e[k-1] = 0; */
3953 : : {
3954 : 106 : Node vv = v[k - 1];
3955 [ - + ]: 106 : if (0 > ( ret = PartitionColorVertex( pCG, G, &pi[k - 1], vv /*v[k-1]*/, n, n_tg, n_max, bDigraph, 0 ) ))
3956 : : {
3957 : 0 : goto exit_error;
3958 : : }
3959 : : }
3960 : 106 : pCC->lNumBreakTies++;
3961 : 106 : k++;
3962 : :
3963 : 106 : CtPartFill( G, pCD, &pi[k - 1], &Lambda, k - 1, n, n_tg, n_max);
3964 : :
3965 : : /* return -1; *//* debug only */
3966 : : /* if(h_zeta==0)goto L5; L5: */
3967 : : /* the first terminal node has not been reached yet */
3968 : : /* search for the predefined numbering */
3969 [ + + + - ]: 106 : if (pzb_rho_fix && QZFIX_OK( qzb_rho_fix ))
3970 : : {
3971 : 22 : qzb_rho_fix = CtPartCompare( &Lambda, pzb_rho_fix, qzb, kLeast_rho_fix, k - 1, 1, bSplitTautCompare );
3972 [ + - ]: 22 : if (QZFIX_OK( qzb_rho_fix ))
3973 : : {
3974 : 22 : hzb_rho_fix = k;
3975 : : }
3976 : : }
3977 : :
3978 [ + - + - ]: 106 : if (lab && QZFIX_OK( qzb_rho_fix )) /* DCh */
3979 : 106 : CtPartCopy( pzb_rho, &Lambda, k - 1 );
3980 : 106 : CtPartCopy( &zf_zeta, &Lambda, k - 1 );
3981 : : /*goto L4; L4:*/
3982 [ + + ]: 106 : if (PartitionIsDiscrete( &pi[k - 1], n ))
3983 : : {
3984 : 55 : break; /* goto L7; */
3985 : : }
3986 : : /* goto L2; */
3987 : : }
3988 : :
3989 : 55 : pCC->lNumTotCT++;
3990 : :
3991 : : /* L7; L7: */
3992 : : /* if ( h_zeta == 0 ) goto L18; L18:*/
3993 : 55 : h_zeta = t_eq_zeta = hz_zeta = k;
3994 : 55 : CtPartINCHI_CANON_INFINITY( &zf_zeta, NULL, k );
3995 : : /******************** <<<===== B **************************/
3996 : 55 : PartitionCopy( &zeta, &pi[k - 1], n_tg );
3997 [ + - ]: 55 : if (lab)
3998 : : {
3999 [ + + ]: 55 : if (pzb_rho_fix)
4000 : : {
4001 [ + - ]: 8 : if (0 == qzb_rho_fix)
4002 : : {
4003 : 8 : qzb_rho_fix = CtFullCompare( &Lambda, pzb_rho_fix, 1, bSplitTautCompare );
4004 [ - + ]: 8 : if (qzb_rho_fix > 0)
4005 : : {
4006 : 0 : hzb_rho_fix = 1;
4007 : : }
4008 : : }
4009 [ + - ]: 8 : if (hzb_rho_fix > 1)
4010 : : {
4011 : 8 : PartitionCopy( &rho, &pi[hzb_rho_fix - 1], n_tg );
4012 : : /*CtPartINCHI_CANON_INFINITY( pzb_rho, qzb, k );*/
4013 : : }
4014 : 8 : hz_rho = h_rho = hzb_rho_fix;
4015 : 8 : bRhoIsDiscrete = ( hzb_rho_fix == k );
4016 [ + - ]: 8 : if (bRhoIsDiscrete)
4017 : : {
4018 : 8 : CtPartINCHI_CANON_INFINITY( pzb_rho, qzb, k );
4019 : 8 : pzb_rho_fix_reached = !qzb_rho_fix;
4020 : 8 : CtCompareLayersGetFirstDiff( kLeast_rho_fix, nOneAdditionalLayer,
4021 : : &L_rho_fix_prev, &I_rho_fix_prev, &k_rho_fix_prev );
4022 : : }
4023 : :
4024 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4025 : : else
4026 : : {
4027 : : int stop = 1;
4028 : : }
4029 : : #endif
4030 : : }
4031 : : else
4032 : : {
4033 : 47 : PartitionCopy( &rho, &pi[k - 1], n_tg );
4034 : 47 : hz_rho = h_rho = k;
4035 : 47 : CtPartINCHI_CANON_INFINITY( pzb_rho, qzb, k );
4036 : : }
4037 : 55 : qzb_rho = 0;
4038 : : }
4039 : :
4040 : 55 : r = k;
4041 : 55 : v[k - 1] = INCHI_CANON_INFINITY; /* DCh */
4042 : 55 : CellMakeEmpty( W, k ); /* DCh */
4043 : 55 : k--;
4044 : 55 : goto L13;
4045 : :
4046 : :
4047 : 218 : L2:
4048 : : /* the two next lines intentionally switched */
4049 : : /* Create equitable partition in pi[k] */
4050 [ - + ]: 218 : if (0 > ( ret = PartitionColorVertex( pCG, G, &pi[k - 1], v[k - 1],
4051 : : n, n_tg, n_max, bDigraph, 0 ) ))
4052 : : {
4053 : 0 : goto exit_error;
4054 : : }
4055 : 218 : pCC->lNumBreakTies++;
4056 : 218 : k++;
4057 : 218 : CtPartFill( G, pCD, &pi[k - 1], &Lambda, k - 1, n, n_tg, n_max );
4058 : 218 : e[k - 1] = 0; /* moved */
4059 : 218 : v[k - 1] = INCHI_CANON_INFINITY; /* added by DCh. */
4060 : 218 : CellMakeEmpty( W, k ); /* DCh */
4061 : :
4062 [ + - + + ]: 436 : if (hz_zeta == k - 1 &&
4063 : 218 : 0 == CtPartCompare( &Lambda, &zf_zeta, NULL, NULL, k - 1, 0, bSplitTautCompare ))
4064 : : {
4065 : 215 : hz_zeta = k; /* max{k|Lambda(G,pi,nu(k))==Lambda(G,pi,zeta) } */
4066 : : } /* added */
4067 : :
4068 : :
4069 : : /* -- old code ---
4070 : : if ( pzb_rho_fix && QZFIX_OK(qzb_rho_fix) ) {
4071 : : qzb_rho_fix = CtPartCompare( &Lambda, pzb_rho_fix, qzb, kLeast_rho_fix, k-1, 1, bSplitTautCompare );
4072 : : if ( QZFIX_OK(qzb_rho_fix) ) {
4073 : : hzb_rho_fix = k;
4074 : : } else {
4075 : : pCC->lNumRejectedCT ++;
4076 : : }
4077 : : }
4078 : : */
4079 : :
4080 : : /* --- new code ---*/
4081 [ + + + - ]: 218 : if (pzb_rho_fix && !qzb_rho_fix)
4082 : : {
4083 : 51 : qzb_rho_fix = CtPartCompare( &Lambda, pzb_rho_fix, qzb, kLeast_rho_fix, k - 1, 1, bSplitTautCompare );
4084 [ + - + - ]: 51 : if (!qzb_rho_fix && bRhoIsDiscrete)
4085 : : {
4086 : 51 : qzb_rho_fix = CtPartCompareLayers( kLeast_rho_fix, L_rho_fix_prev, nOneAdditionalLayer );
4087 : :
4088 : : #if ( FIX_ChCh_CONSTIT_CANON_BUG == 1 )
4089 [ - + ]: 51 : if (qzb_rho_fix)
4090 : : {
4091 : 0 : int L_rho_fix_diff = abs( qzb_rho_fix ) - 1;
4092 [ # # # # ]: 0 : if (L_rho_fix_diff < L_rho_fix_prev ||
4093 [ # # # # ]: 0 : (L_rho_fix_diff == (L_rho_fix_prev && kLeast_rho_fix[L_rho_fix_diff].i < I_rho_fix_prev))) /* djb-rwth: addressing LLVM warning */
4094 : : {
4095 : 0 : qzb_rho_fix = L_rho_fix_diff + 1; /* positive difference will be rejected */
4096 : : }
4097 : : }
4098 : : #endif
4099 : :
4100 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4101 : : if (qzb_rho_fix)
4102 : : {
4103 : : int stop = 1; /* debug only */
4104 : : }
4105 : : #endif
4106 : : }
4107 [ - + ]: 51 : if (!QZFIX_OK( qzb_rho_fix ))
4108 : : {
4109 : 0 : pCC->lNumRejectedCT++;
4110 : : }
4111 : : }
4112 [ + + + - ]: 218 : if (pzb_rho_fix && QZFIX_OK( qzb_rho_fix ))
4113 : : {
4114 : 51 : hzb_rho_fix = k;
4115 : : }
4116 : : /* if (!lab) goto L3; */
4117 [ + - + - ]: 218 : if (lab && QZFIX_OK( qzb_rho_fix ))
4118 : : {
4119 : : /* once the difference has been found it is meaningful as long as k increments */
4120 : : /* cur_qzb2 = CtPartCompare( &Lambda, pzb_rho, qzb, k-1 ); */ /* rho compare */
4121 [ + - + - : 218 : if (hz_rho == k - 1 && !qzb_rho && bRhoIsDiscrete)
+ - ]
4122 : : {
4123 : 218 : int qzb_rho_temp = 0; /* djb-rwth: ignoring LLVM warning: variable used */
4124 : 218 : qzb_rho = CtPartCompare( &Lambda, pzb_rho, qzb, kLeast_rho, k - 1, 0, bSplitTautCompare );
4125 : : /* old code */
4126 [ + - + + : 218 : if (!qzb_rho && pzb_rho_fix_reached &&
+ - ]
4127 [ + + ]: 51 : nOneAdditionalLayer && 0 > kLeast_rho[nOneAdditionalLayer].k)
4128 : : {
4129 : 3 : qzb_rho_temp = -( nOneAdditionalLayer + 1 ); /* djb-rwth: ignoring LLVM warning: variable used */
4130 : : /* qzb_rho = -(nOneAdditionalLayer+1); *//* early rejection */
4131 : : }
4132 : : /* new code */
4133 [ + - + - ]: 218 : if (!qzb_rho && bRhoIsDiscrete)
4134 : : {
4135 : 218 : qzb_rho = CtPartCompareLayers( kLeast_rho, L_rho_fix_prev, 0 );
4136 : : #if ( FIX_ChCh_CONSTIT_CANON_BUG == 1 )
4137 [ + + ]: 218 : if (qzb_rho)
4138 : : {
4139 : 3 : int L_rho_diff = abs( qzb_rho ) - 1;
4140 [ + - - + ]: 6 : if (L_rho_diff < L_rho_fix_prev ||
4141 [ + - - + ]: 3 : (L_rho_diff == (L_rho_fix_prev && kLeast_rho[L_rho_diff].i < I_rho_fix_prev))) /* djb-rwth: addressing LLVM warning */
4142 : : {
4143 : 0 : qzb_rho = -( L_rho_diff + 1 ); /* negative difference will be rejected */
4144 : : }
4145 : : }
4146 : : #endif
4147 : : }
4148 : : /* compare old results to new */
4149 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4150 : : if (qzb_rho_temp && qzb_rho_temp != qzb_rho)
4151 : : {
4152 : : int stop = 1; /* <BRKPT> */
4153 : : }
4154 : : #endif
4155 [ + + ]: 218 : if (!qzb_rho)
4156 : : {
4157 : 215 : hz_rho = k;
4158 : : }
4159 : : else
4160 : : {
4161 [ + - ]: 3 : if (qzb_rho < 0)
4162 : : {
4163 : 3 : pCC->lNumRejectedCT++;
4164 : : }
4165 : : }
4166 : : }
4167 [ + - + + : 218 : if (qzb_rho > 0 || (!qzb_rho && !bRhoIsDiscrete)) /* djb-rwth: addressing LLVM warning */
- + ]
4168 : : {
4169 : : /* found better rho */
4170 [ # # ]: 0 : if (!nNumLayers)
4171 : : {
4172 : 0 : CtPartCopy( pzb_rho, &Lambda, k - 1 );
4173 : : }
4174 : : }
4175 : : }
4176 : :
4177 : :
4178 : : /*L3:*/
4179 : : /*if ( hz_rho == k || (lab && qzb_rho >= 0 ) )*/
4180 : : /*if ( hz_zeta == k || hz_rho == k || (lab && qzb_rho >= 0 ) ) goto L4; else goto L6;*/
4181 [ + + + - : 218 : if (hz_zeta == k || hz_rho == k ||
+ - ]
4182 [ + - - - ]: 3 : ( lab && qzb_rho >= 0 && QZFIX_OK( qzb_rho_fix ) ))
4183 : : {
4184 : : /*L4: check for possible isomorphism or found a better rho */
4185 [ + + ]: 215 : if (PartitionIsDiscrete( &pi[k - 1], n ))
4186 : : {
4187 : 108 : pCC->lNumTotCT++;
4188 : 108 : goto L7;
4189 : : }
4190 : 107 : PartitionGetFirstCell( &pi[k - 1], W, k, n );
4191 : 107 : v[k - 1] = CellGetMinNode( &pi[k - 1], &W[k - 1], 0, pCD1 );
4192 [ + + + + ]: 107 : if (!dig && PartitionSatisfiesLemma_2_25( &pi[k - 1], n ))
4193 : : {
4194 : : ; /* found additional isomprphism */
4195 : : }
4196 : : else
4197 : : {
4198 : 40 : t_Lemma = k + 1;
4199 : : }
4200 : 107 : e[k - 1] = 0; /* created new cell W[k-1] */
4201 : 107 : goto L2;
4202 : : }
4203 : :
4204 : :
4205 : 3 : L6:
4206 : : /* a better rho or no good node was found at this level; return to smaller k */
4207 : 3 : k2 = k;
4208 [ - + ]: 3 : k = inchi_min( t_Lemma - 1, inchi_max( t_eq_zeta - 1, hz_rho ) );
4209 [ + - ]: 3 : if (k2 == t_Lemma)
4210 : : {
4211 : 3 : goto L13;
4212 : : }
4213 : :
4214 : : /* store isomorphism found from Lemma 2.25. should be dig=0 !!! */
4215 [ # # ]: 0 : if (dig)
4216 : : {
4217 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4218 : : int stop = 1;
4219 : : #endif
4220 : 0 : goto L13;
4221 : : }
4222 : :
4223 : 0 : l = inchi_min( l + 1, L_curr_max_set_size );
4224 [ # # # # ]: 0 : if (!NodeSetReserve( &Omega, l ) || !NodeSetReserve( &Phi, l ))
4225 : : {
4226 : 0 : ret = CT_OUT_OF_RAM;
4227 : 0 : goto exit_error;
4228 : : }
4229 : 0 : PartitionGetMcrAndFixSet( pCG, &pi[t_Lemma - 1], &Omega, &Phi, n_tg, l );
4230 : 0 : goto L12;
4231 : :
4232 : :
4233 : :
4234 : 108 : L7:
4235 : : /* from L4: pi[k-1] is discrete */
4236 [ - + ]: 108 : if (h_zeta == 0)
4237 : : {
4238 : : /*goto L18;*/ /* error. the first T(nu) leaf was found */
4239 : 0 : ret = CT_CANON_ERR;
4240 : 0 : goto exit_error;
4241 : : }
4242 [ - + ]: 108 : if (k != hz_zeta)
4243 : : {
4244 : 0 : goto L8;
4245 : : }
4246 : : /* here zeta^gamma == nu */
4247 : : /* if ( G^gamma == G ) goto L10; */
4248 [ + - ]: 108 : if (0 == ( res = CtFullCompare( &Lambda, &zf_zeta, 0, bSplitTautCompare ) )) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
4249 : : {
4250 : 108 : PartitionGetTransposition( &zeta, &pi[k - 1], n_tg, &gamma );
4251 : 108 : bZetaIsomorph = 1; /* for testing only */
4252 : : /* djb-rwth: removing redundant code */
4253 : 108 : goto L10;
4254 : : }
4255 : : else
4256 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4257 : : {
4258 : : int stop = 1;
4259 : : }
4260 : : #endif
4261 : : /* !!! we should never come here !!! */
4262 [ # # ]: 0 : if (!nNumLayers)
4263 : : {
4264 : 0 : ret = -2;
4265 : 0 : goto exit_error;
4266 : : }
4267 : :
4268 : :
4269 : 0 : L8: /* here nu is discrete: check rho for being a bettere leaf or isomorphism */
4270 : : /*if ( !lab || qzb_rho < 0 || !QZFIX_OK(qzb_rho_fix) )*/
4271 [ # # # # : 0 : if (!lab || (qzb_rho < 0 && ( !pzb_rho_fix || qzb_rho_fix > 0 ))) /* djb-rwth: addressing LLVM warning */
# # # # ]
4272 : : {
4273 : 0 : goto L6;
4274 : : }
4275 [ # # # # ]: 0 : if (pzb_rho_fix && kLeast_rho_fix && 0 == qzb_rho_fix) /* djb-rwth: addressing of array kLeast_rho_fix will always evaluate to true? */
4276 : : {
4277 : : /* check for the rejection condition: Lambda > zb_rho_fix */
4278 : : if (kLeast_rho_fix) /* djb-rwth: addressing of array kLeast_rho_fix will always evaluate to true? */
4279 : : {
4280 : : int qzb_rho_fix_alt;
4281 : 0 : qzb_rho_fix = CtFullCompareLayers( kLeast_rho_fix );
4282 : : /* for debug only */
4283 : 0 : qzb_rho_fix_alt = CtFullCompare( &Lambda, pzb_rho_fix, 1, bSplitTautCompare );
4284 [ # # ]: 0 : if (qzb_rho_fix != qzb_rho_fix_alt)
4285 : : {
4286 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4287 : : int stop = 1;
4288 : : #endif
4289 : 0 : qzb_rho_fix = qzb_rho_fix_alt;
4290 : : }
4291 : : /* end debug */
4292 : : }
4293 : : else
4294 : : {
4295 : : qzb_rho_fix = CtFullCompare( &Lambda, pzb_rho_fix, 1, bSplitTautCompare );
4296 : : }
4297 [ # # ]: 0 : if (!pzb_rho_fix_reached)
4298 : : {
4299 : 0 : pzb_rho_fix_reached = !qzb_rho_fix;
4300 : : }
4301 [ # # ]: 0 : if (0 < qzb_rho_fix)
4302 : : {
4303 : : /* Lambda > pzb_rho_fix, ignore this node */
4304 : : /* hzb_rho_fix = min( hzb_rho_fix, hz_rho ); */ /* ??? */
4305 : 0 : qzb_rho_fix = 0;
4306 : 0 : goto L6;
4307 : : }
4308 : 0 : qzb_rho_fix = 0;
4309 : : }
4310 : :
4311 [ # # ]: 0 : if (qzb_rho < 0)
4312 : : {
4313 : 0 : goto L6;
4314 : : }
4315 [ # # # # ]: 0 : if (qzb_rho > 0 || !bRhoIsDiscrete)
4316 : : {
4317 : 0 : goto L9; /* note: p67 says k > PartitionSize( &rho, n ) */
4318 : : }
4319 [ # # ]: 0 : if (k < r)
4320 : : {
4321 : 0 : goto L9; /* cannot understand it... */
4322 : : }
4323 : :
4324 : : /* !!! we should never come here if G(nu) != G(rho): CtPartCompare must be enough !!! */
4325 : :
4326 : : /* if ( G(nu) > G(rho) ) goto L9; */
4327 : : if (kLeast_rho) /* djb-rwth: addressing of array kLeast_rho will always evaluate to true? */
4328 : : {
4329 : : int cur_qzb_alt;
4330 : 0 : qzb_rho = CtFullCompareLayers( kLeast_rho );
4331 : : /* for debug only */
4332 : 0 : cur_qzb_alt = CtFullCompare( &Lambda, pzb_rho, 0, bSplitTautCompare );
4333 [ # # ]: 0 : if (qzb_rho != cur_qzb_alt)
4334 : : {
4335 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4336 : : int stop = 1;
4337 : : #endif
4338 : 0 : qzb_rho = cur_qzb_alt;
4339 : : }
4340 : : /* end debug */
4341 : : }
4342 : : else
4343 : : {
4344 : : qzb_rho = CtFullCompare( &Lambda, pzb_rho, 0, bSplitTautCompare );
4345 : : }
4346 : : /* qzb_rho difference can be due to layers 1..MAX_LAYERS-1 only */
4347 [ # # ]: 0 : if (0 < qzb_rho)
4348 : : {
4349 : : /* CtFullCompare( &Lambda, pzb_rho, 0, bSplitTautCompare ); */
4350 : : /* djb-rwth: removing redundant code */
4351 : 0 : goto L9;
4352 : : }
4353 : : /* if ( G(nu) < G(rho) ) goto L6; */
4354 [ # # ]: 0 : if (0 > qzb_rho)
4355 : : {
4356 : 0 : qzb_rho = 0;
4357 : 0 : goto L6;
4358 : : }
4359 : : /* nu^gamma == rho */
4360 [ # # ]: 0 : if (r != k)
4361 : : { /* if() is for debug only */
4362 : 0 : r = k;
4363 : : }
4364 : :
4365 : 0 : PartitionGetTransposition( &pi[k - 1], &rho, n_tg, &gamma );
4366 : 0 : bZetaIsomorph = 0; /* DCh */
4367 : 0 : pCC->lNumEqualCT++;
4368 : 0 : goto L10;
4369 : :
4370 : 0 : L9:
4371 : : /* rho := nu; */
4372 : 0 : PartitionCopy( &rho, &pi[k - 1], n_tg );
4373 [ # # ]: 0 : if (nNumLayers)
4374 : : {
4375 : 0 : CtFullCopy( pzb_rho, &Lambda );
4376 : : }
4377 : 0 : bZetaEqRho = 0;
4378 : 0 : qzb_rho = 0;
4379 : 0 : CtCompareLayersGetFirstDiff( kLeast_rho_fix, nOneAdditionalLayer,
4380 : : &L_rho_fix_prev, &I_rho_fix_prev,
4381 : : &k_rho_fix_prev );
4382 : 0 : memset( kLeast_rho, 0, sizeof( kLeast_rho ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4383 : 0 : h_rho = hz_rho = k;
4384 : 0 : CtPartINCHI_CANON_INFINITY( pzb_rho, qzb, k );
4385 : 0 : pCC->lNumDecreasedCT++;
4386 : 0 : pCC->lNumEqualCT = 1;
4387 : 0 : bRhoIsDiscrete = 1;
4388 : 0 : goto L6;
4389 : :
4390 : 108 : L10: /* discrete pi[k-1] && G^gamma == G */
4391 [ - + - - : 108 : pCC->lNumEqualCT += bZetaEqRho || !( bZetaIsomorph || qzb_rho );
- - ]
4392 : 108 : l = inchi_min( l + 1, L_curr_max_set_size );
4393 [ + - - + ]: 108 : if (!NodeSetReserve( &Omega, l ) || !NodeSetReserve( &Phi, l ))
4394 : : {
4395 : 0 : ret = CT_OUT_OF_RAM;
4396 : 0 : goto exit_error;
4397 : : }
4398 : : /* Omega[l] := mcr(gamma);
4399 : : Phi[l] := fix(gamma);
4400 : : */
4401 : :
4402 : 108 : TranspositionGetMcrAndFixSetAndUnorderedPartition( pCG, &gamma, &Omega, &Phi,
4403 : : n_tg, l, &theta_from_gamma );
4404 : : /*
4405 : : if ( theta(gamma) <= theta ) goto L11;
4406 : : theta := theta v theta(gamma);
4407 : : UnorderedPartitionJoin() returns 0 if theta_from_gamma is finer than theta,
4408 : : which means no changes in theta: theta_from_gamma ^ theta == theta.
4409 : : */
4410 [ - + ]: 108 : if (!UnorderedPartitionJoin( &theta_from_gamma, &theta, n_tg ))
4411 : : {
4412 : 0 : goto L11; /* no new isomorphism found */
4413 : : }
4414 : :
4415 : : /* Output gamma (it is the Aut(G) generator) -- omitted -- */
4416 : 108 : nNumFoundGenerators++;
4417 : : /* if ( tvc in mcr(theta) ) goto L11; */
4418 [ + - ]: 108 : if (tvc == GetUnorderedPartitionMcrNode( &theta, tvc ))
4419 : : {
4420 : 108 : goto L11;
4421 : : }
4422 : 0 : k = h_zeta;
4423 : 0 : goto L13;
4424 : :
4425 : 108 : L11:
4426 [ + - ]: 108 : k = lab ? h_rho : h_zeta; /***Changed*** originally was k = h_rho; */
4427 : :
4428 : 108 : L12:
4429 : : /* if ( e[k-1] == 1 ) */
4430 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4431 : : if (e[k - 1] == 1 && v[k - 1] == INCHI_CANON_INFINITY)
4432 : : {
4433 : : int stop = 1; /* <BRKPT> testing only */
4434 : : }
4435 : : #endif
4436 [ + - - - ]: 108 : if (e[k - 1] == 1 && v[k - 1] != INCHI_CANON_INFINITY)
4437 : : {
4438 : : /* INCHI_CANON_INFINITY for testing only */
4439 : 0 : CellIntersectWithSet( pCG, &pi[k - 1], &W[k - 1], &Omega, l );
4440 : : }
4441 : :
4442 : 217 : L13:
4443 : :
4444 [ - + - - ]: 272 : if ((UserAction && USER_ACTION_QUIT == ( *UserAction )( )) ||
4445 [ - + - - ]: 272 : (ConsoleQuit && ( *ConsoleQuit )( ))) /* djb-rwth: addressing LLVM warning */
4446 : : {
4447 : 0 : ret = CT_USER_QUIT_ERR;
4448 : 0 : goto exit_error;
4449 : : }
4450 : :
4451 [ - + ]: 272 : if (bInchiTimeIsOver( ic, pCD->ulTimeOutTime ))
4452 : : {
4453 : 0 : ret = CT_TIMEOUT_ERR;
4454 : 0 : goto exit_error;
4455 : : }
4456 : :
4457 [ + + ]: 272 : if (k == 0)
4458 : : {
4459 : 55 : goto exit_function; /* stop */
4460 : : }
4461 : :
4462 [ + - + + ]: 217 : if (lab && k < h_rho)
4463 : : {
4464 : : /***Added***/
4465 : 106 : h_rho = k;
4466 : : }
4467 : :
4468 [ - + ]: 217 : if (k > h_zeta)
4469 : : {
4470 [ # # ]: 0 : if (v[k - 1] == INCHI_CANON_INFINITY)
4471 : : {
4472 : : /*** Added by DCh for testing only ****/
4473 : 0 : k--;
4474 : 0 : goto L13;
4475 : : }
4476 : 0 : goto L17;
4477 : : }
4478 [ + + ]: 217 : if (k == h_zeta)
4479 : : {
4480 : 111 : goto L14;
4481 : : }
4482 : 106 : h_zeta = k;
4483 : 106 : tvc = tvh = CellGetMinNode( &pi[k - 1], &W[k - 1], 0, pCD1 );
4484 : :
4485 : 217 : L14:
4486 : : /* if v[k] and tvh are in the same cell of theta then index ++ */
4487 [ + + ]: 534 : if (GetUnorderedPartitionMcrNode( &theta, v[k - 1] ) ==
4488 : 267 : GetUnorderedPartitionMcrNode( &theta, tvh ))
4489 : : {
4490 : 264 : index++;
4491 : : }
4492 : 267 : v[k - 1] = CellGetMinNode( &pi[k - 1], &W[k - 1], v[k - 1], pCD1 );
4493 : :
4494 [ + + ]: 267 : if (v[k - 1] == INCHI_CANON_INFINITY)
4495 : : {
4496 : 106 : goto L16;
4497 : : }
4498 [ + + ]: 161 : if (v[k - 1] != GetUnorderedPartitionMcrNode( &theta, v[k - 1] ))
4499 : : {
4500 : 50 : goto L14;
4501 : : }
4502 : :
4503 : 111 : L15:
4504 [ + + ]: 111 : t_Lemma = inchi_min( t_Lemma, k + 1 );
4505 : 111 : hz_zeta = inchi_min( hz_zeta, k );
4506 : : /*
4507 : : if ( lab && hz_rho >= k ) {
4508 : : hz_rho = k;
4509 : : qzb_rho = 0;
4510 : : }
4511 : : */
4512 [ + - ]: 111 : if (lab)
4513 : : {
4514 [ + - ]: 111 : if (hz_rho >= k /*-1*/)
4515 : : {
4516 : 111 : qzb_rho = 0;
4517 : : }
4518 [ + - ]: 111 : if (hz_rho > k)
4519 : : {
4520 : 111 : hz_rho = k;
4521 : : }
4522 : 111 : UpdateCompareLayers( kLeast_rho, hz_rho );
4523 : : }
4524 [ + + ]: 111 : if (pzb_rho_fix)
4525 : : {
4526 [ + - ]: 22 : if (hzb_rho_fix >= k /*-1*/)
4527 : : {
4528 : 22 : qzb_rho_fix = 0;
4529 : : }
4530 [ + - ]: 22 : if (hzb_rho_fix > k)
4531 : : {
4532 : 22 : hzb_rho_fix = k;
4533 : : }
4534 : 22 : UpdateCompareLayers( kLeast_rho_fix, hzb_rho_fix );
4535 : : }
4536 : :
4537 : 111 : goto L2;
4538 : :
4539 : 106 : L16:
4540 [ + - + + ]: 106 : if (t_eq_zeta == k + 1 && index == CellGetNumberOfNodes( &pi[k - 1], &W[k - 1] ))
4541 : : {
4542 : 103 : t_eq_zeta = k;
4543 : : }
4544 : :
4545 : 106 : size *= (double) index;
4546 : : /******************** <<<===== A **************************/
4547 : : /* passed K times after passing point A. At these passes
4548 : : k = K, K-1, ..., 1 in this order
4549 : : */
4550 : 106 : index = 0;
4551 : 106 : k--;
4552 : 106 : goto L13;
4553 : :
4554 : 0 : L17:
4555 : : /* if ( e[k-1] == 0 ) */
4556 : : #if ( bRELEASE_VERSION != 1 && defined(_DEBUG) )
4557 : : if (e[k - 1] == 0 && v[k - 1] == INCHI_CANON_INFINITY)
4558 : : {
4559 : : /* testing only */
4560 : : int stop = 1; /* <BRKPT> */
4561 : : }
4562 : : #endif
4563 : : /*
4564 : : if ( e[k] == 0 set W[k] = Intersection(W[k], Omega[i]) for each i = 1..l,
4565 : : such that {v[1]..v[k-1]} in Phi[i]
4566 : : */
4567 [ # # # # ]: 0 : if (e[k - 1] == 0 && v[k - 1] != INCHI_CANON_INFINITY) /* Added v[k-1]!=... DCh */
4568 : : {
4569 : 0 : NodeSetFromVertices( pCG, &cur_nodes, 1, v, k - 1 );
4570 [ # # ]: 0 : for (i = 1; i <= l; i++)
4571 : : {
4572 [ # # ]: 0 : if (AllNodesAreInSet( &cur_nodes, 1, &Phi, i ))
4573 : : {
4574 : 0 : CellIntersectWithSet( pCG, &pi[k - 1], &W[k - 1], &Omega, i );
4575 : : }
4576 : : }
4577 : : }
4578 : :
4579 : 0 : e[k - 1] = 1;
4580 : 0 : v[k - 1] = CellGetMinNode( &pi[k - 1], &W[k - 1], v[k - 1], pCD1 );
4581 [ # # ]: 0 : if (v[k - 1] != INCHI_CANON_INFINITY)
4582 : : {
4583 : 0 : goto L15;
4584 : : }
4585 : 0 : k--;
4586 : 0 : goto L13;
4587 : : /* L18: see above */
4588 : :
4589 : 1288 : exit_function:
4590 : : /* CtPartFill( G, pCD, &rho, pzb_rho, 1, n, n_tg ); */
4591 [ - + ]: 1288 : if (!bRhoIsDiscrete)
4592 : : {
4593 : 0 : ret = CT_CANON_ERR;
4594 : 0 : goto exit_error;
4595 : : }
4596 : :
4597 [ + + ]: 1288 : if (pzb_rho_fix)
4598 : : {
4599 : 21 : qzb_rho_fix = CtFullCompare( pzb_rho_fix, pzb_rho, 1, bSplitTautCompare );
4600 [ - + ]: 21 : if (qzb_rho_fix)
4601 : : {
4602 : 0 : ret = CT_CANON_ERR;
4603 : 0 : goto exit_error;
4604 : : }
4605 : : }
4606 : :
4607 : : /* SymmRank */
4608 : 1288 : memset( nSymmRank, 0, n_tg * sizeof( nSymmRank[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4609 [ + + ]: 13554 : for (i = 0; i < n_tg; i++)
4610 : : {
4611 : 12266 : k = rho.AtNumber[i];
4612 : 12266 : k2 = (int) GetUnorderedPartitionMcrNode( &theta, (AT_NUMB) ( k + 1 ) ) - 1;
4613 [ + + - + ]: 12266 : if (!nSymmRank[k2] || nSymmRank[k2] > rho.Rank[k])
4614 : : {
4615 : 10560 : nSymmRank[k2] = rho.Rank[k];
4616 : : }
4617 : : }
4618 [ + + ]: 13554 : for (i = 0; i < n_tg; i++)
4619 : : {
4620 : 12266 : k = rho.AtNumber[i];
4621 : 12266 : k2 = (int) GetUnorderedPartitionMcrNode( &theta, (AT_NUMB) ( k + 1 ) ) - 1;
4622 : 12266 : nSymmRank[k] = nSymmRank[k2];
4623 : : }
4624 : : /* CanonRank, nAtomNumberCanon */
4625 : 1288 : memcpy(nCanonRank, rho.Rank, n_tg * sizeof(nCanonRank[0]));
4626 : 1288 : memcpy(nAtomNumberCanon, rho.AtNumber, n_tg * sizeof(nAtomNumberCanon[0]));
4627 : : /* LinearCT */
4628 [ + - ]: 1288 : if (pzb_rho)
4629 : : {
4630 : 1288 : *nLenCt = pzb_rho->lenCt - 1;
4631 : : }
4632 [ - + - - : 1288 : if (pCt && pzb_rho && (*nLenCt > 0)) /* djb-rwth: GHI #164 fix */
- - ]
4633 : : {
4634 : 0 : memcpy(pCt, pzb_rho->Ctbl, *nLenCt * sizeof(pCt[0]));
4635 : : }
4636 : 1288 : pCC->lNumTotCT = pCC->lNumDecreasedCT + pCC->lNumRejectedCT + pCC->lNumEqualCT;
4637 : 1288 : pCC->dGroupSize = size;
4638 : 1288 : pCC->lNumGenerators = nNumFoundGenerators;
4639 : 1288 : pCC->lNumStoredIsomorphisms = l;
4640 : : /* Note: check nNumFoundGenerators */
4641 : :
4642 [ - + - + ]: 1288 : if (pp_zb_rho_out && !*pp_zb_rho_out)
4643 : : {
4644 : 1288 : *pp_zb_rho_out = pzb_rho;
4645 : 1288 : pzb_rho = NULL;
4646 : : }
4647 : :
4648 : :
4649 : :
4650 : 0 : exit_error:
4651 : : INCHI_HEAPCHK
4652 : :
4653 : 1288 : UnorderedPartitionFree( &theta );
4654 : 1288 : UnorderedPartitionFree( &theta_from_gamma );
4655 [ + - ]: 1288 : if (W)
4656 : : {
4657 [ + - ]: 1288 : inchi_free( W );
4658 : : }
4659 [ + - ]: 1288 : if (v)
4660 : : {
4661 [ + - ]: 1288 : inchi_free( v );
4662 : : }
4663 [ + - ]: 1288 : if (e)
4664 : : {
4665 [ + - ]: 1288 : inchi_free( e );
4666 : : }
4667 [ - + ]: 1288 : if (qzb)
4668 : : {
4669 [ # # ]: 0 : inchi_free( qzb );
4670 : : }
4671 : 1288 : CTableFree( &Lambda );
4672 : 1288 : CTableFree( &zf_zeta );
4673 [ - + ]: 1288 : if (pzb_rho)
4674 : : {
4675 : 0 : CTableFree( pzb_rho );
4676 [ # # ]: 0 : inchi_free( pzb_rho );
4677 : 0 : pzb_rho = NULL;
4678 : : }
4679 : : /* CTableFree( &zf_zeta2 ); */
4680 : 1288 : NodeSetFree( pCG, &Omega );
4681 : 1288 : NodeSetFree( pCG, &Phi );
4682 : : /* NodeSetFree( &mcr_theta, n, 1 ); */
4683 : 1288 : NodeSetFree( pCG, &cur_nodes );
4684 : 1288 : PartitionFree( &zeta );
4685 : : /* PartitionFree( &zeta2 ); */
4686 : 1288 : PartitionFree( &rho );
4687 : 1288 : TranspositionFree( &gamma );
4688 : :
4689 : 1288 : return ret;
4690 : : }
4691 : :
4692 : :
4693 : : /****************************************************************************
4694 : : SetInitialRanks2: Set initial ranks in nRank according to
4695 : : pAtomInvariant[] values
4696 : : Make sure enough prines have been generated.
4697 : : ****************************************************************************/
4698 : : /* Upon exit: */
4699 : : /* nAtomNumber[i]: number (from 0) of an atom in the ith (from 0) position of the sorted order */
4700 : : /* nNewRank[i]: initial rank of the atom[i] based on atom invariants; from 1 to num_atoms */
4701 : : /* Return value: Number of different ranks */
4702 : 1306 : int SetInitialRanks2( int num_atoms,
4703 : : ATOM_INVARIANT2* pAtomInvariant2,
4704 : : AT_RANK *nNewRank,
4705 : : AT_RANK *nAtomNumber,
4706 : : CANON_GLOBALS *pCG )
4707 : : {
4708 : : int i, nNumDiffRanks;
4709 : : AT_RANK nCurrentRank;
4710 : :
4711 [ + + ]: 13631 : for (i = 0; i < num_atoms; i++)
4712 : : {
4713 : 12325 : nAtomNumber[i] = (AT_RANK) i;
4714 : : }
4715 : :
4716 : : /* global for qsort */
4717 : 1306 : pCG->m_pAtomInvariant2ForSort = pAtomInvariant2;
4718 : :
4719 : 1306 : inchi_qsort( pCG, nAtomNumber, num_atoms, sizeof( nAtomNumber[0] ), CompAtomInvariants2 );
4720 : :
4721 : : /* nNewRank[i]: non-decreading order; do not increment nCurrentRank */
4722 : : /* if consecutive sorted atom invariants are identical */
4723 : :
4724 : : /* djb-rwth: fixing oss-fuzz issue #69315 */
4725 : 1306 : nNumDiffRanks = 1;
4726 [ + - ]: 1306 : if (num_atoms > 0)
4727 : : {
4728 : 1306 : nCurrentRank = (AT_RANK)num_atoms;
4729 : 1306 : nNewRank[nAtomNumber[num_atoms - 1]] = nCurrentRank;
4730 [ + + ]: 12325 : for (i = num_atoms - 1; i > 0; i--)
4731 : : {
4732 : : /* Note: CompAtomInvariants2Only() in following line implicitly reads pAtomInvariant2 pointed by pAtomInvariant2ForSort */
4733 [ + + ]: 11019 : if (CompAtomInvariants2Only(&nAtomNumber[i - 1], &nAtomNumber[i], pCG))
4734 : : {
4735 : 1875 : nNumDiffRanks++;
4736 : 1875 : nCurrentRank = (AT_RANK)i;
4737 : : }
4738 : 11019 : nNewRank[nAtomNumber[i - 1]] = nCurrentRank;
4739 : : }
4740 : : }
4741 : :
4742 : 1306 : return nNumDiffRanks;
4743 : : }
4744 : :
4745 : :
4746 : : /****************************************************************************/
4747 : 1285 : void FillOutAtomInvariant2( sp_ATOM* at,
4748 : : int num_atoms,
4749 : : int num_at_tg,
4750 : : ATOM_INVARIANT2* pAtomInvariant,
4751 : : int bIgnoreIsotopic,
4752 : : int bHydrogensInRanks,
4753 : : int bHydrogensFixedInRanks,
4754 : : int bDigraph,
4755 : : int bTautGroupsOnly,
4756 : : T_GROUP_INFO *t_group_info )
4757 : : {
4758 : : int i, k, j, i_t_group;
4759 : : /* tautomers */
4760 : 1285 : T_GROUP *t_group = NULL;
4761 : 1285 : int num_t_groups = 0;
4762 : 1285 : int num_tautomer_iso = 0;
4763 : : #define ELEM_NAME_LEN 2
4764 : : char ChemElements[ELEM_NAME_LEN*NUM_CHEM_ELEMENTS + ELEM_NAME_LEN];
4765 : : char CurElement[ELEM_NAME_LEN + ELEM_NAME_LEN], *pCurElem;
4766 : 1285 : int nNumChemElements = 0;
4767 : 1285 : int nNumHydrogenAtoms = 0;
4768 : 1285 : int nNumCarbonAtoms = 0;
4769 : :
4770 : 1285 : ChemElements[0] = '\0'; /* djb-rwth: initialisation prevents empty string comparison */
4771 : 1285 : memset( ChemElements, 0, sizeof( ChemElements ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4772 : 1285 : memset( CurElement, 0, sizeof( CurElement ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4773 : 1285 : nNumChemElements = 0;
4774 : :
4775 [ + + + - ]: 1285 : if (num_at_tg > num_atoms && t_group_info)
4776 : : {
4777 : 5 : t_group = t_group_info->t_group;
4778 : 5 : num_t_groups = t_group_info->num_t_groups;
4779 [ + - ]: 5 : num_tautomer_iso = t_group_info->bIgnoreIsotopic ? 0 : T_NUM_ISOTOPIC;
4780 : : }
4781 : :
4782 [ + + ]: 1285 : if (!bTautGroupsOnly)
4783 : : {
4784 : :
4785 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
4786 : : {
4787 [ + + ]: 9207 : if (!strcmp( at[i].elname, "C" ))
4788 : : {
4789 : 8699 : nNumCarbonAtoms++;
4790 : : }
4791 [ + + ]: 508 : else if (!strcmp( at[i].elname, "H" ) ||
4792 [ + - ]: 500 : !strcmp( at[i].elname, "D" ) ||
4793 [ - + ]: 500 : !strcmp( at[i].elname, "T" ))
4794 : : {
4795 : 8 : nNumHydrogenAtoms++;
4796 : : }
4797 : : else
4798 : : {
4799 : 500 : CurElement[0] = at[i].elname[0];
4800 [ + + ]: 500 : CurElement[1] = at[i].elname[1] ? at[i].elname[1] : ' ';
4801 [ + + ]: 500 : if (!( pCurElem = strstr( ChemElements, CurElement ) )) /* djb-rwth: ignoring LLVM warning: variable used for function return value */
4802 : : {
4803 : 320 : strcat(ChemElements, CurElement);
4804 : 320 : nNumChemElements++;
4805 : : }
4806 : : }
4807 : : }
4808 [ + + ]: 1267 : if (nNumChemElements > 1)
4809 : : {
4810 : 71 : qsort( ChemElements, nNumChemElements, ELEM_NAME_LEN, CompChemElemLex );
4811 : : }
4812 [ + + ]: 1267 : if (nNumCarbonAtoms)
4813 : : {
4814 [ + + ]: 1144 : if (nNumChemElements)
4815 : : {
4816 : 125 : memmove(ChemElements + ELEM_NAME_LEN, ChemElements, (long long)ELEM_NAME_LEN * (long long)nNumChemElements); /* djb-rwth: cast operators added */
4817 : : }
4818 : 1144 : ChemElements[0] = 'C';
4819 : 1144 : ChemElements[1] = ' ';
4820 : 1144 : nNumChemElements++;
4821 : : }
4822 [ + + ]: 1267 : if (nNumHydrogenAtoms)
4823 : : {
4824 : 8 : ChemElements[ELEM_NAME_LEN*nNumChemElements] = 'H';
4825 : 8 : ChemElements[ELEM_NAME_LEN*nNumChemElements + 1] = ' ';
4826 : 8 : nNumChemElements++;
4827 : : }
4828 : :
4829 : : /* general */
4830 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
4831 : : {
4832 : 9207 : memset( &pAtomInvariant[i], 0, sizeof( pAtomInvariant[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4833 : 9207 : CurElement[0] = at[i].elname[0];
4834 [ + + ]: 9207 : CurElement[1] = at[i].elname[1] ? at[i].elname[1] : ' ';
4835 : 9207 : pCurElem = strstr( ChemElements, CurElement );
4836 [ + - ]: 9207 : if (pCurElem)
4837 : : {
4838 : 9207 : j = (int) ( pCurElem - ChemElements ) / ELEM_NAME_LEN + 1;
4839 : : }
4840 : : else
4841 : : {
4842 : 0 : j = nNumChemElements; /* must be D or T */
4843 : : }
4844 : : /* at[i].hill_type = (U_CHAR) j; */
4845 : 9207 : pAtomInvariant[i].val[AT_INV_HILL_ORDER] = j;
4846 : :
4847 : 9207 : pAtomInvariant[i].val[AT_INV_NUM_CONNECTIONS] = at[i].valence;
4848 [ - + ]: 9207 : if (bHydrogensInRanks)
4849 : : {
4850 [ # # # # ]: 0 : pAtomInvariant[i].val[AT_INV_NUM_H] = ( ( t_group && at[i].endpoint > 0 ) ? 0 : at[i].num_H );
4851 : : }
4852 [ - + ]: 9207 : if (bHydrogensFixedInRanks)
4853 : : {
4854 [ # # # # ]: 0 : pAtomInvariant[i].val[AT_INV_NUM_H_FIX] = ( ( t_group && at[i].endpoint > 0 ) ? at[i].num_H : 0 );
4855 : : }
4856 [ + - - + : 9207 : if (!bDigraph && t_group && ( i_t_group = (int) at[i].endpoint - 1 ) >= 0 && i_t_group < num_t_groups)
- - - - ]
4857 : : {
4858 : 0 : pAtomInvariant[i].val[AT_INV_NUM_TG_ENDPOINTS] = t_group[i_t_group].nNumEndpoints;
4859 [ # # ]: 0 : for (j = 0; j < T_NUM_NO_ISOTOPIC; j++)
4860 : : {
4861 : 0 : pAtomInvariant[i].val[AT_INV_TG_NUMBERS + j] = t_group[i_t_group].num[j];
4862 : : }
4863 [ # # ]: 0 : for (j = 0; j < num_tautomer_iso; j++)
4864 : : {
4865 : 0 : pAtomInvariant[i].val[AT_INV_TAUT_ISO + j] = t_group[i_t_group].num[j + T_NUM_NO_ISOTOPIC];
4866 : : }
4867 : : }
4868 [ - + ]: 9207 : pAtomInvariant[i].iso_sort_key = bIgnoreIsotopic ? 0 : at[i].iso_sort_key;
4869 : : }
4870 : : }
4871 : : else
4872 : : {
4873 : : /* fill tautomeric groups only */
4874 : 18 : memset( pAtomInvariant, 0, num_at_tg * sizeof( pAtomInvariant[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4875 : : }
4876 : :
4877 : : /**************************************/
4878 : : /* tautomeric groups */
4879 : : /**************************************/
4880 [ + + ]: 1295 : for (i = num_atoms; i < num_at_tg; i++)
4881 : : {
4882 : :
4883 : 10 : k = i - num_atoms;
4884 : 10 : memset( &pAtomInvariant[i], 0, sizeof( pAtomInvariant[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4885 [ - + ]: 10 : if (!t_group)
4886 : 0 : continue;
4887 : : /* make sure ranks of t-groups are larger than that of any atom */
4888 : : /* greater than for any real atom */
4889 [ - + ]: 10 : pAtomInvariant[i].val[AT_INV_HILL_ORDER] = bTautGroupsOnly ? num_at_tg : nNumChemElements + 1;
4890 : : /* greater than for any real atom */
4891 : 10 : pAtomInvariant[i].val[AT_INV_NUM_CONNECTIONS] = MAXVAL + 1;
4892 [ + - ]: 10 : if (k < num_t_groups)
4893 : : {
4894 : 10 : pAtomInvariant[i].val[AT_INV_NUM_TG_ENDPOINTS] = t_group[k].nNumEndpoints;
4895 [ + + ]: 30 : for (j = 0; j < T_NUM_NO_ISOTOPIC; j++)
4896 : : {
4897 : 20 : pAtomInvariant[i].val[AT_INV_TAUT_ISO + j] = t_group[k].num[j];
4898 : : }
4899 [ - + ]: 10 : for (j = 0; j < num_tautomer_iso; j++)
4900 : : {
4901 : 0 : pAtomInvariant[i].val[AT_INV_TAUT_ISO + j] = t_group[k].num[j + T_NUM_NO_ISOTOPIC];
4902 : : }
4903 : : }
4904 : : }
4905 : 1285 : }
4906 : :
4907 : :
4908 : : /*****************************************************************************/
4909 : 5032 : void CleanNumH( NUM_H *NumH, int len )
4910 : : {
4911 : : int i;
4912 [ + + ]: 5032 : if (NumH)
4913 : : {
4914 [ + + ]: 23522 : for (i = 0; i < len; i++)
4915 : : {
4916 [ + + ]: 20988 : if (NumH[i] == EMPTY_H_NUMBER)
4917 : : {
4918 : 890 : NumH[i] = 0;
4919 : : }
4920 : : else
4921 : : {
4922 : 20098 : NumH[i] -= BASE_H_NUMBER;
4923 : : }
4924 : : }
4925 : : }
4926 : 5032 : }
4927 : :
4928 : :
4929 : : /****************************************************************************/
4930 : 1267 : int CleanCt( AT_RANK *Ct, int len )
4931 : : {
4932 [ + - + - ]: 1267 : if (Ct && Ct[len] == EMPTY_CT)
4933 : : {
4934 : 1267 : Ct[len] = 0;
4935 : 1267 : return 1;
4936 : : }
4937 : :
4938 : 0 : return 0;
4939 : : }
4940 : :
4941 : :
4942 : : /*****************************************************************************/
4943 : 20 : void CleanIsoSortKeys( AT_ISO_SORT_KEY * isk, int len )
4944 : : {
4945 : : int i;
4946 [ + - ]: 20 : if (isk)
4947 : : {
4948 [ + + ]: 10034 : for (i = 0; i < len; i++)
4949 : : {
4950 [ + + ]: 10014 : if (isk[i] == EMPTY_ISO_SORT_KEY)
4951 : : {
4952 : 9994 : isk[i] = 0;
4953 : : }
4954 : : }
4955 : : }
4956 : 20 : }
4957 : :
4958 : :
4959 : : /*****************************************************************************/
4960 : :
4961 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
4962 : : void MergeCleanIsoSortKeys( AT_ISO_SORT_KEY * isk1, AT_ISO_SORT_KEY * isk2, int len )
4963 : : {
4964 : : int i;
4965 : : AT_ISO_SORT_KEY k1, k2;
4966 : : if (isk1 && isk2)
4967 : : {
4968 : : for (i = 0; i < len; i++)
4969 : : {
4970 : : k1 = ( isk1[i] == EMPTY_ISO_SORT_KEY ) ? 0 : isk1[i];
4971 : : k2 = ( isk2[i] == EMPTY_ISO_SORT_KEY ) ? 0 : isk2[i];
4972 : : isk1[i] = k1 | k2;
4973 : : }
4974 : : }
4975 : : else if (isk1)
4976 : : {
4977 : : CleanIsoSortKeys( isk1, len );
4978 : : }
4979 : : }
4980 : : #endif
4981 : :
4982 : :
4983 : : #define FREE_CONTABLE( X) if (X) {CTableFree( X);inchi_free( X);}
4984 : : #define FREE_ARRAY( X) if (X) inchi_free( X);
4985 : :
4986 : :
4987 : : /****************************************************************************/
4988 : 1267 : void DeAllocBCN( BCN *pBCN )
4989 : : {
4990 : : int i, k;
4991 : : FTCN *ftcn;
4992 [ - + ]: 1267 : if (!pBCN)
4993 : : {
4994 : 0 : return;
4995 : : }
4996 [ + - ]: 1267 : if (pBCN->pRankStack)
4997 : : {
4998 [ + + ]: 14777 : for (i = 0; i < pBCN->nMaxLenRankStack; i++)
4999 : : {
5000 [ + + + - ]: 13510 : FREE_ARRAY( pBCN->pRankStack[i] )
5001 : : }
5002 [ + - + - ]: 1267 : FREE_ARRAY( pBCN->pRankStack )
5003 : : }
5004 [ + + ]: 3801 : for (k = 0; k < TAUT_NUM; k++)
5005 : : {
5006 : 2534 : ftcn = pBCN->ftcn + k;
5007 : 2534 : FreeNeighList( ftcn->NeighList );
5008 : :
5009 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->LinearCt )
5010 : :
5011 : 2534 : PartitionFree( &ftcn->PartitionCt );
5012 : :
5013 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->nSymmRankCt )
5014 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->nNumHOrig )
5015 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->nNumH )
5016 [ - + - - ]: 2534 : FREE_ARRAY( ftcn->nNumHOrigFixH )
5017 [ - + - - ]: 2534 : FREE_ARRAY( ftcn->nNumHFixH )
5018 : :
5019 : 2534 : PartitionFree( &ftcn->PartitionCtIso );
5020 : :
5021 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->nSymmRankCtIso )
5022 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->iso_sort_keys )
5023 [ + + + - ]: 2534 : FREE_ARRAY( ftcn->iso_sort_keysOrig )
5024 [ - + - - ]: 2534 : FREE_ARRAY( ftcn->iso_exchg_atnos )
5025 [ - + - - ]: 2534 : FREE_ARRAY( ftcn->iso_exchg_atnosOrig )
5026 : : }
5027 : : }
5028 : :
5029 : : #undef FREE_CONTABLE
5030 : : #undef FREE_ARRAY
5031 : :
5032 : :
5033 : : #if ( bRELEASE_VERSION == 0 && FIND_CANON_NE_EQUITABLE == 1 )
5034 : :
5035 : : /****************************************************************************
5036 : : debug: find whether canonical equivalence is
5037 : : different from equitable partition
5038 : : ****************************************************************************/
5039 : : int bCanonIsFinerThanEquitablePartition( int num_atoms,
5040 : : sp_ATOM* at,
5041 : : AT_RANK *nSymmRank )
5042 : : {
5043 : : AT_RANK *nRank = NULL;
5044 : : AT_RANK *nAtomNumber = NULL;
5045 : : AT_RANK *nTempRank = NULL;
5046 : : AT_RANK nCurSymm, nCurRank;
5047 : : ATOM_INVARIANT2 *pAtomInvariant = NULL;
5048 : : NEIGH_LIST *NeighList = NULL;
5049 : :
5050 : : int nNumCurrRanks, i, is, ir, j;
5051 : : long lCount;
5052 : : int bIsNotSame = 0;
5053 : :
5054 : : if (at && nSymmRank)
5055 : : {
5056 : : if (!( nRank = (AT_RANK*) inchi_calloc( num_atoms, sizeof( nRank[0] ) ) ) ||
5057 : : !( nAtomNumber = (AT_RANK*) inchi_calloc( num_atoms, sizeof( nAtomNumber[0] ) ) ) ||
5058 : : !( nTempRank = (AT_RANK*) inchi_calloc( num_atoms, sizeof( nTempRank[0] ) ) ) ||
5059 : : !( pAtomInvariant = (ATOM_INVARIANT2 *) inchi_calloc( num_atoms, sizeof( pAtomInvariant[0] ) ) )
5060 : : )
5061 : : {
5062 : : goto exit_err;
5063 : : }
5064 : : if (!( NeighList = CreateNeighList( num_atoms, num_atoms, at, 0, NULL ) ))
5065 : : {
5066 : : goto exit_err;
5067 : : }
5068 : :
5069 : : FillOutAtomInvariant2( at,
5070 : : num_atoms,
5071 : : num_atoms,
5072 : : pAtomInvariant,
5073 : : 1 /*bIgnoreIsotopic*/,
5074 : : 1 /*bHydrogensInRanks*/,
5075 : : 1 /*bHydrogensFixedInRanks*/,
5076 : : 0 /*bTaut=bDigraph*/,
5077 : : 0 /* bTautGroupsOnly */,
5078 : : NULL /*t_group_info*/ );
5079 : :
5080 : : /* initial partitioning of a hydrogenless skeleton: create equitable partition (assign initial ranks) */
5081 : : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariant, nRank, nAtomNumber, pCG );
5082 : :
5083 : : lCount = 0;
5084 : : /* make equitable partition in pBCN->pRankStack[0,1] */
5085 : : nNumCurrRanks = DifferentiateRanks2( pCG, num_atoms, NeighList,
5086 : : nNumCurrRanks, nRank,
5087 : : nTempRank, nAtomNumber, &lCount, 0 /* 0 means use qsort */ );
5088 : :
5089 : :
5090 : : /* at this point the equitable partition is in nRank; the order of atoms is in nAtomNumber*/
5091 : : /* compare */
5092 : : nCurSymm = nCurRank = 0;
5093 : : for (i = 0; i < num_atoms; i++)
5094 : : {
5095 : : j = (int) nAtomNumber[i];
5096 : : if (nCurSymm != nSymmRank[j])
5097 : : {
5098 : : nCurSymm = nSymmRank[j];
5099 : : is = i;
5100 : : }
5101 : : if (nCurRank != nRank[j])
5102 : : {
5103 : : nCurRank = nRank[j];
5104 : : ir = i;
5105 : : }
5106 : : if (is != ir)
5107 : : {
5108 : : bIsNotSame = 1;
5109 : : break;
5110 : : }
5111 : : }
5112 : : }
5113 : :
5114 : : exit_err:
5115 : : if (nRank)
5116 : : inchi_free( nRank );
5117 : : if (nAtomNumber)
5118 : : inchi_free( nAtomNumber );
5119 : : if (nTempRank)
5120 : : inchi_free( nTempRank );
5121 : : if (pAtomInvariant)
5122 : : inchi_free( pAtomInvariant );
5123 : : if (NeighList)
5124 : : FreeNeighList( NeighList );
5125 : :
5126 : : return bIsNotSame;
5127 : : }
5128 : :
5129 : : #endif
5130 : :
5131 : :
5132 : : /****************************************************************************/
5133 : 1267 : int GetBaseCanonRanking( INCHI_CLOCK *ic,
5134 : : int num_atoms,
5135 : : int num_at_tg,
5136 : : sp_ATOM* at[],
5137 : : T_GROUP_INFO *t_group_info,
5138 : : ATOM_SIZES s[],
5139 : : BCN *pBCN,
5140 : : struct tagInchiTime *ulTimeOutTime,
5141 : : CANON_GLOBALS *pCG,
5142 : : int bFixIsoFixedH,
5143 : : int LargeMolecules )
5144 : : {
5145 : 1267 : int ret = 0;
5146 : : int iBase; /* base structure index, always valid; = TAUT_YES except special fully non-taut mode */
5147 : : int iOther; /* other than basic structure index, usually non-taut; may be = iBase */
5148 : : int bReqNonTaut; /* 1 => requested non-tautomeric results */
5149 : : int bReqTaut; /* 1 => requested tautomeric results and the base structure is tautomeric */
5150 : : int bChanged;
5151 : :
5152 : 1267 : sp_ATOM *at_base = NULL;
5153 : 1267 : sp_ATOM *at_other = NULL;
5154 : :
5155 : 1267 : int bTautIgnoreIsotopic = 0;
5156 : : /*int bIgnoreIsotopic = 0;*/
5157 : 1267 : int nNumCurrRanks = 0;
5158 : 1267 : int nMaxLenRankStack = 0;
5159 : 1267 : int num_max = num_at_tg;
5160 : : long lCount;
5161 : :
5162 : : /* local allocations */
5163 : 1267 : ATOM_INVARIANT2 *pAtomInvariant = NULL;
5164 : : NEIGH_LIST *NeighList[TAUT_NUM];
5165 : 1267 : ConTable *Ct_Temp = NULL;
5166 : :
5167 : : /* initial partition for canonicalization */
5168 : 1267 : AT_RANK *nRank = NULL;
5169 : 1267 : AT_NUMB *nAtomNumber = NULL;
5170 : :
5171 : : /* canonicalization output */
5172 : :
5173 : 1267 : ConTable *Ct_NoH = NULL;
5174 : 1267 : AT_RANK *nCanonRankNoH = NULL;
5175 : 1267 : AT_NUMB *nAtomNumberCanonNoH = NULL;
5176 : 1267 : AT_RANK *nSymmRankNoH = NULL;
5177 : :
5178 : 1267 : ConTable *Ct_NoTautH = NULL;
5179 : 1267 : AT_RANK *nSymmRankNoTautH = NULL;
5180 : 1267 : AT_RANK *nCanonRankNoTautH = NULL;
5181 : 1267 : AT_NUMB *nAtomNumberCanonNoTautH = NULL;
5182 : 1267 : NUM_H *numHNoTautH = NULL;
5183 : : int lenNumHNoTautH;
5184 : : int maxlenNumHNoTautH;
5185 : :
5186 : 1267 : ConTable *Ct_Base = NULL;
5187 : 1267 : AT_RANK *nSymmRankBase = NULL;
5188 : 1267 : AT_RANK *nCanonRankBase = NULL;
5189 : 1267 : AT_NUMB *nAtomNumberCanonBase = NULL;
5190 : 1267 : NUM_H *numH = NULL;
5191 : 1267 : int lenNumH = num_atoms;
5192 : 1267 : int maxlenNumH = 0;
5193 : :
5194 : : #if ( USE_AUX_RANKING == 1 )
5195 : 1267 : AT_RANK *nRankAux = NULL;
5196 : 1267 : AT_NUMB *nAtomNumberAux = NULL;
5197 : 1267 : ATOM_INVARIANT2 *pAtomInvariantAux = NULL;
5198 : : #endif
5199 : :
5200 : 1267 : ConTable *Ct_FixH = NULL;
5201 : 1267 : AT_RANK *nSymmRankFixH = NULL;
5202 : 1267 : AT_RANK *nCanonRankFixH = NULL;
5203 : 1267 : AT_NUMB *nAtomNumberCanonFixH = NULL;
5204 : 1267 : NUM_H *NumHfixed = NULL;
5205 : : int maxlenNumHfixed;
5206 : :
5207 : : /* isotopic canonicalization */
5208 : :
5209 : 1267 : ConTable *Ct_NoTautHIso = NULL;
5210 : 1267 : AT_RANK *nSymmRankNoTautHIso = NULL;
5211 : 1267 : AT_RANK *nCanonRankNoTautHIso = NULL;
5212 : 1267 : AT_NUMB *nAtomNumberCanonNoTautHIso = NULL;
5213 : 1267 : AT_ISO_SORT_KEY *iso_sort_key_NoTautH = NULL;
5214 : 1267 : int maxlen_iso_sort_key_NoTautH = 0;
5215 : 1267 : int len_iso_sort_key_NoTautH = 0;
5216 : 1267 : int num_iso_NoTautH = 0, num_iso_NoAuxBase;
5217 : :
5218 : 1267 : ConTable *Ct_BaseIso = NULL;
5219 : 1267 : AT_RANK *nSymmRankBaseIso = NULL;
5220 : 1267 : AT_RANK *nCanonRankBaseIso = NULL;
5221 : 1267 : AT_NUMB *nAtomNumberCanonBaseIso = NULL;
5222 : :
5223 : 1267 : AT_ISO_SORT_KEY *iso_sort_keyBase = NULL;
5224 : 1267 : int maxlen_iso_sort_keyBase = 0;
5225 : 1267 : int len_iso_sort_keyBase = 0;
5226 : :
5227 : : int bUseIsoAuxBase[TAUT_NUM];
5228 : 1267 : S_CHAR *iso_exchg_atnos = NULL;
5229 : 1267 : int len_iso_exchg_atnos = 0;
5230 : 1267 : int maxlen_iso_exchg_atnos = 0;
5231 : 1267 : int num_iso_Base = 0;
5232 : :
5233 : : AT_ISO_SORT_KEY iso_sort_key;
5234 : :
5235 : 1267 : ConTable *Ct_FixHIso = NULL;
5236 : 1267 : AT_RANK *nSymmRankFixHIso = NULL;
5237 : 1267 : AT_RANK *nCanonRankFixHIso = NULL;
5238 : 1267 : AT_NUMB *nAtomNumberCanonFixHIso = NULL;
5239 : :
5240 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5241 : : AT_ISO_SORT_KEY iso_sort_key2;
5242 : : AT_ISO_SORT_KEY *iso_sort_key_Hfixed = NULL;
5243 : : int maxlen_iso_sort_key_Hfixed;
5244 : : int len_iso_sort_key_Hfixed;
5245 : : int num_iso_Hfixed;
5246 : : #endif
5247 : :
5248 : 1267 : AT_RANK *nTempRank = NULL;
5249 : :
5250 : : CANON_DATA pCD[3]; /* = &CanonData; */
5251 : : CANON_COUNTS CanonCounts;
5252 : 1267 : CANON_COUNTS *pCC = &CanonCounts;
5253 : :
5254 : : int i, j, k, m;
5255 : : int nCanonFlags[2];
5256 : :
5257 : : /* */
5258 : : int iflag;
5259 : :
5260 : 1267 : memset( pCD, 0, sizeof( pCD ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5261 : 1267 : memset( pCC, 0, sizeof( pCC[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5262 : 1267 : memset( bUseIsoAuxBase, 0, sizeof( bUseIsoAuxBase ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5263 : 1267 : memset( nCanonFlags, 0, sizeof( nCanonFlags ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5264 : 1267 : NeighList[TAUT_NON] = NULL;
5265 : 1267 : NeighList[TAUT_YES] = NULL;
5266 : :
5267 : : /* select base structure, find whether it is tautomeric or not */
5268 : : /* djb-rwth: addressing LLVM warning */
5269 [ + - + - : 1267 : if (at[TAUT_YES] && s[TAUT_YES].nLenCT &&
+ - ]
5270 [ + + ]: 1267 : t_group_info && ( s[TAUT_YES].nLenLinearCTTautomer > 0 && /* ordinary tautomerism */
5271 [ + - + + ]: 18 : ((t_group_info->t_group && t_group_info->num_t_groups > 0) ||
5272 : : /* protons have been moved */
5273 [ - + ]: 13 : ( t_group_info->tni.bNormalizationFlags & FLAG_NORM_CONSIDER_TAUT ) ||
5274 : : /* tautomerism due to possible isotopic proton exchange */
5275 [ # # ]: 0 : (t_group_info->nNumIsotopicEndpoints > 1 &&
5276 [ # # ]: 0 : ( t_group_info->bTautFlagsDone & ( TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE ) ))) ))
5277 : : {
5278 : : /* tautomeric: (1) has tautomeric atoms OR
5279 : : (2) H-atoms have been rearranged due to proton addition/removal OR
5280 : : (3) Found isotopic H-atoms on tautomeric or hetero atoms
5281 : : */
5282 : 18 : iBase = TAUT_YES;
5283 : 18 : bReqTaut = 1;
5284 [ - + ]: 18 : bUseIsoAuxBase[iBase] = ( s[iBase].nLenIsotopicEndpoints > 1 ) &&
5285 [ # # ]: 0 : ( t_group_info->bTautFlagsDone & ( TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE ) );
5286 [ - + - - ]: 18 : if (at[TAUT_NON] && s[TAUT_NON].nLenCT)
5287 : : {
5288 : 0 : iOther = TAUT_NON; /* tautomeric and non-tautomeric */
5289 : 0 : bReqNonTaut = 1;
5290 : : }
5291 : : else
5292 : : {
5293 : 18 : iOther = iBase; /* tautomeric only */
5294 : 18 : bReqNonTaut = 0;
5295 : : }
5296 : : }
5297 : :
5298 [ - + - - ]: 1249 : else if (at[TAUT_NON] && s[TAUT_NON].nLenCT)
5299 : : {
5300 : : /* force pure non-tautomeric processing; happens for testing only */
5301 : 0 : iBase = TAUT_NON;
5302 : 0 : bReqTaut = 0;
5303 : 0 : iOther = iBase;
5304 : 0 : bReqNonTaut = 1;
5305 : 0 : num_at_tg = num_atoms;
5306 : : }
5307 : :
5308 [ + - + - ]: 1249 : else if (at[TAUT_YES] && s[TAUT_YES].nLenCT)
5309 : : {
5310 : : /* although the user requested tautomeric processing, tautomerism has not been found */
5311 : : /* however, the results should be saved in the TAUT_YES elements of the arrays */
5312 : 1249 : iBase = TAUT_YES;
5313 : 1249 : bReqTaut = 0;
5314 : 1249 : bUseIsoAuxBase[iBase] = ( s[iBase].nLenIsotopicEndpoints > 1 );
5315 : 1249 : iOther = iBase;
5316 : 1249 : bReqNonTaut = 1;
5317 : 1249 : num_at_tg = num_atoms;
5318 : : }
5319 : : else
5320 : : {
5321 : 0 : ret = CT_UNKNOWN_ERR;
5322 : 0 : goto exit_error;
5323 : : }
5324 : :
5325 : :
5326 [ + + ]: 1267 : if (bReqTaut)
5327 : : {
5328 : : /* save "process isotopic" mark; temporarily set it to NO */
5329 : 18 : bTautIgnoreIsotopic = t_group_info->bIgnoreIsotopic;
5330 : 18 : t_group_info->bIgnoreIsotopic = 1;
5331 : : }
5332 : :
5333 : : /* djb-rwth: removing redundant code */
5334 : :
5335 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5336 : : num_iso_Hfixed =
5337 : : len_iso_sort_key_Hfixed =
5338 : : maxlen_iso_sort_key_Hfixed = 0;
5339 : : #endif
5340 : :
5341 : : /* prepare initial data */
5342 : 1267 : at_base = at[iBase];
5343 : 1267 : at_other = at[iOther];
5344 : 1267 : pAtomInvariant = (ATOM_INVARIANT2 *) inchi_calloc( num_max, sizeof( pAtomInvariant[0] ) );
5345 : 1267 : nSymmRankNoH = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankNoH[0] ) );
5346 : 1267 : nCanonRankNoH = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankNoH[0] ) );
5347 : 1267 : nAtomNumberCanonNoH = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonNoH[0] ) );
5348 : 1267 : nRank = (AT_RANK *) inchi_calloc( num_max, sizeof( nRank[0] ) );
5349 : 1267 : nAtomNumber = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumber[0] ) );
5350 : 1267 : nTempRank = (AT_RANK *) inchi_calloc( num_max, sizeof( nTempRank[0] ) );
5351 : :
5352 [ + - + - ]: 1267 : if (!pAtomInvariant ||
5353 [ + - + - : 1267 : !nSymmRankNoH || !nCanonRankNoH || !nAtomNumberCanonNoH ||
+ - ]
5354 [ + - - + ]: 1267 : !nRank || !nAtomNumber || !nTempRank)
5355 : : {
5356 : 0 : goto exit_error_alloc;
5357 : : }
5358 : :
5359 : : #if ( USE_AUX_RANKING == 1 )
5360 : 1267 : nRankAux = (AT_RANK *) inchi_calloc( num_max, sizeof( nRankAux[0] ) );
5361 : 1267 : nAtomNumberAux = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberAux[0] ) );
5362 : 1267 : pAtomInvariantAux = (ATOM_INVARIANT2 *) inchi_malloc( num_max * sizeof( pAtomInvariantAux[0] ) );
5363 [ + - + - : 1267 : if (!nRankAux || !nAtomNumberAux || !pAtomInvariantAux)
- + ]
5364 : : {
5365 : 0 : goto exit_error_alloc;
5366 : : }
5367 : : #endif
5368 : :
5369 [ + + ]: 1267 : if (bReqTaut)
5370 : : {
5371 [ - + ]: 18 : if (!( NeighList[TAUT_YES] =
5372 : 18 : CreateNeighList( num_atoms, num_at_tg, at_base, 0, t_group_info ) ))
5373 : : {
5374 : 0 : goto exit_error_alloc;
5375 : : }
5376 : : /* needed for the hydrogenless structure */
5377 [ - + ]: 18 : if (!( NeighList[TAUT_NON] =
5378 : 18 : CreateNeighList( num_atoms, num_atoms, at_base, 0, NULL ) ))
5379 : : {
5380 : 0 : goto exit_error_alloc;
5381 : : }
5382 : : }
5383 : : else
5384 : : {
5385 [ - + ]: 1249 : if (!( NeighList[TAUT_NON] =
5386 : 1249 : CreateNeighList( num_atoms, num_atoms, at_base, 0, NULL ) ))
5387 : : {
5388 : 0 : goto exit_error_alloc;
5389 : : }
5390 : 1249 : NeighList[TAUT_YES] = NULL;
5391 : :
5392 : : INCHI_HEAPCHK
5393 : : }
5394 : :
5395 : :
5396 : : /* avoid memory leaks in case of error */
5397 : : /*
5398 : : pBCN->ftcn[TAUT_NON].NeighList = NeighList[TAUT_NON];
5399 : : pBCN->ftcn[TAUT_YES].NeighList = NeighList[TAUT_YES];
5400 : : */
5401 : 1267 : pBCN->nMaxLenRankStack = 0;
5402 : 1267 : pBCN->num_max = num_max; /* allocated nRank[] arrays lengths in pRankStack */
5403 : 1267 : pBCN->num_at_tg = num_at_tg; /* all of the following arrays have this length */
5404 : 1267 : pBCN->num_atoms = num_atoms;
5405 : 1267 : pBCN->ulTimeOutTime = ulTimeOutTime;
5406 : :
5407 : : /* initial partitioning of a hydrogenless skeleton: fill out the inveriant */
5408 : 1267 : FillOutAtomInvariant2( at_base,
5409 : : num_atoms,
5410 : : num_atoms,
5411 : : pAtomInvariant,
5412 : : 1 /*bIgnoreIsotopic*/,
5413 : : 0 /*bHydrogensInRanks*/,
5414 : : 0 /*bHydrogensFixedInRanks*/,
5415 : : 0 /*bTaut=bDigraph*/,
5416 : : 0 /* bTautGroupsOnly */,
5417 : : NULL /*t_group_info*/ );
5418 : :
5419 : : /* initial partitioning of a hydrogenless skeleton: create equitable partition (assign initial ranks) */
5420 : 1267 : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariant, nRank, nAtomNumber, pCG );
5421 : :
5422 : 1267 : lCount = 0;
5423 : :
5424 : : /* make equitable partition in pBCN->pRankStack[0,1] */
5425 : 1267 : nNumCurrRanks = DifferentiateRanks2( pCG,
5426 : : num_atoms,
5427 : : NeighList[TAUT_NON],
5428 : : nNumCurrRanks, nRank,
5429 : : nTempRank,
5430 : : nAtomNumber,
5431 : : &lCount,
5432 : : 0 /* 0 means use qsort */ );
5433 : :
5434 : : /* allocate partition stack */
5435 : 1267 : nMaxLenRankStack = 2 * ( num_at_tg - nNumCurrRanks ) + 8; /* was 2*(...) + 6 */
5436 : 1267 : pBCN->pRankStack = (AT_RANK **) inchi_calloc( nMaxLenRankStack, sizeof( pBCN->pRankStack[0] ) );
5437 [ - + ]: 1267 : if (!pBCN->pRankStack)
5438 : : {
5439 : 0 : pBCN->nMaxLenRankStack = 0; /* avoid memory leaks in case of error */
5440 : 0 : goto exit_error_alloc;
5441 : : }
5442 : 1267 : pBCN->nMaxLenRankStack = nMaxLenRankStack; /* avoid memory leaks in case of error */
5443 : : /* init partition stack */
5444 : 1267 : pBCN->pRankStack[0] = nRank;
5445 : 1267 : pBCN->pRankStack[1] = nAtomNumber;
5446 : :
5447 : : /********************************************************************************************/
5448 : : /* get NoH/no taut groups canonical numbering, connection table, and equivalence partition */
5449 : : /********************************************************************************************/
5450 : :
5451 : : /* pointers */
5452 : 1267 : pCD[iOther].LinearCT = NULL;
5453 : 1267 : pCD[iOther].NumH = NULL;
5454 : 1267 : pCD[iOther].NumHfixed = NULL;
5455 : 1267 : pCD[iOther].iso_sort_key = NULL;
5456 : 1267 : pCD[iOther].iso_exchg_atnos = NULL;
5457 : :
5458 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5459 : : pCD[iOther].iso_sort_key_Hfixed = NULL;
5460 : : #endif
5461 : :
5462 : : /* variables - unchanged */
5463 : 1267 : pCD[iOther].ulTimeOutTime = pBCN->ulTimeOutTime;
5464 : 1267 : pCD[iOther].nMaxLenLinearCT = s[iOther].nLenCTAtOnly + 1;
5465 : : /* return values & input/output */
5466 : 1267 : pCD[iOther].nLenLinearCT = s[iOther].nLenCTAtOnly;
5467 : 1267 : pCD[iOther].nLenCTAtOnly = s[iOther].nLenCTAtOnly;
5468 : 1267 : pCD[iOther].lenNumH = 0;
5469 : 1267 : pCD[iOther].lenNumHfixed = 0;
5470 : 1267 : pCD[iOther].len_iso_sort_key = 0;
5471 : 1267 : pCD[iOther].maxlen_iso_sort_key = 0;
5472 : 1267 : pCD[iOther].len_iso_exchg_atnos = 0;
5473 : 1267 : pCD[iOther].maxlen_iso_exchg_atnos = 0;
5474 : :
5475 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5476 : : pCD[iOther].len_iso_sort_key_Hfixed = 0;
5477 : : pCD[iOther].maxlen_iso_sort_key_Hfixed = 0;
5478 : : #endif
5479 : :
5480 : 1267 : ret = CanonGraph01( ic, pCG, num_atoms, num_atoms, num_max, 0,
5481 : 1267 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
5482 : : nSymmRankNoH, nCanonRankNoH, nAtomNumberCanonNoH,
5483 : 1267 : pCD + iOther, pCC, NULL, &Ct_NoH, LargeMolecules );
5484 : :
5485 [ - + ]: 1267 : if (ret < 0)
5486 : : {
5487 : 0 : goto exit_error;
5488 : : }
5489 : :
5490 : : /* update initial partitioning */
5491 : 1267 : nNumCurrRanks = FixCanonEquivalenceInfo( pCG, num_atoms, nSymmRankNoH, nRank, nTempRank, nAtomNumber, &bChanged ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
5492 : :
5493 : : /* repartition if necessary */
5494 [ - + ]: 1267 : if (bChanged & 3)
5495 : : {
5496 [ # # ]: 0 : if (Ct_NoH)
5497 : : {
5498 : 0 : CTableFree( Ct_NoH );
5499 [ # # ]: 0 : inchi_free( Ct_NoH );
5500 : 0 : Ct_NoH = NULL;
5501 : : }
5502 : 0 : pCD[iOther].nCanonFlags |= CANON_FLAG_NO_H_RECANON;
5503 : :
5504 : 0 : ret = CanonGraph02( ic, pCG, num_atoms, num_atoms, num_max, 0,
5505 : 0 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
5506 : : nSymmRankNoH, nCanonRankNoH, nAtomNumberCanonNoH,
5507 : 0 : pCD + iOther, pCC, NULL, &Ct_NoH, LargeMolecules );
5508 : :
5509 [ # # ]: 0 : if (ret < 0)
5510 : : {
5511 : 0 : goto exit_error;
5512 : : }
5513 : : }
5514 : :
5515 : :
5516 : : /********************************************************************************/
5517 : : /* get NoTautH canonical numbering, connection table, and equivalence partition */
5518 : : /********************************************************************************/
5519 : 1267 : maxlenNumHNoTautH = num_atoms + 1;
5520 : 1267 : nSymmRankNoTautH = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankNoTautH[0] ) );
5521 : 1267 : nCanonRankNoTautH = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankNoTautH[0] ) );
5522 : 1267 : nAtomNumberCanonNoTautH = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonNoTautH[0] ) );
5523 : 1267 : numHNoTautH = (NUM_H *) inchi_calloc( maxlenNumHNoTautH, sizeof( numHNoTautH[0] ) );
5524 [ + - + - : 1267 : if (!numHNoTautH || !nSymmRankNoTautH || !nCanonRankNoTautH || !nAtomNumberCanonNoTautH)
+ - - + ]
5525 : : {
5526 : 0 : goto exit_error_alloc;
5527 : : }
5528 : :
5529 : : /* find number of H atoms attached to not-a-tautomeric-endpoint atoms */
5530 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
5531 : : {
5532 [ + + + + ]: 9207 : numHNoTautH[i] = ( !at_base[i].endpoint && at_base[i].num_H ) ? at_base[i].num_H + BASE_H_NUMBER : EMPTY_H_NUMBER;
5533 : : }
5534 : :
5535 : : /* pointers */
5536 : 1267 : pCD[iOther].LinearCT = NULL;
5537 : 1267 : pCD[iOther].NumH = numHNoTautH;
5538 : 1267 : pCD[iOther].NumHfixed = NULL;
5539 : 1267 : pCD[iOther].iso_sort_key = NULL;
5540 : 1267 : pCD[iOther].iso_exchg_atnos = NULL;
5541 : :
5542 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5543 : : pCD[iOther].iso_sort_key_Hfixed = NULL;
5544 : : #endif
5545 : :
5546 : : /* variables - unchanged */
5547 : 1267 : pCD[iOther].ulTimeOutTime = pBCN->ulTimeOutTime;
5548 : 1267 : pCD[iOther].nMaxLenLinearCT = s[iOther].nLenCTAtOnly + 1;
5549 : 1267 : pCD[iOther].maxlenNumH = maxlenNumHNoTautH;
5550 : : /* return values & input/output */
5551 : 1267 : pCD[iOther].nLenLinearCT = s[iOther].nLenCTAtOnly;
5552 : 1267 : pCD[iOther].nLenCTAtOnly = s[iOther].nLenCTAtOnly;
5553 : 1267 : pCD[iOther].lenNumH = lenNumHNoTautH = num_atoms;
5554 : 1267 : pCD[iOther].lenNumHfixed = 0;
5555 : 1267 : pCD[iOther].len_iso_sort_key = 0;
5556 : 1267 : pCD[iOther].maxlen_iso_sort_key = 0;
5557 : 1267 : pCD[iOther].len_iso_exchg_atnos = 0;
5558 : 1267 : pCD[iOther].maxlen_iso_exchg_atnos = 0;
5559 : :
5560 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5561 : : pCD[iOther].len_iso_sort_key_Hfixed = 0;
5562 : : pCD[iOther].maxlen_iso_sort_key_Hfixed = 0;
5563 : : #endif
5564 : :
5565 : 1267 : pCD[iOther].nAuxRank = NULL;
5566 : :
5567 : : /* check whether we need NoTautH cononicalization */
5568 : 1267 : memset( nTempRank, 0, num_max * sizeof( nTempRank[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5569 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
5570 : : {
5571 [ + + ]: 9207 : if (nTempRank[nSymmRankNoH[i] - 1] < i)
5572 : : {
5573 : 7940 : nTempRank[nSymmRankNoH[i] - 1] = i; /* greatest class representative */
5574 : : }
5575 : : }
5576 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
5577 : : {
5578 [ - + ]: 9207 : if (numHNoTautH[i] != numHNoTautH[nTempRank[nSymmRankNoH[i] - 1]])
5579 : : {
5580 : 0 : pCD[iOther].nCanonFlags |= CANON_FLAG_NO_TAUT_H_DIFF;
5581 : 0 : break; /* atoms so far found to be equivalent have different number of H; the canonicalization is needed */
5582 : : }
5583 : : }
5584 : :
5585 : : /* i = 0; *//* debug: force to call the canonicalization */
5586 [ - + ]: 1267 : if (i < num_atoms)
5587 : : {
5588 : : /* needs canonicalization */
5589 : : /* get aux canonical ranking of the structure with attached H */
5590 : :
5591 : : #if ( USE_AUX_RANKING == 1 )
5592 : : /* refine no-H partition according to not-a-taut-H distribution */
5593 : 0 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5594 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
5595 : : {
5596 : 0 : pAtomInvariantAux[i].val[0] = nSymmRankNoH[i];
5597 : 0 : pAtomInvariantAux[i].val[1] = numHNoTautH[i]; /* additional differentiation: not-a-taut-H distribution */
5598 : : }
5599 : :
5600 : : /* initial partitioning */
5601 : 0 : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
5602 : :
5603 : : /* make equitable partition */
5604 : 0 : nNumCurrRanks = DifferentiateRanks2( pCG, num_atoms, NeighList[TAUT_NON],
5605 : : nNumCurrRanks, nRankAux,
5606 : : nTempRank, nAtomNumberAux, &lCount, 0 /* 0 means use qsort */ ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
5607 : :
5608 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
5609 : 0 : pCD[iOther].nAuxRank = nRankAux;
5610 : : #endif
5611 : :
5612 : 0 : ret = CanonGraph03( ic, pCG, num_atoms, num_atoms, num_max, 1 /* digraph?? was 0 */,
5613 : 0 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
5614 : : nSymmRankNoTautH, nCanonRankNoTautH, nAtomNumberCanonNoTautH,
5615 : 0 : pCD + iOther, pCC, &Ct_NoH, &Ct_NoTautH, LargeMolecules );
5616 : :
5617 [ # # ]: 0 : if (ret < 0)
5618 : : {
5619 : 0 : goto exit_error;
5620 : : }
5621 : :
5622 : : /* in case of non-tautomeric structure the final results are in:
5623 : :
5624 : : nSymmRankNoTautH
5625 : : nCanonRankNoTautH
5626 : : nAtomNumberCanonNoTautH
5627 : : Ct_NoTautH
5628 : : numHNoTautH (original H positions)
5629 : : */
5630 : : } /* if ( i < num_atoms ) */
5631 : :
5632 : : else
5633 : :
5634 : : {
5635 : :
5636 : : /* copy the results of the previous (no H) canonicalization */
5637 : : /* in this case numHNoTautH[] is not needed for the next canonicalization(s) */
5638 [ + - + - ]: 2534 : if (( Ct_Temp = (ConTable *) inchi_calloc( 1, sizeof( *Ct_Temp ) ) ) &&
5639 : 1267 : CTableCreate( Ct_Temp, num_atoms, pCD + iOther ))
5640 : : {
5641 : 1267 : CtFullCopy( Ct_Temp, Ct_NoH );
5642 : : /* since Ct_NoH does not have Ct_NoH->NumH we have to fill out Ct_Temp->NumH separately */
5643 [ + + ]: 10474 : for (i = 0; i < num_atoms; i++)
5644 : : {
5645 : 9207 : Ct_Temp->NumH[nCanonRankNoH[i] - 1] = numHNoTautH[i];
5646 : : /*Ct_Temp->NumH[i] = numHNoTautH[nAtomNumberCanonNoH[i]]; -- alternative */
5647 : : }
5648 : 1267 : Ct_Temp->lenNumH = num_atoms;
5649 : : }
5650 : : else
5651 : : {
5652 : 0 : goto exit_error_alloc;
5653 : : }
5654 : :
5655 : 1267 : Ct_NoTautH = Ct_Temp;
5656 : 1267 : Ct_Temp = NULL;
5657 : : /* djb-rwth: functions replaced with their safe C11 variants */
5658 : 1267 : memcpy(nSymmRankNoTautH, nSymmRankNoH, num_atoms * sizeof(nSymmRankNoTautH[0]));
5659 : 1267 : memcpy(nCanonRankNoTautH, nCanonRankNoH, num_atoms * sizeof(nCanonRankNoTautH[0]));
5660 : 1267 : memcpy(nAtomNumberCanonNoTautH, nAtomNumberCanonNoH, num_atoms * sizeof(nAtomNumberCanonNoTautH[0]));
5661 : : }
5662 : :
5663 : : /* in case of non-tautomeric component this is the final result */
5664 : : /* i = CtFullCompare( Ct_NoTautH, Ct_Temp, num_atoms, 0, 0 );*/
5665 : :
5666 : : /*******************************************************************************************/
5667 : : /* If only Isotopic atoms and isotopic H, tautomerism has not been found: */
5668 : : /* get isotopic canonical numbering, connection table, and equivalence partition */
5669 : : /*******************************************************************************************/
5670 : :
5671 [ + + + - : 1267 : if (s[iOther].num_isotopic_atoms && !s[iOther].bIgnoreIsotopic && !bReqTaut && bReqNonTaut)
+ + + - ]
5672 : : {
5673 : :
5674 : 6 : maxlen_iso_sort_key_NoTautH = num_atoms + 1;
5675 : 6 : nSymmRankNoTautHIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankNoTautHIso[0] ) );
5676 : 6 : nCanonRankNoTautHIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankNoTautHIso[0] ) );
5677 : 6 : nAtomNumberCanonNoTautHIso = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonNoTautHIso[0] ) );
5678 : 6 : iso_sort_key_NoTautH = (AT_ISO_SORT_KEY *) inchi_calloc( maxlen_iso_sort_key_NoTautH, sizeof( iso_sort_key_NoTautH[0] ) );
5679 : :
5680 [ + - + - : 6 : if (!nSymmRankNoTautHIso || !nCanonRankNoTautHIso || !nAtomNumberCanonNoTautHIso || !iso_sort_key_NoTautH)
+ - - + ]
5681 : : {
5682 : 0 : goto exit_error_alloc;
5683 : : }
5684 : :
5685 : : /* fill out isotopic non-tautomeric keys */
5686 : 6 : num_iso_NoTautH = 0;
5687 [ + + ]: 5009 : for (i = 0; i < num_atoms; i++)
5688 : : {
5689 [ - + ]: 5003 : if (at_base[i].endpoint)
5690 : : {
5691 : : /* should not happen */
5692 : 0 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, 0, 0, 0 );
5693 : : }
5694 : : else
5695 : : {
5696 : 5003 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
5697 : : }
5698 [ + + ]: 5003 : if (iso_sort_key)
5699 : : {
5700 : 6 : iso_sort_key_NoTautH[i] = iso_sort_key;
5701 : 6 : num_iso_NoTautH++;
5702 : : }
5703 : : else
5704 : : {
5705 : 4997 : iso_sort_key_NoTautH[i] = EMPTY_ISO_SORT_KEY;
5706 : : }
5707 : : }
5708 : :
5709 : : /* pointers */
5710 : 6 : pCD[iOther].LinearCT = NULL; /* LinearCT; */
5711 : 6 : pCD[iOther].NumH = numHNoTautH;
5712 : 6 : pCD[iOther].NumHfixed = NULL;
5713 : 6 : pCD[iOther].iso_sort_key = iso_sort_key_NoTautH;
5714 : 6 : pCD[iOther].iso_exchg_atnos = NULL;
5715 : :
5716 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5717 : : pCD[iOther].iso_sort_key_Hfixed = NULL;
5718 : : #endif
5719 : :
5720 : : /* variables - unchanged */
5721 : 6 : pCD[iOther].ulTimeOutTime = pBCN->ulTimeOutTime;
5722 : 6 : pCD[iOther].nMaxLenLinearCT = s[iOther].nLenCTAtOnly + 1;
5723 : 6 : pCD[iOther].maxlenNumH = maxlenNumHNoTautH;
5724 : : /* return values & input/output */
5725 : 6 : pCD[iOther].nLenLinearCT = s[iOther].nLenCTAtOnly;
5726 : 6 : pCD[iOther].nLenCTAtOnly = s[iOther].nLenCTAtOnly;
5727 : 6 : pCD[iOther].lenNumH = lenNumHNoTautH /*= num_atoms*/;
5728 : 6 : pCD[iOther].lenNumHfixed = 0;
5729 : 6 : pCD[iOther].len_iso_sort_key = len_iso_sort_key_NoTautH = num_atoms;
5730 : 6 : pCD[iOther].maxlen_iso_sort_key = maxlen_iso_sort_key_NoTautH;
5731 : 6 : pCD[iOther].len_iso_exchg_atnos = 0;
5732 : 6 : pCD[iOther].maxlen_iso_exchg_atnos = 0;
5733 : :
5734 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5735 : : pCD[iOther].len_iso_sort_key_Hfixed = 0;
5736 : : pCD[iOther].maxlen_iso_sort_key_Hfixed = 0;
5737 : : #endif
5738 : :
5739 : 6 : pCD[iOther].nAuxRank = NULL;
5740 : :
5741 [ + - ]: 6 : if (num_iso_NoTautH)
5742 : : {
5743 : : /* check whether we need NoTautH cononicalization */
5744 : 6 : memset( nTempRank, 0, num_max * sizeof( nTempRank[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5745 [ + + ]: 5009 : for (i = 0; i < num_atoms; i++)
5746 : : {
5747 [ + + ]: 5003 : if (nTempRank[nSymmRankNoTautH[i] - 1] < i)
5748 : : {
5749 : 4997 : nTempRank[nSymmRankNoTautH[i] - 1] = i; /* greatest class representative */
5750 : : }
5751 : : }
5752 [ + + ]: 3007 : for (i = 0; i < num_atoms; i++)
5753 : : {
5754 [ + + ]: 3004 : if (iso_sort_key_NoTautH[i] != iso_sort_key_NoTautH[nTempRank[nSymmRankNoTautH[i] - 1]])
5755 : : {
5756 : 3 : pCD[iOther].nCanonFlags |= CANON_FLAG_ISO_ONLY_NON_TAUT_DIFF;
5757 : 3 : break; /* atoms so far found to be equivalent differ in isotopes; the canonicalization is needed */
5758 : : }
5759 : : }
5760 : : }
5761 : : else
5762 : : {
5763 : 0 : i = num_atoms;
5764 : : }
5765 : : /* i = 0; *//* debug: force to call the canonicalization */
5766 [ + + ]: 6 : if (i < num_atoms)
5767 : : {
5768 : : /* we need canonicalization */
5769 : : /* get aux canonical ranking of the structure with isotopic non-tautomeric H */
5770 : :
5771 : : #if ( USE_AUX_RANKING == 1 )
5772 : : /* refine no-taut-H partition according to non-taut H isotopic distribution */
5773 : 3 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5774 [ + + ]: 3003 : for (i = 0; i < num_atoms; i++)
5775 : : {
5776 : 3000 : pAtomInvariantAux[i].val[0] = nSymmRankNoTautH[i];
5777 : 3000 : pAtomInvariantAux[i].iso_sort_key = iso_sort_key_NoTautH[i]; /* additional differentiation */
5778 : : }
5779 : : /* initial ranks for non-taut H isotopic distribution */
5780 : 3 : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
5781 : : /* make equitable */
5782 : 3 : nNumCurrRanks = DifferentiateRanks2( pCG, num_atoms, NeighList[TAUT_NON],
5783 : : nNumCurrRanks, nRankAux,
5784 : : nTempRank, nAtomNumberAux, &lCount, 0 /* 0 means use qsort */ ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
5785 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
5786 : 3 : pCD[iOther].nAuxRank = nRankAux;
5787 : : #endif
5788 : :
5789 : 3 : ret = CanonGraph04( ic, pCG,
5790 : : num_atoms, num_atoms, num_max, 1 /* digraph?? was 0 */,
5791 : 3 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
5792 : : nSymmRankNoTautHIso, nCanonRankNoTautHIso, nAtomNumberCanonNoTautHIso,
5793 : 3 : pCD + iOther, pCC, &Ct_NoTautH, &Ct_NoTautHIso, LargeMolecules );
5794 [ - + ]: 3 : if (ret < 0)
5795 : : {
5796 : 0 : goto exit_error;
5797 : : }
5798 : :
5799 : : /* in case of non-tautomeric structure the final results are in:
5800 : :
5801 : : nSymmRankNoTautHIso
5802 : : nCanonRankNoTautHIso
5803 : : nAtomNumberCanonNoTautHIso
5804 : : Ct_NoTautHIso
5805 : : iso_sort_key_NoTautH (original isotopic atom positions)
5806 : : */
5807 : : }
5808 : : else
5809 : : {
5810 : : /* copy the results of the previous (no taut H) canonicalization */
5811 : : /* in this case numHNoTautH[] is not needed for the next canonicalization(s) */
5812 [ + - + - ]: 6 : if (( Ct_Temp = (ConTable *) inchi_calloc( 1, sizeof( *Ct_Temp ) ) ) &&
5813 : 3 : CTableCreate( Ct_Temp, num_atoms, pCD + iOther ))
5814 : : {
5815 : 3 : CtFullCopy( Ct_Temp, Ct_NoTautH );
5816 : : /* since Ct_NoTautH does not have Ct_NoTautH->iso_sort_key we have to fill out Ct_Temp->iso_sort_key separately */
5817 [ + + ]: 2006 : for (i = 0; i < num_atoms; i++)
5818 : : {
5819 : 2003 : Ct_Temp->iso_sort_key[nCanonRankNoTautH[i] - 1] = iso_sort_key_NoTautH[i];
5820 : : }
5821 : 3 : Ct_Temp->len_iso_sort_key = num_atoms;
5822 : : }
5823 : : else
5824 : : {
5825 : 0 : goto exit_error_alloc;
5826 : : }
5827 : 3 : Ct_NoTautHIso = Ct_Temp;
5828 : 3 : Ct_Temp = NULL;
5829 : 3 : memcpy(nSymmRankNoTautHIso, nSymmRankNoTautH, num_atoms * sizeof(nSymmRankNoTautHIso[0]));
5830 : 3 : memcpy(nCanonRankNoTautHIso, nCanonRankNoTautH, num_atoms * sizeof(nCanonRankNoTautHIso[0]));
5831 : 3 : memcpy(nAtomNumberCanonNoTautHIso, nAtomNumberCanonNoTautH, num_atoms * sizeof(nAtomNumberCanonNoTautHIso[0]));
5832 : : }
5833 : : /* in case of non-tautomeric component this is the final result */
5834 : : /* i = CtFullCompare( Ct_NoTautHIso, Ct_Temp, num_atoms, 0, 0 );*/
5835 : : }
5836 : :
5837 : :
5838 [ + + ]: 1267 : if (bReqTaut)
5839 : : {
5840 : : /*****************************************************************************/
5841 : : /* Tautomeric Structure Canonicalizaton: */
5842 : : /* get base canonical numbering, connection table, and equivalence partition */
5843 : : /*****************************************************************************/
5844 : : /* find H atoms attached to non-tautomeric-endpoints and to tautomeric endpoints */
5845 : 18 : maxlenNumH = num_atoms + T_NUM_NO_ISOTOPIC*( num_at_tg - num_atoms ) + 1; /* including negative charges */
5846 : 18 : nSymmRankBase = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankBase[0] ) );
5847 : 18 : nCanonRankBase = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankBase[0] ) );
5848 : 18 : nAtomNumberCanonBase = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonBase[0] ) );
5849 : 18 : numH = (NUM_H *) inchi_calloc( maxlenNumH, sizeof( numH[0] ) );
5850 : :
5851 [ + - + - : 18 : if (!numH || !nSymmRankBase || !nCanonRankBase || !nAtomNumberCanonBase)
+ - - + ]
5852 : : {
5853 : 0 : goto exit_error_alloc;
5854 : : }
5855 : :
5856 : : /* non-tautomeric H counts */
5857 [ + + ]: 67 : for (i = 0; i < num_atoms; i++)
5858 : : {
5859 [ + + + + ]: 49 : numH[i] = ( !at_base[i].endpoint && at_base[i].num_H ) ? at_base[i].num_H + BASE_H_NUMBER : EMPTY_H_NUMBER;
5860 : : }
5861 : :
5862 : : /* tautomeric H and negative charge counts */
5863 [ + + ]: 28 : for (i = k = num_atoms; i < num_at_tg; i++)
5864 : : {
5865 : 10 : m = i - num_atoms;
5866 [ + + ]: 30 : for (j = 0; j < T_NUM_NO_ISOTOPIC; j++)
5867 : : {
5868 : : /* non-zeroes for j=1 are negative charge counts; T_NUM_NO_ISOTOPIC=2 entry per t-group */
5869 [ + + ]: 20 : numH[k++] = t_group_info->t_group[m].num[j] ? t_group_info->t_group[m].num[j] + BASE_H_NUMBER : EMPTY_H_NUMBER;
5870 : : }
5871 : : }
5872 : :
5873 : : /* pointers */
5874 : 18 : pCD[iBase].LinearCT = NULL;
5875 : 18 : pCD[iBase].NumH = numH; /* num_atoms non-tautomeric H; num_tg pairs of H and (-) in t-groups */
5876 : 18 : pCD[iBase].NumHfixed = NULL;
5877 : 18 : pCD[iBase].iso_sort_key = NULL;
5878 : 18 : pCD[iBase].iso_exchg_atnos = NULL;
5879 : :
5880 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5881 : : pCD[iBase].iso_sort_key_Hfixed = NULL;
5882 : : #endif
5883 : :
5884 : : /* variables - unchanged */
5885 : 18 : pCD[iBase].ulTimeOutTime = pBCN->ulTimeOutTime;
5886 : 18 : pCD[iBase].nMaxLenLinearCT = s[iBase].nLenCT + 1;
5887 : 18 : pCD[iBase].maxlenNumH = maxlenNumH;
5888 : : /* return values & input/output */
5889 : 18 : pCD[iBase].nLenLinearCT = s[iBase].nLenCT;
5890 : 18 : pCD[iBase].nLenCTAtOnly = s[iBase].nLenCTAtOnly;
5891 : 18 : pCD[iBase].lenNumH = lenNumH = k;
5892 : 18 : pCD[iBase].lenNumHfixed = 0;
5893 : 18 : pCD[iBase].len_iso_sort_key = 0;
5894 : 18 : pCD[iBase].maxlen_iso_sort_key = 0;
5895 : 18 : pCD[iBase].len_iso_exchg_atnos = 0;
5896 : 18 : pCD[iBase].maxlen_iso_exchg_atnos = 0;
5897 : :
5898 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
5899 : : pCD[iBase].len_iso_sort_key_Hfixed = 0;
5900 : : pCD[iBase].maxlen_iso_sort_key_Hfixed = 0;
5901 : : #endif
5902 : :
5903 : 18 : pCD[iBase].nAuxRank = NULL;
5904 : :
5905 : : /* make sure the initial partition is equitable (at this point t-groups do not have ranks yet) */
5906 : 18 : FillOutAtomInvariant2( at_base,
5907 : : num_atoms,
5908 : : num_at_tg,
5909 : : pAtomInvariant,
5910 : : 1 /*bIgnoreIsotopic*/,
5911 : : 0 /*bHydrogensInRanks*/,
5912 : : 0 /*bHydrogensFixedInRanks*/,
5913 : : 1 /*bTaut=bDigraph*/,
5914 : : 1 /* bTautGroupsOnly */,
5915 : : t_group_info );
5916 : :
5917 [ + + ]: 67 : for (i = 0; i < num_atoms; i++)
5918 : : {
5919 : 49 : pAtomInvariant[i].val[0] = pBCN->pRankStack[0][i];
5920 : : }
5921 : :
5922 : : /* initial ranks for t-group(s) only */
5923 : 18 : nNumCurrRanks = SetInitialRanks2( num_at_tg, pAtomInvariant, nRank, nAtomNumber, pCG );
5924 : :
5925 : : /* make equitable, call digraph procedure;
5926 : : pBCN->pRankStack[0] is nRank, pBCN->pRankStack[1] is nAtomNumber
5927 : : This should only split ranks of tautomeric groups */
5928 : 18 : nNumCurrRanks = DifferentiateRanks4( pCG, num_at_tg, NeighList[TAUT_YES],
5929 : 18 : nNumCurrRanks, pBCN->pRankStack[0], nTempRank /* temp array */,
5930 : 18 : pBCN->pRankStack[1], (AT_RANK) num_atoms, &lCount ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
5931 : :
5932 : : #if ( USE_AUX_RANKING == 1 )
5933 : : /* refine no-H partition according to non-taut H distribution */
5934 : 18 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5935 [ + + ]: 67 : for (i = 0; i < num_atoms; i++)
5936 : : {
5937 : 49 : pAtomInvariantAux[i].val[0] = nSymmRankNoTautH[i];
5938 : 49 : pAtomInvariantAux[i].val[1] = numH[i]; /* additional differentiation */
5939 : : }
5940 : : /*
5941 : : * djb-rwth: original badly written loop
5942 : : for (j = i; i < num_at_tg; i++)
5943 : : {
5944 : : pAtomInvariantAux[i].val[0] = nRank[i];
5945 : : }
5946 : : */
5947 [ + + ]: 28 : for (j = i; j < num_at_tg; j++) /* djb-rwth: corrected loop */
5948 : : {
5949 : 10 : pAtomInvariantAux[j].val[0] = nRank[j];
5950 : : }
5951 : : /* initial ranks for t-group(s) */
5952 : 18 : nNumCurrRanks = SetInitialRanks2( num_at_tg, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
5953 : : /* make equitable, call digraph procedure */
5954 : 18 : nNumCurrRanks = DifferentiateRanks4( pCG, num_at_tg, NeighList[TAUT_YES],
5955 : : nNumCurrRanks, nRankAux, nTempRank /* temp array */,
5956 : 18 : nAtomNumberAux, (AT_RANK) num_atoms, &lCount ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
5957 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
5958 : 18 : pCD[iBase].nAuxRank = nRankAux;
5959 : : #endif
5960 : :
5961 : 18 : ret = CanonGraph05( ic, pCG, num_atoms, num_at_tg, num_max, 1 /* digraph*/,
5962 : 18 : NeighList[TAUT_YES], (Partition *) pBCN->pRankStack,
5963 : : nSymmRankBase, nCanonRankBase, nAtomNumberCanonBase,
5964 : 18 : pCD + iBase, pCC, &Ct_NoTautH, &Ct_Base, LargeMolecules );
5965 [ - + ]: 18 : if (ret < 0)
5966 : : {
5967 : 0 : goto exit_error;
5968 : : }
5969 : :
5970 : : /* tautomeric isotopic structure */
5971 : : /**************************************************************************************/
5972 : : /* Isotopic atoms and isotopic H atoms and isotopic tautomeric groups */
5973 : : /* get isotopic canonical numbering, connection table, and equivalence partition */
5974 : : /**************************************************************************************/
5975 [ + + - + ]: 18 : if ((s[iBase].num_isotopic_atoms && !s[iBase].bIgnoreIsotopic) ||
5976 [ - + - - ]: 14 : (s[iBase].bHasIsotopicTautGroups && !bTautIgnoreIsotopic) ||
5977 [ - + - - ]: 14 : (bUseIsoAuxBase[iBase] && !bTautIgnoreIsotopic)) /* djb-rwth: addressing LLVM warning */
5978 : : {
5979 : :
5980 : 4 : t_group_info->bIgnoreIsotopic = bTautIgnoreIsotopic;
5981 : :
5982 : 4 : nSymmRankBaseIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankBaseIso[0] ) );
5983 : 4 : nCanonRankBaseIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankBaseIso[0] ) );
5984 : 4 : nAtomNumberCanonBaseIso = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonBaseIso[0] ) );
5985 [ - + ]: 4 : if (bUseIsoAuxBase[iBase])
5986 : : {
5987 : 0 : maxlen_iso_exchg_atnos = num_max + 1;
5988 : 0 : iso_exchg_atnos = (S_CHAR *) inchi_calloc( maxlen_iso_exchg_atnos, sizeof( iso_exchg_atnos[0] ) );
5989 : : }
5990 : 4 : maxlen_iso_sort_keyBase = num_max + 1; /* num_at_tg+1;*/
5991 : 4 : iso_sort_keyBase = (AT_ISO_SORT_KEY *) inchi_calloc( maxlen_iso_sort_keyBase, sizeof( iso_sort_keyBase[0] ) );
5992 [ + - + - : 4 : if (!nSymmRankBaseIso || !nCanonRankBaseIso || !nAtomNumberCanonBaseIso ||
+ - + - ]
5993 [ - + ]: 4 : !iso_sort_keyBase ||
5994 [ # # ]: 0 : (maxlen_iso_exchg_atnos && !iso_exchg_atnos)) /* djb-rwth: addressing LLVM warning */
5995 : : {
5996 : 0 : goto exit_error_alloc;
5997 : : }
5998 : : /* atoms */
5999 : 4 : num_iso_NoTautH = 0;
6000 : 4 : num_iso_NoAuxBase = 0;
6001 [ - + ]: 4 : if (iso_exchg_atnos)
6002 : : {
6003 : 0 : len_iso_exchg_atnos = num_at_tg;
6004 : : }
6005 [ + + ]: 8 : for (i = 0; i < num_atoms; i++)
6006 : : {
6007 [ + - - + : 4 : if (at_base[i].endpoint || (iso_exchg_atnos && ( at_base[i].cFlags & AT_FLAG_ISO_H_POINT ))) /* djb-rwth: addressing LLVM warning */
- - ]
6008 : : {
6009 : : /* tautomeric or may have exchangeable isotopic H */
6010 : 0 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, 0, 0, 0 );
6011 [ # # ]: 0 : if (iso_exchg_atnos)
6012 : : {
6013 : 0 : num_iso_NoAuxBase += !at_base[i].endpoint; /* these non-taut atom may exchange isotopic H as tautomeric atoms do */
6014 : : }
6015 : : }
6016 : : else
6017 : : {
6018 : : /* non-mobile H */
6019 : 4 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
6020 [ - + ]: 4 : if (iso_exchg_atnos)
6021 : : {
6022 : 0 : iso_exchg_atnos[i] = 1; /* atom cannot have exchangable isotopic H atom(s) */
6023 : : }
6024 : : }
6025 [ + - ]: 4 : if (iso_sort_key)
6026 : : {
6027 : 4 : num_iso_NoTautH++;
6028 : 4 : iso_sort_keyBase[i] = iso_sort_key;
6029 : : }
6030 : : else
6031 : : {
6032 : 0 : iso_sort_keyBase[i] = EMPTY_ISO_SORT_KEY;
6033 : : }
6034 : : }
6035 : :
6036 : : /* check marking and count of non-taut atoms that may exchange isotopic H -- debug only */
6037 [ - + ]: 4 : if (iso_exchg_atnos)
6038 : : {
6039 [ # # ]: 0 : if (num_iso_NoAuxBase != t_group_info->nIsotopicEndpointAtomNumber[0])
6040 : : {
6041 : 0 : ret = CT_ISOCOUNT_ERR;
6042 : 0 : goto exit_error;
6043 : : }
6044 [ # # ]: 0 : for (i = 1; i <= num_iso_NoAuxBase; i++)
6045 : : {
6046 : 0 : j = t_group_info->nIsotopicEndpointAtomNumber[i];
6047 [ # # # # ]: 0 : if (at_base[j].endpoint || !( at_base[j].cFlags & AT_FLAG_ISO_H_POINT ))
6048 : : {
6049 : 0 : ret = CT_ISOCOUNT_ERR;
6050 : 0 : goto exit_error;
6051 : : }
6052 : : }
6053 : : }
6054 : :
6055 : : /* t-groups */
6056 : 4 : num_iso_Base = 0;
6057 [ - + ]: 4 : if (iso_exchg_atnos)
6058 : : {
6059 [ # # ]: 0 : for (i = num_atoms; i < num_at_tg; i++)
6060 : : {
6061 : 0 : iso_sort_keyBase[i] = EMPTY_ISO_SORT_KEY; /* new mode: do not provide info about isotopic tautomeric H */
6062 : : }
6063 : : }
6064 : : else
6065 : : {
6066 [ - + ]: 4 : for (i = num_atoms; i < num_at_tg; i++)
6067 : : {
6068 : : /* should not happen anymore */
6069 : 0 : m = i - num_atoms;
6070 [ # # ]: 0 : if ((iso_sort_key = t_group_info->t_group[m].iWeight)) /* djb-rwth: addressing LLVM warning */
6071 : : {
6072 : : /* old approach: each t-group has its own isotopic "weight" */
6073 : 0 : num_iso_Base++;
6074 : 0 : iso_sort_keyBase[i] = iso_sort_key;
6075 : : }
6076 : : else
6077 : : {
6078 : 0 : iso_sort_keyBase[i] = EMPTY_ISO_SORT_KEY;
6079 : : }
6080 : : }
6081 : : }
6082 : :
6083 [ + - - + ]: 4 : if (!num_iso_NoAuxBase && iso_exchg_atnos)
6084 : : {
6085 : : /* all atoms that may exchange isotopic H are either tautomeric or not present */
6086 [ # # ]: 0 : inchi_free( iso_exchg_atnos );
6087 : 0 : iso_exchg_atnos = NULL;
6088 : 0 : len_iso_exchg_atnos = 0;
6089 : 0 : maxlen_iso_exchg_atnos = 0;
6090 : : }
6091 : :
6092 [ - + - - : 4 : if (!num_iso_NoTautH && !num_iso_Base && iso_sort_keyBase)
- - ]
6093 : : {
6094 : : /* no isotopic atoms present */
6095 [ # # ]: 0 : inchi_free( iso_sort_keyBase );
6096 : 0 : iso_sort_keyBase = NULL;
6097 : 0 : maxlen_iso_sort_keyBase = 0;
6098 : : }
6099 : : else
6100 : : {
6101 : 4 : len_iso_sort_keyBase = num_at_tg;
6102 : : }
6103 : :
6104 [ + - - + ]: 4 : if (!iso_exchg_atnos && !iso_sort_keyBase)
6105 : : {
6106 : : /* no isotopic part at all or only tautomeric groups */
6107 [ # # ]: 0 : inchi_free( nSymmRankBaseIso ); nSymmRankBaseIso = NULL;
6108 [ # # ]: 0 : inchi_free( nCanonRankBaseIso ); nCanonRankBaseIso = NULL;
6109 [ # # ]: 0 : inchi_free( nAtomNumberCanonBaseIso ); nAtomNumberCanonBaseIso = NULL;
6110 : : }
6111 : : else
6112 : : {
6113 : : /* proceed with tautomeric isotopic canonicalization */
6114 : : /* pointers */
6115 : 4 : pCD[iBase].LinearCT = NULL;
6116 : 4 : pCD[iBase].NumH = numH; /* num_atoms non-tautomeric H; num_tg pairs of H and (-) in t-groups */
6117 : 4 : pCD[iBase].NumHfixed = NULL;
6118 : 4 : pCD[iBase].iso_sort_key = iso_sort_keyBase;
6119 : 4 : pCD[iBase].iso_exchg_atnos = iso_exchg_atnos;
6120 : :
6121 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6122 : : pCD[iBase].iso_sort_key_Hfixed = NULL;
6123 : : #endif
6124 : :
6125 : : /* variables - unchanged */
6126 : 4 : pCD[iBase].ulTimeOutTime = pBCN->ulTimeOutTime;
6127 : 4 : pCD[iBase].nMaxLenLinearCT = s[iBase].nLenCT + 1;
6128 : 4 : pCD[iBase].maxlenNumH = maxlenNumH;
6129 : : /* return values & input/output */
6130 : 4 : pCD[iBase].nLenLinearCT = s[iBase].nLenCT;
6131 : 4 : pCD[iBase].nLenCTAtOnly = s[iBase].nLenCTAtOnly;
6132 : 4 : pCD[iBase].lenNumH = lenNumH /* = k */;
6133 : 4 : pCD[iBase].lenNumHfixed = 0;
6134 : 4 : pCD[iBase].len_iso_sort_key = len_iso_sort_keyBase;
6135 : 4 : pCD[iBase].maxlen_iso_sort_key = maxlen_iso_sort_keyBase;
6136 : 4 : pCD[iBase].len_iso_exchg_atnos = len_iso_exchg_atnos;
6137 : 4 : pCD[iBase].maxlen_iso_exchg_atnos = maxlen_iso_exchg_atnos;
6138 : :
6139 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6140 : : pCD[iBase].len_iso_sort_key_Hfixed = 0;
6141 : : pCD[iBase].maxlen_iso_sort_key_Hfixed = 0;
6142 : : #endif
6143 : :
6144 : 4 : pCD[iBase].nAuxRank = NULL;
6145 : :
6146 [ - + - - : 4 : if (num_iso_NoTautH || num_iso_Base || num_iso_NoAuxBase)
- - ]
6147 : : {
6148 : : /* check whether we need actual canonicalization */
6149 : 4 : memset( nTempRank, 0, num_max * sizeof( nTempRank[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6150 [ + + ]: 8 : for (i = 0; i < num_at_tg; i++)
6151 : : {
6152 [ - + ]: 4 : if (nTempRank[nSymmRankBase[i] - 1] < i)
6153 : : {
6154 : 0 : nTempRank[nSymmRankBase[i] - 1] = i; /* greatest class representative */
6155 : : }
6156 : : }
6157 [ + + ]: 8 : for (i = 0; i < num_at_tg; i++)
6158 : : {
6159 [ + - + - : 4 : if (( iso_sort_keyBase ? ( iso_sort_keyBase[i] != iso_sort_keyBase[nTempRank[nSymmRankBase[i] - 1]] ) : 0 ) ||
- + ]
6160 [ # # ]: 0 : ( iso_exchg_atnos ? ( iso_exchg_atnos[i] != iso_exchg_atnos[nTempRank[nSymmRankBase[i] - 1]] ) : 0 ))
6161 : : {
6162 : 0 : pCD[iBase].nCanonFlags |= CANON_FLAG_ISO_TAUT_DIFF;
6163 : 0 : break; /* atoms so far found to be equivalent have different number of H; the canonicalization is needed */
6164 : : }
6165 : : }
6166 : : }
6167 : : else
6168 : : {
6169 : 0 : i = num_at_tg; /* should not happen */
6170 : : }
6171 : :
6172 : : /* i = 0; *//* debug: force to call the canonicalization */
6173 : :
6174 [ - + ]: 4 : if (i < num_at_tg)
6175 : : {
6176 : : /* we need canonicalization */
6177 : : /* get aux canonical ranking of the structure with isotopic non-tautomeric H */
6178 : :
6179 : : #if ( USE_AUX_RANKING == 1 )
6180 : : /* refine no-taut-H partition according to non-taut H + t-groups isotopic distribution */
6181 : 0 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6182 [ # # ]: 0 : for (i = 0; i < num_at_tg; i++)
6183 : : {
6184 : 0 : pAtomInvariantAux[i].val[0] = nSymmRankBase[i];
6185 [ # # ]: 0 : pAtomInvariantAux[i].iso_sort_key = iso_sort_keyBase ? iso_sort_keyBase[i] : 0; /* additional differentiation */
6186 [ # # ]: 0 : pAtomInvariantAux[i].iso_aux_key = iso_exchg_atnos ? iso_exchg_atnos[i] : 0;
6187 : : }
6188 : : /* initial ranks for non-taut H isotopic distribution */
6189 : 0 : nNumCurrRanks = SetInitialRanks2( num_at_tg, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
6190 : : /* make equitable, not a digraph procedure */
6191 : 0 : nNumCurrRanks = DifferentiateRanks2( pCG, num_at_tg, NeighList[TAUT_YES],
6192 : : nNumCurrRanks, nRankAux,
6193 : : nTempRank, nAtomNumberAux, &lCount, 0 /* 0 means first use qsort */ ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
6194 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
6195 : 0 : pCD[iBase].nAuxRank = nRankAux;
6196 : : #endif
6197 : :
6198 : 0 : ret = CanonGraph06( ic, pCG, num_atoms, num_at_tg, num_max, 1 /* digraph */,
6199 : 0 : NeighList[TAUT_YES], (Partition *) pBCN->pRankStack,
6200 : : nSymmRankBaseIso, nCanonRankBaseIso, nAtomNumberCanonBaseIso,
6201 : 0 : pCD + iBase, pCC, &Ct_Base, &Ct_BaseIso, LargeMolecules );
6202 [ # # ]: 0 : if (ret < 0)
6203 : : {
6204 : 0 : goto exit_error;
6205 : : }
6206 : : /* in case of a tautomeric structure the final results are in:
6207 : :
6208 : : nSymmRankBaseIso
6209 : : nCanonRankBaseIso
6210 : : nAtomNumberCanonBaseIso
6211 : : Ct_BaseIso
6212 : : iso_sort_keyBase (original isotopic atom & t-group positions)
6213 : : Ct_BaseIso->iso_exchg_atnos: 0=>can exchange isotopic H, including tautomeric atoms
6214 : : iso_exchg_atnos : same, in order of t_group_info->nIsotopicEndpointAtomNumber[]
6215 : : */
6216 : : }
6217 : : else
6218 : : {
6219 : : /* copy the results of the previous (no taut H) canonicalization */
6220 : : /* in this case numHNoTautH[] is not needed for the next canonicalization(s) */
6221 [ + - + - ]: 8 : if (( Ct_Temp = (ConTable *) inchi_calloc( 1, sizeof( *Ct_Temp ) ) ) &&
6222 : 4 : CTableCreate( Ct_Temp, num_atoms, pCD + iBase ))
6223 : : {
6224 : 4 : CtFullCopy( Ct_Temp, Ct_Base );
6225 : : /* since Ct_Base does not have Ct_Base->iso_sort_key we
6226 : : have to fill out Ct_Temp->iso_sort_key separately */
6227 [ + - ]: 4 : if (iso_sort_keyBase)
6228 : : {
6229 [ + + ]: 8 : for (i = 0; i < num_at_tg; i++)
6230 : : {
6231 : 4 : Ct_Temp->iso_sort_key[nCanonRankBase[i] - 1] = iso_sort_keyBase[i];
6232 : : }
6233 : 4 : Ct_Temp->len_iso_sort_key = num_at_tg;
6234 : : }
6235 : : else
6236 : : {
6237 : 0 : Ct_Temp->len_iso_sort_key = 0;
6238 : : }
6239 [ - + ]: 4 : if (iso_exchg_atnos)
6240 : : {
6241 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6242 : : {
6243 : 0 : Ct_Temp->iso_exchg_atnos[nCanonRankBase[i] - 1] = iso_exchg_atnos[i];
6244 : : }
6245 : 0 : Ct_Temp->len_iso_exchg_atnos = num_at_tg;
6246 : : }
6247 : : else
6248 : : {
6249 : 4 : Ct_Temp->len_iso_exchg_atnos = 0;
6250 : : }
6251 : : }
6252 : : else
6253 : : {
6254 : 0 : goto exit_error_alloc;
6255 : : }
6256 : 4 : Ct_BaseIso = Ct_Temp;
6257 : 4 : Ct_Temp = NULL;
6258 : 4 : memcpy(nSymmRankBaseIso, nSymmRankBase, num_at_tg * sizeof(nSymmRankBaseIso[0]));
6259 : 4 : memcpy(nCanonRankBaseIso, nCanonRankBase, num_at_tg * sizeof(nCanonRankBaseIso[0]));
6260 : 4 : memcpy(nAtomNumberCanonBaseIso, nAtomNumberCanonBase, num_at_tg * sizeof(nAtomNumberCanonBaseIso[0]));
6261 : : }
6262 : : /* in case of non-tautomeric component this is the final result */
6263 : : /* i = CtFullCompare( Ct_BaseIso, Ct_Temp, num_at_tg, 0, 0 );*/
6264 : :
6265 : 4 : t_group_info->bIgnoreIsotopic = 1;
6266 : : }
6267 : : }
6268 : : }
6269 : :
6270 : : /**********************************************************************************/
6271 : : /* get "fixed H" canonical numbering, connection table, and equivalence partition */
6272 : : /**********************************************************************************/
6273 : :
6274 [ + + - + ]: 1267 : if (bReqTaut && bReqNonTaut)
6275 : : {
6276 : 0 : maxlenNumHfixed = num_atoms + 1;
6277 : 0 : nSymmRankFixH = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankFixH[0] ) );
6278 : 0 : nCanonRankFixH = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankFixH[0] ) );
6279 : 0 : nAtomNumberCanonFixH = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonFixH[0] ) );
6280 : 0 : NumHfixed = (NUM_H *) inchi_calloc( maxlenNumHfixed, sizeof( NumHfixed[0] ) );
6281 [ # # # # : 0 : if (!NumHfixed || !nSymmRankFixH || !nCanonRankFixH || !nAtomNumberCanonFixH)
# # # # ]
6282 : : {
6283 : 0 : goto exit_error_alloc;
6284 : : }
6285 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6286 : : {
6287 : : /* fixed and non-tautomeric H different in taut and non-taut structures */
6288 : :
6289 [ # # ]: 0 : if (at_base[i].endpoint)
6290 : : {
6291 [ # # ]: 0 : NumHfixed[i] = at_other[i].num_H ? at_other[i].num_H + BASE_H_NUMBER : EMPTY_H_NUMBER;
6292 : : }
6293 [ # # ]: 0 : else if (at_other[i].num_H != at_base[i].num_H)
6294 : : {
6295 : 0 : NumHfixed[i] = (NUM_H) at_other[i].num_H - (NUM_H) at_base[i].num_H + BASE_H_NUMBER;
6296 : : }
6297 : : else
6298 : : {
6299 : 0 : NumHfixed[i] = EMPTY_H_NUMBER;
6300 : : }
6301 : : }
6302 : :
6303 : : /* pointers */
6304 : 0 : pCD[iOther].LinearCT = NULL; /* LinearCT; */
6305 : 0 : pCD[iOther].NumH = numHNoTautH;
6306 : 0 : pCD[iOther].NumHfixed = NumHfixed;/* variables - unchanged */
6307 : 0 : pCD[iOther].iso_sort_key = NULL;
6308 : 0 : pCD[iOther].iso_exchg_atnos = NULL;
6309 : :
6310 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6311 : : pCD[iOther].iso_sort_key_Hfixed = NULL;
6312 : : #endif
6313 : :
6314 : 0 : pCD[iOther].ulTimeOutTime = pBCN->ulTimeOutTime;
6315 : 0 : pCD[iOther].nMaxLenLinearCT = s[iOther].nLenCTAtOnly + 1;
6316 : 0 : pCD[iOther].maxlenNumH = maxlenNumHNoTautH;
6317 : 0 : pCD[iOther].maxlenNumHfixed = maxlenNumHfixed;
6318 : : /* return values & input/output */
6319 : 0 : pCD[iOther].nLenLinearCT = s[iOther].nLenCTAtOnly;
6320 : 0 : pCD[iOther].nLenCTAtOnly = s[iOther].nLenCTAtOnly;
6321 : 0 : pCD[iOther].lenNumH = num_atoms; /* djb-rwth: removing redundant code */
6322 : 0 : pCD[iOther].lenNumHfixed = num_atoms;
6323 : 0 : pCD[iOther].len_iso_sort_key = 0;
6324 : 0 : pCD[iOther].maxlen_iso_sort_key = 0;
6325 : 0 : pCD[iOther].len_iso_exchg_atnos = 0;
6326 : 0 : pCD[iOther].maxlen_iso_exchg_atnos = 0;
6327 : :
6328 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6329 : : pCD[iOther].len_iso_sort_key_Hfixed = 0;
6330 : : pCD[iOther].maxlen_iso_sort_key_Hfixed = 0;
6331 : : #endif
6332 : :
6333 : 0 : pCD[iOther].nAuxRank = NULL;
6334 : :
6335 : : #if ( USE_AUX_RANKING == 1 )
6336 [ # # ]: 0 : if (!nRankAux)
6337 : : {
6338 : 0 : nRankAux = (AT_RANK *) inchi_calloc( num_max, sizeof( nRankAux[0] ) );
6339 : : }
6340 [ # # ]: 0 : if (!nAtomNumberAux)
6341 : : {
6342 : 0 : nAtomNumberAux = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberAux[0] ) );
6343 : : }
6344 [ # # ]: 0 : if (!pAtomInvariantAux)
6345 : : {
6346 : 0 : pAtomInvariantAux = (ATOM_INVARIANT2 *) inchi_malloc( num_max * sizeof( pAtomInvariantAux[0] ) );
6347 : : }
6348 [ # # # # : 0 : if (!nRankAux || !nAtomNumberAux || !pAtomInvariantAux)
# # ]
6349 : : {
6350 : 0 : goto exit_error_alloc;
6351 : : }
6352 : :
6353 : : /* refine no-H partition according to non-taut H distribution */
6354 : 0 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6355 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6356 : : {
6357 : 0 : pAtomInvariantAux[i].val[0] = nSymmRankBase[i];
6358 : 0 : pAtomInvariantAux[i].val[1] = NumHfixed[i]; /* additional differentiation */
6359 : : }
6360 : :
6361 : : /* initial ranks for t-group(s) */
6362 : 0 : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
6363 : :
6364 : : /* make equitable, digraph procedure */
6365 : 0 : nNumCurrRanks = DifferentiateRanks2( pCG, num_atoms, NeighList[TAUT_NON],
6366 : : nNumCurrRanks, nRankAux,
6367 : : nTempRank, nAtomNumberAux, &lCount, 0 /* 0 means use qsort */ ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
6368 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
6369 : 0 : pCD[iOther].nAuxRank = nRankAux;
6370 : : #endif
6371 : :
6372 : 0 : ret = CanonGraph07( ic, pCG, num_atoms, num_atoms, num_max, 0,
6373 : 0 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
6374 : : nSymmRankFixH, nCanonRankFixH, nAtomNumberCanonFixH,
6375 : 0 : pCD + iOther, pCC, &Ct_NoTautH, &Ct_FixH, LargeMolecules );
6376 [ # # ]: 0 : if (ret < 0)
6377 : : {
6378 : 0 : goto exit_error;
6379 : : }
6380 : :
6381 : : /*******************************************************************************************/
6382 : : /* get "fixed H" isotopic canonical numbering, connection table, and equivalence partition */
6383 : : /*******************************************************************************************/
6384 [ # # # # ]: 0 : iflag = (s[iBase].num_isotopic_atoms && !s[iBase].bIgnoreIsotopic) ||
6385 [ # # # # ]: 0 : (s[iBase].bHasIsotopicTautGroups && !bTautIgnoreIsotopic); /* djb-rwth: addressing LLVM warning */
6386 [ # # ]: 0 : if (bFixIsoFixedH) /* #if ( FIX_ISO_FIXEDH_BUG == 1 ) */
6387 : : /* fix bug when iso H was removed as a proton and fixed-H isotopic layer is missing - 2008-09-24 DT*/
6388 : : {
6389 [ # # # # : 0 : iflag = iflag || (s[iOther].num_isotopic_atoms && !s[iOther].bIgnoreIsotopic); /* djb-rwth: addressing LLVM warning */
# # ]
6390 : : }
6391 [ # # ]: 0 : if (iflag)
6392 : : {
6393 : :
6394 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6395 : : maxlen_iso_sort_key_Hfixed =
6396 : : #endif
6397 : 0 : maxlen_iso_sort_key_NoTautH = num_atoms + 1;
6398 : 0 : nSymmRankFixHIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nSymmRankFixHIso[0] ) );
6399 : 0 : nCanonRankFixHIso = (AT_RANK *) inchi_calloc( num_max, sizeof( nCanonRankFixHIso[0] ) );
6400 : 0 : nAtomNumberCanonFixHIso = (AT_NUMB *) inchi_calloc( num_max, sizeof( nAtomNumberCanonFixHIso[0] ) );
6401 : :
6402 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6403 : : iso_sort_key_Hfixed = (AT_ISO_SORT_KEY *) inchi_calloc( maxlen_iso_sort_key_Hfixed, sizeof( iso_sort_key_Hfixed[0] ) );
6404 : : #endif
6405 : :
6406 : 0 : iso_sort_key_NoTautH = (AT_ISO_SORT_KEY *) inchi_calloc( maxlen_iso_sort_key_NoTautH, sizeof( iso_sort_key_NoTautH[0] ) );
6407 : :
6408 [ # # # # : 0 : if (!nSymmRankFixHIso || !nCanonRankFixHIso || !nAtomNumberCanonFixHIso ||
# # # # ]
6409 : :
6410 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6411 : : !iso_sort_key_Hfixed ||
6412 : : #endif
6413 : :
6414 : : !iso_sort_key_NoTautH)
6415 : : {
6416 : 0 : goto exit_error_alloc;
6417 : : }
6418 : :
6419 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6420 : : /* fill out isotopic non-tautomeric keys */
6421 : : for (i = 0; i < num_atoms; i++)
6422 : : {
6423 : : if (at_base[i].endpoint)
6424 : : {
6425 : : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, 0, 0, 0 );
6426 : : iso_sort_key2 = make_iso_sort_key( 0, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
6427 : : }
6428 : : else
6429 : : {
6430 : : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
6431 : : iso_sort_key2 = 0;
6432 : : }
6433 : : if (iso_sort_key)
6434 : : {
6435 : : iso_sort_key_NoTautH[i] = iso_sort_key;
6436 : : num_iso_NoTautH++;
6437 : : }
6438 : : else
6439 : : {
6440 : : iso_sort_key_NoTautH[i] = EMPTY_ISO_SORT_KEY;
6441 : : }
6442 : : if (iso_sort_key2)
6443 : : {
6444 : : num_iso_Hfixed++;
6445 : : iso_sort_key_Hfixed[i] = iso_sort_key2;
6446 : : }
6447 : : else
6448 : : {
6449 : : iso_sort_key_Hfixed[i] = EMPTY_ISO_SORT_KEY;
6450 : : }
6451 : : }
6452 : : #else
6453 : : /* fill out isotopic non-tautomeric keys */
6454 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6455 : : {
6456 : :
6457 [ # # ]: 0 : if (bFixIsoFixedH) /* #if ( FIX_ISO_FIXEDH_BUG == 1 ) */
6458 : : {
6459 : : /* fix bug when iso H was removed as a proton and fixed-H isotopic layer is missing - 2008-09-24 DT*/
6460 [ # # ]: 0 : if (at_other)
6461 : : {
6462 : 0 : iso_sort_key = make_iso_sort_key( at_other[i].iso_atw_diff, at_other[i].num_iso_H[0], at_other[i].num_iso_H[1], at_other[i].num_iso_H[2] );
6463 : : }
6464 : : else
6465 : : {
6466 : 0 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
6467 : : }
6468 : : }
6469 : : else
6470 : 0 : iso_sort_key = make_iso_sort_key( at_base[i].iso_atw_diff, at_base[i].num_iso_H[0], at_base[i].num_iso_H[1], at_base[i].num_iso_H[2] );
6471 : :
6472 [ # # ]: 0 : if (iso_sort_key)
6473 : : {
6474 : 0 : iso_sort_key_NoTautH[i] = iso_sort_key;
6475 : 0 : num_iso_NoTautH++;
6476 : : }
6477 : : else
6478 : : {
6479 : 0 : iso_sort_key_NoTautH[i] = EMPTY_ISO_SORT_KEY;
6480 : : }
6481 : : }
6482 : : #endif
6483 : :
6484 : : /* pointers */
6485 : 0 : pCD[iOther].LinearCT = NULL; /* LinearCT; */
6486 : 0 : pCD[iOther].NumH = numHNoTautH;
6487 : 0 : pCD[iOther].NumHfixed = NumHfixed;/* variables - unchanged */
6488 : 0 : pCD[iOther].iso_sort_key = iso_sort_key_NoTautH;
6489 : 0 : pCD[iOther].iso_exchg_atnos = NULL;
6490 : :
6491 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6492 : : pCD[iOther].iso_sort_key_Hfixed = iso_sort_key_Hfixed;
6493 : : #endif
6494 : :
6495 : 0 : pCD[iOther].ulTimeOutTime = pBCN->ulTimeOutTime;
6496 : 0 : pCD[iOther].nMaxLenLinearCT = s[iOther].nLenCTAtOnly + 1;
6497 : 0 : pCD[iOther].maxlenNumH = maxlenNumHNoTautH;
6498 : 0 : pCD[iOther].maxlenNumHfixed = maxlenNumHfixed;
6499 : : /* return values & input/output */
6500 : 0 : pCD[iOther].nLenLinearCT = s[iOther].nLenCTAtOnly;
6501 : 0 : pCD[iOther].nLenCTAtOnly = s[iOther].nLenCTAtOnly;
6502 : 0 : pCD[iOther].lenNumH = num_atoms; /* djb-rwth: removing redundant code */
6503 : 0 : pCD[iOther].lenNumHfixed = num_atoms;
6504 : 0 : pCD[iOther].len_iso_sort_key = len_iso_sort_key_NoTautH = num_atoms;
6505 : 0 : pCD[iOther].maxlen_iso_sort_key = maxlen_iso_sort_key_NoTautH;
6506 : 0 : pCD[iOther].len_iso_exchg_atnos = 0;
6507 : 0 : pCD[iOther].maxlen_iso_exchg_atnos = 0;
6508 : :
6509 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6510 : : pCD[iOther].len_iso_sort_key_Hfixed = len_iso_sort_key_Hfixed = num_atoms;
6511 : : pCD[iOther].maxlen_iso_sort_key_Hfixed = maxlen_iso_sort_key_Hfixed;
6512 : : #endif
6513 : :
6514 : 0 : pCD[iOther].nAuxRank = NULL;
6515 : :
6516 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6517 : : if (num_iso_Hfixed || num_iso_NoTautH)
6518 : : #else
6519 [ # # ]: 0 : if (num_iso_NoTautH)
6520 : : #endif
6521 : :
6522 : : {
6523 : : /* check whether we need NoTautH cononicalization */
6524 : 0 : memset( nTempRank, 0, num_max * sizeof( nTempRank[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6525 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6526 : : {
6527 [ # # ]: 0 : if (nTempRank[nSymmRankFixH[i] - 1] < i)
6528 : : {
6529 : 0 : nTempRank[nSymmRankFixH[i] - 1] = i; /* greatest class representative */
6530 : : }
6531 : : }
6532 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6533 : : {
6534 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6535 : : if (iso_sort_key_Hfixed[i] != iso_sort_key_Hfixed[nTempRank[nSymmRankFixH[i] - 1]])
6536 : : break;
6537 : : #endif
6538 [ # # ]: 0 : if (iso_sort_key_NoTautH[i] != iso_sort_key_NoTautH[nTempRank[nSymmRankFixH[i] - 1]])
6539 : 0 : break; /* atoms so far found to be equivalent have different isotopic shifts; the canonicalization is needed */
6540 : : }
6541 : : }
6542 : : else
6543 : : {
6544 : 0 : i = num_atoms; /* should not happen */
6545 : : }
6546 : :
6547 : : /* i = 0; *//* debug: force to call the canonicalization */
6548 : :
6549 [ # # ]: 0 : if (i < num_atoms)
6550 : : {
6551 : 0 : pCD[iOther].nCanonFlags |= CANON_FLAG_ISO_FIXED_H_DIFF;
6552 : : /* we need canonicalization */
6553 : : /* get aux canonical ranking of the structure with isotopic non-tautomeric H */
6554 : :
6555 : : #if ( USE_AUX_RANKING == 1 )
6556 : : /* refine fixed-taut-H partition according to the isotopic distribution */
6557 : 0 : memset( pAtomInvariantAux, 0, num_max * sizeof( pAtomInvariantAux[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6558 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6559 : : {
6560 : 0 : pAtomInvariantAux[i].val[0] = nSymmRankFixH[i];
6561 : :
6562 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6563 : : iso_sort_key = 0;
6564 : : if (iso_sort_key_NoTautH[i] != EMPTY_ISO_SORT_KEY)
6565 : : iso_sort_key |= iso_sort_key_NoTautH[i];
6566 : : if (iso_sort_key_Hfixed[i] != EMPTY_ISO_SORT_KEY)
6567 : : iso_sort_key |= iso_sort_key_Hfixed[i];
6568 : : if (!iso_sort_key)
6569 : : iso_sort_key = EMPTY_ISO_SORT_KEY;
6570 : : #else
6571 : 0 : iso_sort_key = iso_sort_key_NoTautH[i];
6572 : : #endif
6573 : :
6574 : 0 : pAtomInvariantAux[i].iso_sort_key = iso_sort_key; /* additional differentiation */
6575 : : }
6576 : :
6577 : : /* initial ranks for non-taut H isotopic distribution */
6578 : 0 : nNumCurrRanks = SetInitialRanks2( num_atoms, pAtomInvariantAux, nRankAux, nAtomNumberAux, pCG );
6579 : : /* make equitable, digraph procedure */
6580 : 0 : nNumCurrRanks = DifferentiateRanks2( pCG, num_atoms, NeighList[TAUT_NON],
6581 : : nNumCurrRanks, nRankAux,
6582 : : nTempRank, nAtomNumberAux, &lCount, 0 /* 0 means use qsort */ ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
6583 : :
6584 : : /* to accelerate do not call CanonGraph() to find really equivalent atoms */
6585 : 0 : pCD[iOther].nAuxRank = nRankAux;
6586 : : #endif
6587 : :
6588 : 0 : ret = CanonGraph08( ic, pCG, num_atoms, num_atoms, num_max, 1 /* digraph?? was 0 */,
6589 : 0 : NeighList[TAUT_NON], (Partition *) pBCN->pRankStack,
6590 : : nSymmRankFixHIso, nCanonRankFixHIso, nAtomNumberCanonFixHIso,
6591 : 0 : pCD + iOther, pCC, &Ct_FixH, &Ct_FixHIso, LargeMolecules );
6592 [ # # ]: 0 : if (ret < 0)
6593 : : {
6594 : 0 : goto exit_error;
6595 : : }
6596 : : /* in case of non-tautomeric structure the final results are in:
6597 : :
6598 : : nSymmRankFixHIso
6599 : : nCanonRankFixHIso
6600 : : nAtomNumberCanonFixHIso
6601 : : Ct_FixHIso
6602 : : iso_sort_keyBase ([0..num_atoms] original isotopic atom positions)
6603 : : iso_sort_key_Hfixed (original fixed tautomeric H distribution)
6604 : : */
6605 : : }
6606 : : else
6607 : : {
6608 : : /* copy the results of the previous (no taut H) canonicalization */
6609 : : /* in this case numHNoTautH[] is not needed for the next canonicalization(s) */
6610 [ # # # # ]: 0 : if (( Ct_Temp = (ConTable *) inchi_calloc( 1, sizeof( *Ct_Temp ) ) ) &&
6611 : 0 : CTableCreate( Ct_Temp, num_atoms, pCD + iOther ))
6612 : : {
6613 : 0 : CtFullCopy( Ct_Temp, Ct_FixH );
6614 : : /* since Ct_FixH does not have Ct_FixH->iso_sort_key and Ct_FixH->iso_sort_key_Hfixed we
6615 : : have to fill out Ct_Temp->iso_sort_key and Ct_Temp->iso_sort_key_Hfixed separately */
6616 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
6617 : : {
6618 : 0 : Ct_Temp->iso_sort_key[nCanonRankFixH[i] - 1] = iso_sort_key_NoTautH[i];
6619 : :
6620 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6621 : : Ct_Temp->iso_sort_key_Hfixed[nCanonRankFixH[i] - 1] = iso_sort_key_Hfixed[i];
6622 : : #endif
6623 : : }
6624 : 0 : Ct_Temp->len_iso_sort_key = num_atoms;
6625 : :
6626 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6627 : : Ct_Temp->len_iso_sort_key_Hfixed = num_atoms;
6628 : : #endif
6629 : :
6630 : : /*Ct_Temp->lenNumH = num_atoms;*/
6631 : : }
6632 : : else
6633 : : {
6634 : 0 : goto exit_error_alloc;
6635 : : }
6636 : 0 : Ct_FixHIso = Ct_Temp;
6637 : 0 : Ct_Temp = NULL;
6638 : 0 : memcpy(nSymmRankFixHIso, nSymmRankFixH, num_atoms * sizeof(nSymmRankFixHIso[0]));
6639 : 0 : memcpy(nCanonRankFixHIso, nCanonRankFixH, num_atoms * sizeof(nCanonRankFixHIso[0]));
6640 : 0 : memcpy(nAtomNumberCanonFixHIso, nAtomNumberCanonFixH, num_atoms * sizeof(nAtomNumberCanonFixHIso[0]));
6641 : : }
6642 : : /* in case of non-tautomeric component this is the final result */
6643 : : /* i = CtFullCompare( Ct_NoTautHIso, Ct_Temp, num_atoms, 0, 0 );*/
6644 : : }
6645 : : } /* "fixed H" canonical numbering */
6646 : :
6647 : :
6648 : : /* consistency check: compare canonical connection tables, H-atoms, isotopic H & taut groups */
6649 : 1267 : ret = 0;
6650 [ + - - + ]: 1267 : ret |= ( Ct_NoH->lenCt != Ct_NoTautH->lenCt ) || memcmp( Ct_NoH->Ctbl, Ct_NoTautH->Ctbl, Ct_NoH->lenCt * sizeof( Ct_NoH->Ctbl[0] ) );
6651 [ + + ]: 1267 : if (bReqTaut)
6652 : : {
6653 [ - + ]: 18 : if (Ct_FixH)
6654 : : {
6655 [ # # # # ]: 0 : ret |= ( Ct_NoTautH->lenCt != Ct_FixH->lenCt ) || memcmp( Ct_NoTautH->Ctbl, Ct_FixH->Ctbl, Ct_NoTautH->lenCt * sizeof( Ct_NoTautH->Ctbl[0] ) );
6656 [ # # # # ]: 0 : ret |= ( Ct_NoTautH->lenNumH != Ct_FixH->lenNumH ) || memcmp( Ct_NoTautH->NumH, Ct_FixH->NumH, Ct_NoTautH->lenNumH * sizeof( Ct_Base->NumH[0] ) );
6657 : : }
6658 [ + - - + ]: 18 : ret |= ( Ct_NoTautH->lenCt > Ct_Base->lenCt ) || memcmp( Ct_NoTautH->Ctbl, Ct_Base->Ctbl, Ct_NoTautH->lenCt * sizeof( Ct_NoTautH->Ctbl[0] ) );
6659 [ + - - + ]: 18 : ret |= ( Ct_NoTautH->lenNumH > Ct_Base->lenNumH ) || memcmp( Ct_NoTautH->NumH, Ct_Base->NumH, Ct_NoTautH->lenNumH * sizeof( Ct_Base->NumH[0] ) );
6660 : : }
6661 : :
6662 : : /* isotopic canonicalization */
6663 [ + + ]: 1267 : if (Ct_NoTautHIso)
6664 : : {
6665 [ + - - + ]: 6 : ret |= ( Ct_NoH->lenCt != Ct_NoTautHIso->lenCt ) || memcmp( Ct_NoH->Ctbl, Ct_NoTautHIso->Ctbl, Ct_NoH->lenCt * sizeof( Ct_NoH->Ctbl[0] ) );
6666 [ + - - + ]: 6 : ret |= ( Ct_NoTautH->lenNumH != Ct_NoTautHIso->lenNumH ) || memcmp( Ct_NoTautH->NumH, Ct_NoTautHIso->NumH, Ct_NoTautH->lenNumH * sizeof( Ct_Base->NumH[0] ) );
6667 : : }
6668 [ + + ]: 1261 : else if (Ct_BaseIso)
6669 : : {
6670 [ + - - + ]: 4 : ret |= ( Ct_BaseIso->lenCt != Ct_Base->lenCt ) || memcmp( Ct_BaseIso->Ctbl, Ct_Base->Ctbl, Ct_BaseIso->lenCt * sizeof( Ct_BaseIso->Ctbl[0] ) );
6671 [ + - - + ]: 4 : ret |= ( Ct_BaseIso->lenNumH != Ct_Base->lenNumH ) || memcmp( Ct_BaseIso->NumH, Ct_Base->NumH, Ct_BaseIso->lenNumH * sizeof( Ct_BaseIso->NumH[0] ) );
6672 [ - + ]: 4 : if (Ct_FixHIso)
6673 : : {
6674 [ # # # # ]: 0 : ret |= ( Ct_FixHIso->lenCt > Ct_BaseIso->lenCt ) || memcmp( Ct_FixHIso->Ctbl, Ct_BaseIso->Ctbl, Ct_FixHIso->lenCt * sizeof( Ct_FixHIso->Ctbl[0] ) );
6675 [ # # # # ]: 0 : ret |= ( Ct_FixHIso->lenNumH > Ct_BaseIso->lenNumH ) || memcmp( Ct_FixHIso->NumH, Ct_BaseIso->NumH, Ct_FixHIso->lenNumH * sizeof( Ct_BaseIso->NumH[0] ) );
6676 : : }
6677 : : }
6678 : :
6679 [ - + ]: 1267 : if (ret)
6680 : : {
6681 : 0 : goto exit_error;
6682 : : }
6683 : :
6684 [ + + ]: 1267 : if (bReqTaut)
6685 : : {
6686 : : /* restore save "process isotopic" mark; temporarily set it to NO */
6687 : 18 : t_group_info->bIgnoreIsotopic = bTautIgnoreIsotopic;
6688 : : }
6689 : :
6690 : :
6691 : : /* output the canonicalization results */
6692 : 1267 : pBCN->num_max = num_max;
6693 : 1267 : pBCN->num_at_tg = num_at_tg;
6694 : 1267 : pBCN->num_atoms = num_atoms;
6695 : :
6696 : 1267 : pBCN->ftcn[TAUT_NON].NeighList = NeighList[TAUT_NON]; NeighList[TAUT_NON] = NULL;
6697 : 1267 : pBCN->ftcn[TAUT_YES].NeighList = NeighList[TAUT_YES]; NeighList[TAUT_YES] = NULL;
6698 : :
6699 [ + + ]: 1267 : if (bReqTaut)
6700 : : {
6701 : : /* tautomeric results */
6702 : :
6703 : : /* base tautomeric structure, iBase = TAUT_YES */
6704 : :
6705 : 18 : pBCN->ftcn[TAUT_YES].num_at_tg = num_at_tg;
6706 : 18 : pBCN->ftcn[TAUT_YES].num_atoms = num_atoms;
6707 : :
6708 : 18 : pBCN->ftcn[TAUT_YES].LinearCt = Ct_Base->Ctbl; Ct_Base->Ctbl = NULL;
6709 : 18 : pBCN->ftcn[TAUT_YES].nLenLinearCtAtOnly = s[iBase].nLenCTAtOnly;
6710 : 18 : pBCN->ftcn[TAUT_YES].nMaxLenLinearCt = s[iBase].nLenCT + 1;
6711 : 18 : pBCN->ftcn[TAUT_YES].nLenLinearCt = s[iBase].nLenCT;
6712 : :
6713 : 18 : pBCN->ftcn[TAUT_YES].PartitionCt.Rank = nCanonRankBase; nCanonRankBase = NULL;
6714 : 18 : pBCN->ftcn[TAUT_YES].PartitionCt.AtNumber = nAtomNumberCanonBase; nAtomNumberCanonBase = NULL;
6715 : 18 : pBCN->ftcn[TAUT_YES].nSymmRankCt = nSymmRankBase; nSymmRankBase = NULL;
6716 : :
6717 : 18 : pBCN->ftcn[TAUT_YES].nNumHOrig = numH; numH = NULL;
6718 : 18 : pBCN->ftcn[TAUT_YES].nNumH = Ct_Base->NumH; Ct_Base->NumH = NULL;
6719 : 18 : pBCN->ftcn[TAUT_YES].nLenNumH = inchi_min( maxlenNumH, Ct_Base->maxlenNumH );
6720 : :
6721 : : /* fixed H structure: exists only if the structure is tautomeric */
6722 : 18 : pBCN->ftcn[TAUT_YES].nNumHOrigFixH = NULL;
6723 : 18 : pBCN->ftcn[TAUT_YES].nNumHFixH = NULL;
6724 : 18 : pBCN->ftcn[TAUT_YES].nLenNumHFixH = 0;
6725 : 18 : pBCN->ftcn[TAUT_YES].nCanonFlags |= pCD[iBase].nCanonFlags;
6726 : :
6727 : 18 : CleanNumH( pBCN->ftcn[TAUT_YES].nNumHOrig, pBCN->ftcn[TAUT_YES].nLenNumH );
6728 : 18 : CleanNumH( pBCN->ftcn[TAUT_YES].nNumH, pBCN->ftcn[TAUT_YES].nLenNumH );
6729 : 18 : CleanCt( pBCN->ftcn[TAUT_YES].LinearCt, pBCN->ftcn[TAUT_YES].nLenLinearCt );
6730 : :
6731 : : /* isotopic canonicalization */
6732 [ + + ]: 18 : if (Ct_BaseIso)
6733 : : {
6734 : 4 : pBCN->ftcn[TAUT_YES].PartitionCtIso.Rank = nCanonRankBaseIso; nCanonRankBaseIso = NULL;
6735 : 4 : pBCN->ftcn[TAUT_YES].PartitionCtIso.AtNumber = nAtomNumberCanonBaseIso; nAtomNumberCanonBaseIso = NULL;
6736 : 4 : pBCN->ftcn[TAUT_YES].nSymmRankCtIso = nSymmRankBaseIso; nSymmRankBaseIso = NULL;
6737 : 4 : pBCN->ftcn[TAUT_YES].iso_sort_keys = Ct_BaseIso->iso_sort_key; Ct_BaseIso->iso_sort_key = NULL;
6738 : 4 : pBCN->ftcn[TAUT_YES].iso_sort_keysOrig = iso_sort_keyBase; iso_sort_keyBase = NULL;
6739 : 4 : pBCN->ftcn[TAUT_YES].len_iso_sort_keys = len_iso_sort_keyBase;
6740 : 4 : pBCN->ftcn[TAUT_YES].iso_exchg_atnos = Ct_BaseIso->iso_exchg_atnos; Ct_BaseIso->iso_exchg_atnos = NULL;
6741 : 4 : pBCN->ftcn[TAUT_YES].iso_exchg_atnosOrig = iso_exchg_atnos; iso_exchg_atnos = NULL;
6742 : :
6743 : 4 : CleanIsoSortKeys( pBCN->ftcn[TAUT_YES].iso_sort_keys, pBCN->ftcn[TAUT_YES].len_iso_sort_keys );
6744 : 4 : CleanIsoSortKeys( pBCN->ftcn[TAUT_YES].iso_sort_keysOrig, pBCN->ftcn[TAUT_YES].len_iso_sort_keys );
6745 : : }
6746 : : } /* tautomeric results */
6747 : :
6748 : :
6749 [ + + ]: 1267 : if (bReqNonTaut)
6750 : : {
6751 : : /* non-tautomeric results */
6752 : :
6753 : : /* TAUT_NON if tautomeric + non-tautomeric or special non-taut request
6754 : : TAUT_YES if the structure happened to be non-tautomeric while user requested tautomeric processing
6755 : : In both cases the correct index is iOther. TAUT_NON replaced with iOther 4-2-2004 */
6756 : :
6757 [ + - ]: 1249 : if (!bReqTaut)
6758 : : {
6759 : : /* rearrange the results for a non-tautomeric structure */
6760 : 1249 : nSymmRankFixH = nSymmRankNoTautH; nSymmRankNoTautH = NULL;
6761 : 1249 : nCanonRankFixH = nCanonRankNoTautH; nCanonRankNoTautH = NULL;
6762 : 1249 : nAtomNumberCanonFixH = nAtomNumberCanonNoTautH; nAtomNumberCanonNoTautH = NULL;
6763 : 1249 : Ct_FixH = Ct_NoTautH; Ct_NoTautH = NULL;
6764 : : /* isotopic canonicalization */
6765 : 1249 : nSymmRankFixHIso = nSymmRankNoTautHIso; nSymmRankNoTautHIso = NULL;
6766 : 1249 : nCanonRankFixHIso = nCanonRankNoTautHIso; nCanonRankNoTautHIso = NULL;
6767 : 1249 : nAtomNumberCanonFixHIso = nAtomNumberCanonNoTautHIso; nAtomNumberCanonNoTautHIso = NULL;
6768 : 1249 : Ct_FixHIso = Ct_NoTautHIso; Ct_NoTautHIso = NULL;
6769 : :
6770 [ + - + - : 1249 : if (iOther == TAUT_YES && pBCN->ftcn[TAUT_NON].NeighList && !pBCN->ftcn[TAUT_YES].NeighList)
+ - ]
6771 : : {
6772 : : /* here only non-taut results go to pBCN->ftcn[TAUT_YES]
6773 : : Since non-taut NeighList is always in pBCN->ftcn[TAUT_NON].NeighList, move it to
6774 : : pBCN->ftcn[TAUT_YES].NeighList. 2004-04-02.
6775 : : */
6776 : 1249 : pBCN->ftcn[TAUT_YES].NeighList = pBCN->ftcn[TAUT_NON].NeighList;
6777 : 1249 : pBCN->ftcn[TAUT_NON].NeighList = NULL;
6778 : : }
6779 : : }
6780 : :
6781 : 1249 : pBCN->ftcn[iOther].num_at_tg = num_atoms;
6782 : 1249 : pBCN->ftcn[iOther].num_atoms = num_atoms;
6783 : :
6784 : 1249 : pBCN->ftcn[iOther].LinearCt = Ct_FixH->Ctbl;
6785 : 1249 : Ct_FixH->Ctbl = NULL;
6786 : 1249 : pBCN->ftcn[iOther].nLenLinearCtAtOnly = s[iOther].nLenCTAtOnly;
6787 : 1249 : pBCN->ftcn[iOther].nMaxLenLinearCt = s[iOther].nLenCTAtOnly + 1;
6788 : 1249 : pBCN->ftcn[iOther].nLenLinearCt = s[iOther].nLenCTAtOnly;
6789 : :
6790 : 1249 : pBCN->ftcn[iOther].PartitionCt.Rank = nCanonRankFixH;
6791 : 1249 : nCanonRankFixH = NULL;
6792 : 1249 : pBCN->ftcn[iOther].PartitionCt.AtNumber = nAtomNumberCanonFixH;
6793 : 1249 : nAtomNumberCanonFixH = NULL;
6794 : 1249 : pBCN->ftcn[iOther].nSymmRankCt = nSymmRankFixH;
6795 : 1249 : nSymmRankFixH = NULL;
6796 : :
6797 : 1249 : pBCN->ftcn[iOther].nNumHOrig = numHNoTautH;
6798 : 1249 : numHNoTautH = NULL;
6799 : 1249 : pBCN->ftcn[iOther].nNumH = Ct_FixH->NumH;
6800 : 1249 : Ct_FixH->NumH = NULL;
6801 : 1249 : pBCN->ftcn[iOther].nLenNumH = inchi_min( maxlenNumHNoTautH, Ct_FixH->maxlenNumH );
6802 : :
6803 : : /* fixed H structure: exists only if the structure is tautomeric */
6804 : 1249 : pBCN->ftcn[iOther].nNumHOrigFixH = NumHfixed;
6805 : 1249 : NumHfixed = NULL;
6806 : 1249 : pBCN->ftcn[iOther].nNumHFixH = Ct_FixH->NumHfixed;
6807 : 1249 : Ct_FixH->NumHfixed = NULL;
6808 : 1249 : pBCN->ftcn[iOther].nLenNumHFixH = num_atoms;
6809 : 1249 : pBCN->ftcn[iOther].nCanonFlags |= pCD[iOther].nCanonFlags;
6810 : :
6811 : : /* original H */
6812 : 1249 : CleanNumH( pBCN->ftcn[iOther].nNumHOrig, pBCN->ftcn[iOther].nLenNumH );
6813 : 1249 : CleanNumH( pBCN->ftcn[iOther].nNumHOrigFixH, pBCN->ftcn[iOther].nLenNumH );
6814 : : /* canonical H positions */
6815 : 1249 : CleanNumH( pBCN->ftcn[iOther].nNumH, pBCN->ftcn[iOther].nLenNumH );
6816 : 1249 : CleanNumH( pBCN->ftcn[iOther].nNumHFixH, pBCN->ftcn[iOther].nLenNumH );
6817 : :
6818 : : /* connection table */
6819 : 1249 : CleanCt( pBCN->ftcn[iOther].LinearCt, pBCN->ftcn[iOther].nLenLinearCt );
6820 : :
6821 : : /* isotopic canonicalization */
6822 [ + + ]: 1249 : if (Ct_FixHIso)
6823 : : {
6824 : 6 : pBCN->ftcn[iOther].PartitionCtIso.Rank = nCanonRankFixHIso; nCanonRankFixHIso = NULL;
6825 : 6 : pBCN->ftcn[iOther].PartitionCtIso.AtNumber = nAtomNumberCanonFixHIso; nAtomNumberCanonFixHIso = NULL;
6826 : 6 : pBCN->ftcn[iOther].nSymmRankCtIso = nSymmRankFixHIso; nSymmRankFixHIso = NULL;
6827 : 6 : pBCN->ftcn[iOther].iso_sort_keys = Ct_FixHIso->iso_sort_key; Ct_FixHIso->iso_sort_key = NULL;
6828 : 6 : pBCN->ftcn[iOther].iso_sort_keysOrig = iso_sort_key_NoTautH; iso_sort_key_NoTautH = NULL;
6829 : 6 : pBCN->ftcn[iOther].len_iso_sort_keys = len_iso_sort_key_NoTautH;
6830 : :
6831 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6832 : : MergeCleanIsoSortKeys( pBCN->ftcn[iOther].iso_sort_keys, Ct_FixHIso->iso_sort_key_Hfixed, pBCN->ftcn[iOther].len_iso_sort_keys );
6833 : : MergeCleanIsoSortKeys( pBCN->ftcn[iOther].iso_sort_keysOrig, iso_sort_key_Hfixed, pBCN->ftcn[iOther].len_iso_sort_keys );
6834 : : #else
6835 : 6 : CleanIsoSortKeys( pBCN->ftcn[iOther].iso_sort_keys, pBCN->ftcn[iOther].len_iso_sort_keys );
6836 : 6 : CleanIsoSortKeys( pBCN->ftcn[iOther].iso_sort_keysOrig, pBCN->ftcn[iOther].len_iso_sort_keys );
6837 : : #endif
6838 : : }
6839 : : } /* non-tautomeric results */
6840 : :
6841 : 1267 : goto exit_function;
6842 : :
6843 : :
6844 : 0 : exit_error_alloc:
6845 : 0 : ret = CT_OUT_OF_RAM;
6846 : 0 : goto exit_function;
6847 : :
6848 : 0 : exit_error:
6849 [ # # # # ]: 0 : if (!RETURNED_ERROR( ret ))
6850 : : {
6851 : 0 : ret = CT_CANON_ERR;
6852 : : }
6853 : 0 : goto exit_function;
6854 : :
6855 : :
6856 : 1267 : exit_function:
6857 : :
6858 : : #define FREE_CONTABLE( X) if (X) {CTableFree( X);inchi_free( X);}
6859 : : #define FREE_ARRAY( X) if (X) inchi_free( X);
6860 : :
6861 : 1267 : FreeNeighList( NeighList[TAUT_NON] );
6862 : 1267 : FreeNeighList( NeighList[TAUT_YES] );
6863 : :
6864 [ + - + - ]: 1267 : FREE_CONTABLE( Ct_NoH )
6865 [ + + + - ]: 1267 : FREE_CONTABLE( Ct_NoTautH )
6866 [ + + + - ]: 1267 : FREE_CONTABLE( Ct_Base )
6867 [ + + + - ]: 1267 : FREE_CONTABLE( Ct_FixH )
6868 [ - + - - ]: 1267 : FREE_CONTABLE( Ct_Temp )
6869 : : /* isotopic canonicalization */
6870 [ - + - - ]: 1267 : FREE_CONTABLE( Ct_NoTautHIso )
6871 [ + + + - ]: 1267 : FREE_CONTABLE( Ct_BaseIso )
6872 [ + + + - ]: 1267 : FREE_CONTABLE( Ct_FixHIso )
6873 : :
6874 : : /* free the first two pointers from pBCN->pRankStack */
6875 [ + - + - ]: 1267 : FREE_ARRAY( nRank )
6876 [ + - + - ]: 1267 : FREE_ARRAY( nAtomNumber )
6877 : :
6878 [ + - ]: 1267 : if (pBCN->pRankStack)
6879 : : {
6880 : 1267 : pBCN->pRankStack[0] =
6881 : 1267 : pBCN->pRankStack[1] = NULL;
6882 : : }
6883 : :
6884 : : #if ( USE_AUX_RANKING == 1 )
6885 [ + - + - ]: 1267 : FREE_ARRAY( nRankAux )
6886 [ + - + - ]: 1267 : FREE_ARRAY( nAtomNumberAux )
6887 [ + - + - ]: 1267 : FREE_ARRAY( pAtomInvariantAux )
6888 : : #endif
6889 : :
6890 [ + - + - ]: 1267 : FREE_ARRAY( pAtomInvariant )
6891 : :
6892 [ + - + - ]: 1267 : FREE_ARRAY( nCanonRankNoH )
6893 [ + - + - ]: 1267 : FREE_ARRAY( nAtomNumberCanonNoH )
6894 [ + - + - ]: 1267 : FREE_ARRAY( nSymmRankNoH )
6895 : :
6896 [ + + + - ]: 1267 : FREE_ARRAY( nSymmRankNoTautH )
6897 [ + + + - ]: 1267 : FREE_ARRAY( nCanonRankNoTautH )
6898 [ + + + - ]: 1267 : FREE_ARRAY( nAtomNumberCanonNoTautH )
6899 [ + + + - ]: 1267 : FREE_ARRAY( numHNoTautH )
6900 : :
6901 [ - + - - ]: 1267 : FREE_ARRAY( nSymmRankBase )
6902 [ - + - - ]: 1267 : FREE_ARRAY( nCanonRankBase )
6903 [ - + - - ]: 1267 : FREE_ARRAY( nAtomNumberCanonBase )
6904 [ - + - - ]: 1267 : FREE_ARRAY( numH )
6905 : :
6906 [ - + - - ]: 1267 : FREE_ARRAY( nSymmRankFixH )
6907 [ - + - - ]: 1267 : FREE_ARRAY( nCanonRankFixH )
6908 [ - + - - ]: 1267 : FREE_ARRAY( nAtomNumberCanonFixH )
6909 [ - + - - ]: 1267 : FREE_ARRAY( NumHfixed )
6910 : :
6911 : : /* isotopic canonicalization */
6912 : :
6913 [ - + - - ]: 1267 : FREE_ARRAY( nSymmRankNoTautHIso )
6914 [ - + - - ]: 1267 : FREE_ARRAY( nCanonRankNoTautHIso )
6915 [ - + - - ]: 1267 : FREE_ARRAY( nAtomNumberCanonNoTautHIso )
6916 [ - + - - ]: 1267 : FREE_ARRAY( iso_sort_key_NoTautH )
6917 : :
6918 [ - + - - ]: 1267 : FREE_ARRAY( nSymmRankBaseIso )
6919 [ - + - - ]: 1267 : FREE_ARRAY( nCanonRankBaseIso )
6920 [ - + - - ]: 1267 : FREE_ARRAY( nAtomNumberCanonBaseIso )
6921 [ - + - - ]: 1267 : FREE_ARRAY( iso_sort_keyBase )
6922 [ - + - - ]: 1267 : FREE_ARRAY( iso_exchg_atnos )
6923 : :
6924 [ - + - - ]: 1267 : FREE_ARRAY( nSymmRankFixHIso )
6925 [ - + - - ]: 1267 : FREE_ARRAY( nCanonRankFixHIso )
6926 [ - + - - ]: 1267 : FREE_ARRAY( nAtomNumberCanonFixHIso )
6927 : :
6928 : : #if ( USE_ISO_SORT_KEY_HFIXED == 1 )
6929 : : FREE_ARRAY( iso_sort_key_Hfixed )
6930 : : #endif
6931 : :
6932 [ + - + - ]: 1267 : FREE_ARRAY( nTempRank )
6933 : :
6934 : : #undef FREE_CONTABLE
6935 : : #undef FREE_ARRAY
6936 : :
6937 : 1267 : return ret;
6938 : : }
|