1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (C) 2015-2026 Mateusz Łącki and Michał Startek.
*
* This file is part of IsoSpec.
*
* IsoSpec is free software: you can redistribute it and/or modify
* it under the terms of the Simplified ("2-clause") BSD licence.
*
* IsoSpec is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Simplified BSD Licence
* along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
*/
#include "element_lookup.h"
#include <cstring>
#include "element_tables.h"
namespace IsoSpec {
namespace {
//! Direct-indexed [first letter A-Z][second letter a-z, or index 26 for "no
//! second letter"] lookup table, built once from element_tables.cpp's 292
//! rows (verified elsewhere: 87 unique symbols, zero collisions). No map, no
//! per-lookup scan.
struct SymbolIndexTable {
int table[26][27];
SymbolIndexTable() {
for (int i = 0; i < 26; i++)
for (int j = 0; j < 27; j++)
table[i][j] = -1;
for (int row = 0; row < ISOSPEC_NUMBER_OF_ISOTOPIC_ENTRIES; row++) {
const char* symbol = elem_table_symbol[row];
size_t len = strlen(symbol);
if (len < 1 || len > 2 || symbol[0] < 'A' || symbol[0] > 'Z')
continue; // not a plain [A-Z][a-z]? symbol -- none in this table today
int first = symbol[0] - 'A';
int second;
if (len == 1) {
second = 26;
} else {
if (symbol[1] < 'a' || symbol[1] > 'z')
continue;
second = symbol[1] - 'a';
}
// Only the first (lowest) row of each element's contiguous
// isotope run is a "first index" -- later rows of the same
// element would just overwrite with the same value if we let
// them, so no need to special-case that.
if (table[first][second] == -1)
table[first][second] = row;
}
}
};
const SymbolIndexTable& symbol_index_table() {
static const SymbolIndexTable instance;
return instance;
}
} // namespace
int find_element_table_first_index(const char* symbol, size_t len) {
if (len < 1 || len > 2)
return -1;
if (symbol[0] < 'A' || symbol[0] > 'Z')
return -1;
int first = symbol[0] - 'A';
int second;
if (len == 1) {
second = 26;
} else {
if (symbol[1] < 'a' || symbol[1] > 'z')
return -1;
second = symbol[1] - 'a';
}
return symbol_index_table().table[first][second];
}
int element_isotope_count(int first_index) {
if (first_index < 0)
return 0;
int elem_ID = elem_table_ID[first_index];
int count = 0;
int idx = first_index;
while (idx < ISOSPEC_NUMBER_OF_ISOTOPIC_ENTRIES && elem_table_ID[idx] == elem_ID) {
idx++;
count++;
}
return count;
}
} // namespace IsoSpec