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-14 15:36:24 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       43240 : 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       43240 :                  size_t id_in_database)
      17       43240 :     : Ket(energy), species(std::move(species)), quantum_numbers(std::move(quantum_numbers)),
      18       43240 :       quantum_numbers_std(std::move(quantum_numbers_std)), database(database),
      19       43240 :       id_in_database(id_in_database) {}
      20             : 
      21         518 : Database &KetAtom::get_database() const { return database; }
      22             : 
      23       47272 : size_t KetAtom::get_id_in_database() const { return id_in_database; }
      24             : 
      25      184843 : double KetAtom::get_quantum_number(const std::string &name) const {
      26      184843 :     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       18741 : const std::string &KetAtom::get_species() const { return species; }
      35             : 
      36       40389 : bool KetAtom::operator==(const KetAtom &other) const {
      37       43463 :     return Ket::operator==(other) && species == other.species &&
      38       45731 :         quantum_numbers == other.quantum_numbers &&
      39       42657 :         quantum_numbers_std == other.quantum_numbers_std;
      40             : }
      41             : 
      42           1 : bool KetAtom::operator!=(const KetAtom &other) const { return !(*this == other); }
      43             : 
      44        3996 : size_t KetAtom::hash::operator()(const KetAtom &k) const {
      45        3996 :     size_t seed = typename Ket::hash()(k);
      46        3996 :     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        3996 :     size_t quantum_numbers_hash = 0;
      50       59940 :     for (const auto &[key, value] : k.quantum_numbers) {
      51       55944 :         size_t entry_seed = 0;
      52       55944 :         utils::hash_combine(entry_seed, key);
      53       55944 :         utils::hash_combine(entry_seed, value);
      54       55944 :         quantum_numbers_hash ^= entry_seed;
      55             :     }
      56       27972 :     for (const auto &[key, value] : k.quantum_numbers_std) {
      57       23976 :         size_t entry_seed = 0;
      58       23976 :         utils::hash_combine(entry_seed, key);
      59       23976 :         utils::hash_combine(entry_seed, value);
      60       23976 :         quantum_numbers_hash ^= entry_seed;
      61             :     }
      62        3996 :     utils::hash_combine(seed, quantum_numbers_hash);
      63        3996 :     return seed;
      64             : }
      65             : 
      66             : } // namespace pairinteraction

Generated by: LCOV version 1.16