LCOV - code coverage report
Current view: top level - src/ket - KetAtom.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 34 100.0 %
Date: 2026-08-17 11:38:34 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2024 PairInteraction Developers
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #include "pairinteraction/ket/KetAtom.hpp"
       5             : 
       6             : #include "pairinteraction/utils/hash.hpp"
       7             : 
       8             : #include <string>
       9             : #include <vector>
      10             : 
      11             : namespace pairinteraction {
      12             : 
      13       43283 : KetAtom::KetAtom(Private /*unused*/, double energy, std::string species,
      14             :                  std::unordered_map<std::string, double> quantum_numbers,
      15             :                  std::unordered_map<std::string, double> quantum_numbers_std, Database &database,
      16       43283 :                  size_t id_in_database)
      17       43283 :     : Ket(energy), species(std::move(species)), quantum_numbers(std::move(quantum_numbers)),
      18       43283 :       quantum_numbers_std(std::move(quantum_numbers_std)), database(database),
      19       43283 :       id_in_database(id_in_database) {}
      20             : 
      21         520 : Database &KetAtom::get_database() const { return database; }
      22             : 
      23       47369 : size_t KetAtom::get_id_in_database() const { return id_in_database; }
      24             : 
      25      180277 : double KetAtom::get_quantum_number(const std::string &name) const {
      26      180277 :     return quantum_numbers.at(name);
      27             : }
      28             : 
      29           5 : double KetAtom::get_quantum_number_std(const std::string &name) const {
      30           5 :     auto it = quantum_numbers_std.find(name);
      31           5 :     return it != quantum_numbers_std.end() ? it->second : 0;
      32             : }
      33             : 
      34       17362 : const std::string &KetAtom::get_species() const { return species; }
      35             : 
      36       40471 : bool KetAtom::operator==(const KetAtom &other) const {
      37       43629 :     return Ket::operator==(other) && species == other.species &&
      38       45981 :         quantum_numbers == other.quantum_numbers &&
      39       42823 :         quantum_numbers_std == other.quantum_numbers_std;
      40             : }
      41             : 
      42           1 : bool KetAtom::operator!=(const KetAtom &other) const { return !(*this == other); }
      43             : 
      44        4164 : size_t KetAtom::hash::operator()(const KetAtom &k) const {
      45        4164 :     size_t seed = typename Ket::hash()(k);
      46        4164 :     utils::hash_combine(seed, k.species);
      47             :     // The quantum numbers are stored in an unordered map, so we combine the per-entry hashes in an
      48             :     // order-independent way (via xor) to obtain a deterministic result.
      49        4164 :     size_t quantum_numbers_hash = 0;
      50       62460 :     for (const auto &[key, value] : k.quantum_numbers) {
      51       58296 :         size_t entry_seed = 0;
      52       58296 :         utils::hash_combine(entry_seed, key);
      53       58296 :         utils::hash_combine(entry_seed, value);
      54       58296 :         quantum_numbers_hash ^= entry_seed;
      55             :     }
      56       29148 :     for (const auto &[key, value] : k.quantum_numbers_std) {
      57       24984 :         size_t entry_seed = 0;
      58       24984 :         utils::hash_combine(entry_seed, key);
      59       24984 :         utils::hash_combine(entry_seed, value);
      60       24984 :         quantum_numbers_hash ^= entry_seed;
      61             :     }
      62        4164 :     utils::hash_combine(seed, quantum_numbers_hash);
      63        4164 :     return seed;
      64             : }
      65             : 
      66             : } // namespace pairinteraction

Generated by: LCOV version 1.16