Commit 0eff4ea0b6bbdea9fef045edbe10b496c117aede
1 parent
fabe14af
Rename GResponseCache::energy() method to GResponseCache::hash()
Showing
8 changed files
with
12 additions
and
730 deletions
include/GResponseCache.hpp
| ... | ... | @@ -98,7 +98,7 @@ protected: |
| 98 | 98 | void init_members(void); |
| 99 | 99 | void copy_members(const GResponseCache& cache); |
| 100 | 100 | void free_members(void); |
| 101 | - uint64_t energy(const GEnergy& ereco, const GEnergy& etrue) const; | |
| 101 | + uint64_t hash(const GEnergy& ereco, const GEnergy& etrue) const; | |
| 102 | 102 | |
| 103 | 103 | // Protected members |
| 104 | 104 | GResponseCacheName m_cache; //<! Response cache | ... | ... |
include/GResponseCacheKey.hpp deleted
| 1 | -/*************************************************************************** | |
| 2 | - * GResponseCacheKey.hpp - Response cache key class * | |
| 3 | - * ----------------------------------------------------------------------- * | |
| 4 | - * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | - * ----------------------------------------------------------------------- * | |
| 6 | - * * | |
| 7 | - * This program is free software: you can redistribute it and/or modify * | |
| 8 | - * it under the terms of the GNU General Public License as published by * | |
| 9 | - * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | - * (at your option) any later version. * | |
| 11 | - * * | |
| 12 | - * This program is distributed in the hope that it will be useful, * | |
| 13 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | - * GNU General Public License for more details. * | |
| 16 | - * * | |
| 17 | - * You should have received a copy of the GNU General Public License * | |
| 18 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | - * * | |
| 20 | - ***************************************************************************/ | |
| 21 | -/** | |
| 22 | - * @file GResponseCacheKey.hpp | |
| 23 | - * @brief Response cache key class definition | |
| 24 | - * @author Juergen Knoedlseder | |
| 25 | - */ | |
| 26 | - | |
| 27 | -#ifndef GRESPONSECACHEKEY_HPP | |
| 28 | -#define GRESPONSECACHEKEY_HPP | |
| 29 | - | |
| 30 | -/* __ Includes ___________________________________________________________ */ | |
| 31 | -#include <string> | |
| 32 | -#include <vector> | |
| 33 | -#include "GContainer.hpp" | |
| 34 | - | |
| 35 | -/* __ Forward declarations _______________________________________________ */ | |
| 36 | - | |
| 37 | -/* __ Constants __________________________________________________________ */ | |
| 38 | - | |
| 39 | -/* __ Typedefs ___________________________________________________________ */ | |
| 40 | - | |
| 41 | - | |
| 42 | -/***********************************************************************//** | |
| 43 | - * @class GResponseCacheKey | |
| 44 | - * | |
| 45 | - * @brief Response cache key class | |
| 46 | - * | |
| 47 | - * The class implements the key for the Instrument Response Function cache. | |
| 48 | - * The key aggregates all relevant attributes to describe a cached value, | |
| 49 | - * such as model name, true and measured event energies and instrument | |
| 50 | - * direction. | |
| 51 | - ***************************************************************************/ | |
| 52 | -class GResponseCacheKey : public GContainer { | |
| 53 | - | |
| 54 | -public: | |
| 55 | - // Constructors and destructors | |
| 56 | - GResponseCacheKey(void); | |
| 57 | - explicit GResponseCacheKey(const std::string& name); | |
| 58 | - GResponseCacheKey(const GResponseCacheKey& key); | |
| 59 | - virtual ~GResponseCacheKey(void); | |
| 60 | - | |
| 61 | - // Operators | |
| 62 | - GResponseCacheKey& operator=(const GResponseCacheKey& key); | |
| 63 | - float& operator[](const int& index); | |
| 64 | - const float& operator[](const int& index) const; | |
| 65 | - bool operator<(const GResponseCacheKey& key) const; | |
| 66 | - | |
| 67 | - // Methods | |
| 68 | - void clear(void); | |
| 69 | - GResponseCacheKey* clone(void) const; | |
| 70 | - std::string classname(void) const; | |
| 71 | - bool is_empty(void) const; | |
| 72 | - int size(void) const; | |
| 73 | - void set(const int& index, const float& value); | |
| 74 | - void append(const float& value); | |
| 75 | - void insert(const int& index, const float& value); | |
| 76 | - void remove(const int& index); | |
| 77 | - void reserve(const int& num); | |
| 78 | - void name(const std::string& name); | |
| 79 | - const std::string& name(void) const; | |
| 80 | - std::string print(const GChatter& chatter = NORMAL) const; | |
| 81 | - | |
| 82 | -protected: | |
| 83 | - // Protected methods | |
| 84 | - void init_members(void); | |
| 85 | - void copy_members(const GResponseCacheKey& key); | |
| 86 | - void free_members(void); | |
| 87 | - | |
| 88 | - // Protected members | |
| 89 | - std::string m_name; //!< Key name (e.g. model name, observation) | |
| 90 | - std::vector<float> m_values; //!< Key values | |
| 91 | -}; | |
| 92 | - | |
| 93 | - | |
| 94 | -/***********************************************************************//** | |
| 95 | - * @brief Return reference to key value | |
| 96 | - * | |
| 97 | - * @param[in] index Key index. | |
| 98 | - * | |
| 99 | - * Returns a reference to a key value. | |
| 100 | - ***************************************************************************/ | |
| 101 | -float& GResponseCacheKey::operator[](const int& index) | |
| 102 | -{ | |
| 103 | - return m_values[index]; | |
| 104 | -} | |
| 105 | - | |
| 106 | - | |
| 107 | -/***********************************************************************//** | |
| 108 | - * @brief Return reference to key value (const version) | |
| 109 | - * | |
| 110 | - * @param[in] index Key index. | |
| 111 | - * | |
| 112 | - * Returns a reference to a key value. | |
| 113 | - ***************************************************************************/ | |
| 114 | -const float& GResponseCacheKey::operator[](const int& index) const | |
| 115 | -{ | |
| 116 | - return m_values[index]; | |
| 117 | -} | |
| 118 | - | |
| 119 | - | |
| 120 | -/***********************************************************************//** | |
| 121 | - * @brief Return class name | |
| 122 | - * | |
| 123 | - * @return String containing the class name ("GResponseCacheKey"). | |
| 124 | - ***************************************************************************/ | |
| 125 | -inline | |
| 126 | -std::string GResponseCacheKey::classname(void) const | |
| 127 | -{ | |
| 128 | - return ("GResponseCacheKey"); | |
| 129 | -} | |
| 130 | - | |
| 131 | - | |
| 132 | -/***********************************************************************//** | |
| 133 | - * @brief Checks whether the key has no values | |
| 134 | - * | |
| 135 | - * @return True if key has no values, false otherwise. | |
| 136 | - * | |
| 137 | - * Checks whether the key has no values. | |
| 138 | - ***************************************************************************/ | |
| 139 | -inline | |
| 140 | -bool GResponseCacheKey::is_empty(void) const | |
| 141 | -{ | |
| 142 | - return (m_values.empty()); | |
| 143 | -} | |
| 144 | - | |
| 145 | - | |
| 146 | -/***********************************************************************//** | |
| 147 | - * @brief Return number of key values | |
| 148 | - * | |
| 149 | - * @return Number of key values. | |
| 150 | - * | |
| 151 | - * Returns the number of key values. | |
| 152 | - ***************************************************************************/ | |
| 153 | -inline | |
| 154 | -int GResponseCacheKey::size(void) const | |
| 155 | -{ | |
| 156 | - return (m_values.size()); | |
| 157 | -} | |
| 158 | - | |
| 159 | - | |
| 160 | -/***********************************************************************//** | |
| 161 | - * @brief Append key value | |
| 162 | - * | |
| 163 | - * @param[in] value Key value. | |
| 164 | - * | |
| 165 | - * Append @p value to key values. | |
| 166 | - ***************************************************************************/ | |
| 167 | -inline | |
| 168 | -void GResponseCacheKey::append(const float& value) | |
| 169 | -{ | |
| 170 | - m_values.push_back(value); | |
| 171 | - return; | |
| 172 | -} | |
| 173 | - | |
| 174 | - | |
| 175 | -/***********************************************************************//** | |
| 176 | - * @brief Reserves space for key values | |
| 177 | - * | |
| 178 | - * @param[in] num Number of key values | |
| 179 | - * | |
| 180 | - * Reserves space for @p num key values. | |
| 181 | - ***************************************************************************/ | |
| 182 | -inline | |
| 183 | -void GResponseCacheKey::reserve(const int& num) | |
| 184 | -{ | |
| 185 | - m_values.reserve(num); | |
| 186 | - return; | |
| 187 | -} | |
| 188 | - | |
| 189 | - | |
| 190 | -/***********************************************************************//** | |
| 191 | - * @brief Set key name | |
| 192 | - * | |
| 193 | - * @param[in] name Key name | |
| 194 | - * | |
| 195 | - * Sets the key name. | |
| 196 | - ***************************************************************************/ | |
| 197 | -inline | |
| 198 | -void GResponseCacheKey::name(const std::string& name) | |
| 199 | -{ | |
| 200 | - m_name = name; | |
| 201 | - return; | |
| 202 | -} | |
| 203 | - | |
| 204 | - | |
| 205 | -/***********************************************************************//** | |
| 206 | - * @brief Return key name | |
| 207 | - * | |
| 208 | - * @returns Key name | |
| 209 | - * | |
| 210 | - * Returns the key name. | |
| 211 | - ***************************************************************************/ | |
| 212 | -inline | |
| 213 | -const std::string& GResponseCacheKey::name(void) const | |
| 214 | -{ | |
| 215 | - return m_name; | |
| 216 | -} | |
| 217 | - | |
| 218 | -#endif /* GRESPONSECACHEKEY_HPP */ |
include/GammaLib.hpp
include/Makefile.am
pyext/GResponseCacheKey.i deleted
| 1 | -/*************************************************************************** | |
| 2 | - * GResponseCacheKey.i - Response cache key class * | |
| 3 | - * ----------------------------------------------------------------------- * | |
| 4 | - * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | - * ----------------------------------------------------------------------- * | |
| 6 | - * * | |
| 7 | - * This program is free software: you can redistribute it and/or modify * | |
| 8 | - * it under the terms of the GNU General Public License as published by * | |
| 9 | - * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | - * (at your option) any later version. * | |
| 11 | - * * | |
| 12 | - * This program is distributed in the hope that it will be useful, * | |
| 13 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | - * GNU General Public License for more details. * | |
| 16 | - * * | |
| 17 | - * You should have received a copy of the GNU General Public License * | |
| 18 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | - * * | |
| 20 | - ***************************************************************************/ | |
| 21 | -/** | |
| 22 | - * @file GResponseCacheKey.i | |
| 23 | - * @brief Response cache key class definition | |
| 24 | - * @author Juergen Knoedlseder | |
| 25 | - */ | |
| 26 | -%{ | |
| 27 | -/* Put headers and other declarations here that are needed for compilation */ | |
| 28 | -#include "GResponseCacheKey.hpp" | |
| 29 | -%} | |
| 30 | - | |
| 31 | - | |
| 32 | -/***********************************************************************//** | |
| 33 | - * @class GResponseCacheKey | |
| 34 | - * | |
| 35 | - * @brief Response cache key class | |
| 36 | - * | |
| 37 | - * The class implements the key for the Instrument Response Function cache. | |
| 38 | - * The key aggregates all relevant attributes to describe a cached value, | |
| 39 | - * such as model name, true and measured event energies and instrument | |
| 40 | - * direction. | |
| 41 | - ***************************************************************************/ | |
| 42 | -class GResponseCacheKey : public GContainer { | |
| 43 | - | |
| 44 | -public: | |
| 45 | - // Constructors and destructors | |
| 46 | - GResponseCacheKey(void); | |
| 47 | - explicit GResponseCacheKey(const std::string& name); | |
| 48 | - GResponseCacheKey(const GResponseCacheKey& key); | |
| 49 | - virtual ~GResponseCacheKey(void); | |
| 50 | - | |
| 51 | - // Operators | |
| 52 | - GResponseCacheKey& operator=(const GResponseCacheKey& key); | |
| 53 | - float& operator[](const int& index); | |
| 54 | - const float& operator[](const int& index) const; | |
| 55 | - bool operator<(const GResponseCacheKey& key) const; | |
| 56 | - | |
| 57 | - // Methods | |
| 58 | - void clear(void); | |
| 59 | - GResponseCacheKey* clone(void) const; | |
| 60 | - std::string classname(void) const; | |
| 61 | - bool is_empty(void) const; | |
| 62 | - int size(void) const; | |
| 63 | - void set(const int& index, const float& value); | |
| 64 | - void append(const float& value); | |
| 65 | - void insert(const int& index, float& value); | |
| 66 | - void remove(const int& index); | |
| 67 | - void reserve(const int& num); | |
| 68 | - void name(const std::string& name); | |
| 69 | - const std::string& name(void) const; | |
| 70 | -}; | |
| 71 | - | |
| 72 | - | |
| 73 | -/***********************************************************************//** | |
| 74 | - * @brief GResponseCacheKey class extension | |
| 75 | - ***************************************************************************/ | |
| 76 | -%extend GResponseCacheKey { | |
| 77 | - GResponseCacheKey copy() { | |
| 78 | - return (*self); | |
| 79 | - } | |
| 80 | -}; |
pyext/gammalib/obs.i
src/obs/GResponseCache.cpp
| ... | ... | @@ -224,7 +224,7 @@ void GResponseCache::set(const std::string& name, |
| 224 | 224 | const double& value) |
| 225 | 225 | { |
| 226 | 226 | // Get energy identifier |
| 227 | - uint64_t energy = this->energy(ereco, etrue); | |
| 227 | + uint64_t energy = hash(ereco, etrue); | |
| 228 | 228 | |
| 229 | 229 | // Set cache value |
| 230 | 230 | m_cache[name][energy][0] = value; |
| ... | ... | @@ -253,7 +253,7 @@ void GResponseCache::set(const std::string& name, |
| 253 | 253 | const double& value) |
| 254 | 254 | { |
| 255 | 255 | // Get energy and direction identifier |
| 256 | - uint64_t energy = this->energy(ereco, etrue); | |
| 256 | + uint64_t energy = hash(ereco, etrue); | |
| 257 | 257 | uint64_t instdir = dir.hash(); |
| 258 | 258 | |
| 259 | 259 | // Set cache value |
| ... | ... | @@ -288,7 +288,7 @@ bool GResponseCache::contains(const std::string& name, |
| 288 | 288 | bool contains = false; |
| 289 | 289 | |
| 290 | 290 | // Get energy identifier |
| 291 | - uint64_t energy = this->energy(ereco, etrue); | |
| 291 | + uint64_t energy = hash(ereco, etrue); | |
| 292 | 292 | uint64_t instdir = 0; |
| 293 | 293 | |
| 294 | 294 | // Search for name in cache |
| ... | ... | @@ -345,7 +345,7 @@ bool GResponseCache::contains(const std::string& name, |
| 345 | 345 | bool contains = false; |
| 346 | 346 | |
| 347 | 347 | // Get energy identifier |
| 348 | - uint64_t energy = this->energy(ereco, etrue); | |
| 348 | + uint64_t energy = hash(ereco, etrue); | |
| 349 | 349 | uint64_t instdir = dir.hash(); |
| 350 | 350 | |
| 351 | 351 | // Search for name in cache |
| ... | ... | @@ -489,30 +489,20 @@ void GResponseCache::free_members(void) |
| 489 | 489 | |
| 490 | 490 | |
| 491 | 491 | /***********************************************************************//** |
| 492 | - * @brief Encode reconstructued and true energy | |
| 492 | + * @brief Encode reconstructued and true energy into hash value | |
| 493 | 493 | * |
| 494 | 494 | * @param[in] ereco Reconstructed energy. |
| 495 | 495 | * @param[in] etrue True energy. |
| 496 | 496 | * @return Encoded reconstructued and true energy |
| 497 | 497 | * |
| 498 | 498 | * Encodes the reconstructued and true energy in a 64 Bit unsigned integer |
| 499 | - * value. The energy are converted into floating | |
| 500 | - | |
| 501 | - single double precision | |
| 502 | - * value. The encoding is done using the following formula: | |
| 503 | - * | |
| 504 | - * \f[ | |
| 505 | - * {\tt encode} = E_{\rm reco} + E_{\rm true} \times 10^2 | |
| 506 | - * \f] | |
| 507 | - * | |
| 508 | - * where | |
| 509 | - * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and | |
| 510 | - * - \f$E_{\rm true}\f$ is the true energy in TeV. | |
| 511 | - * | |
| 512 | - * @todo Verify unique encoding for all instruments!!! | |
| 499 | + * value. The energy are converted into single precision floating point | |
| 500 | + * values and mapped on a 64 Bit unsigned integer value. This provides unique | |
| 501 | + * hash values for all instruments up to the precision of floating point | |
| 502 | + * singles. | |
| 513 | 503 | ***************************************************************************/ |
| 514 | -uint64_t GResponseCache::energy(const GEnergy& ereco, | |
| 515 | - const GEnergy& etrue) const | |
| 504 | +uint64_t GResponseCache::hash(const GEnergy& ereco, | |
| 505 | + const GEnergy& etrue) const | |
| 516 | 506 | { |
| 517 | 507 | // Allocate static array to store the two energies as floats |
| 518 | 508 | static float buffer[2]; | ... | ... |
src/obs/GResponseCacheKey.cpp deleted
| 1 | -/*************************************************************************** | |
| 2 | - * GResponseCacheKey.cpp - Response cache key class * | |
| 3 | - * ----------------------------------------------------------------------- * | |
| 4 | - * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | - * ----------------------------------------------------------------------- * | |
| 6 | - * * | |
| 7 | - * This program is free software: you can redistribute it and/or modify * | |
| 8 | - * it under the terms of the GNU General Public License as published by * | |
| 9 | - * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | - * (at your option) any later version. * | |
| 11 | - * * | |
| 12 | - * This program is distributed in the hope that it will be useful, * | |
| 13 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | - * GNU General Public License for more details. * | |
| 16 | - * * | |
| 17 | - * You should have received a copy of the GNU General Public License * | |
| 18 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | - * * | |
| 20 | - ***************************************************************************/ | |
| 21 | -/** | |
| 22 | - * @file GResponseCacheKey.cpp | |
| 23 | - * @brief Response cache key class implementation | |
| 24 | - * @author Juergen Knoedlseder | |
| 25 | - */ | |
| 26 | - | |
| 27 | -/* __ Includes ___________________________________________________________ */ | |
| 28 | -#ifdef HAVE_CONFIG_H | |
| 29 | -#include <config.h> | |
| 30 | -#endif | |
| 31 | -#include "GException.hpp" | |
| 32 | -#include "GResponseCacheKey.hpp" | |
| 33 | - | |
| 34 | -/* __ Method name definitions ____________________________________________ */ | |
| 35 | -#define G_SET "GResponseCacheKey::set(int&, float&)" | |
| 36 | -#define G_INSERT "GResponseCacheKey::insert(int&, float&)" | |
| 37 | -#define G_REMOVE "GResponseCacheKey::remove(int&)" | |
| 38 | - | |
| 39 | -/* __ Macros _____________________________________________________________ */ | |
| 40 | - | |
| 41 | -/* __ Coding definitions _________________________________________________ */ | |
| 42 | - | |
| 43 | -/* __ Debug definitions __________________________________________________ */ | |
| 44 | - | |
| 45 | - | |
| 46 | -/*========================================================================== | |
| 47 | - = = | |
| 48 | - = Constructors/destructors = | |
| 49 | - = = | |
| 50 | - ==========================================================================*/ | |
| 51 | - | |
| 52 | -/***********************************************************************//** | |
| 53 | - * @brief Void constructor | |
| 54 | - ***************************************************************************/ | |
| 55 | -GResponseCacheKey::GResponseCacheKey(void) | |
| 56 | -{ | |
| 57 | - // Initialise class members | |
| 58 | - init_members(); | |
| 59 | - | |
| 60 | - // Return | |
| 61 | - return; | |
| 62 | -} | |
| 63 | - | |
| 64 | - | |
| 65 | -/***********************************************************************//** | |
| 66 | - * @brief Key name constructor | |
| 67 | - * | |
| 68 | - * @param[in] name Key name. | |
| 69 | - ***************************************************************************/ | |
| 70 | -GResponseCacheKey::GResponseCacheKey(const std::string& name) | |
| 71 | -{ | |
| 72 | - // Initialise class members | |
| 73 | - init_members(); | |
| 74 | - | |
| 75 | - // Set name | |
| 76 | - this->name(name); | |
| 77 | - | |
| 78 | - // Return | |
| 79 | - return; | |
| 80 | -} | |
| 81 | - | |
| 82 | - | |
| 83 | -/***********************************************************************//** | |
| 84 | - * @brief Copy constructor | |
| 85 | - * | |
| 86 | - * @param[in] key Response cache key. | |
| 87 | - ***************************************************************************/ | |
| 88 | -GResponseCacheKey::GResponseCacheKey(const GResponseCacheKey& key) | |
| 89 | -{ | |
| 90 | - // Initialise class members | |
| 91 | - init_members(); | |
| 92 | - | |
| 93 | - // Copy members | |
| 94 | - copy_members(key); | |
| 95 | - | |
| 96 | - // Return | |
| 97 | - return; | |
| 98 | -} | |
| 99 | - | |
| 100 | - | |
| 101 | -/***********************************************************************//** | |
| 102 | - * @brief Destructor | |
| 103 | - ***************************************************************************/ | |
| 104 | -GResponseCacheKey::~GResponseCacheKey(void) | |
| 105 | -{ | |
| 106 | - // Free members | |
| 107 | - free_members(); | |
| 108 | - | |
| 109 | - // Return | |
| 110 | - return; | |
| 111 | -} | |
| 112 | - | |
| 113 | - | |
| 114 | -/*========================================================================== | |
| 115 | - = = | |
| 116 | - = Operators = | |
| 117 | - = = | |
| 118 | - ==========================================================================*/ | |
| 119 | - | |
| 120 | -/***********************************************************************//** | |
| 121 | - * @brief Assignment operator | |
| 122 | - * | |
| 123 | - * @param[in] key Response cache key. | |
| 124 | - * @return Response cache key. | |
| 125 | - ***************************************************************************/ | |
| 126 | -GResponseCacheKey& GResponseCacheKey::operator=(const GResponseCacheKey& key) | |
| 127 | -{ | |
| 128 | - // Execute only if object is not identical | |
| 129 | - if (this != &key) { | |
| 130 | - | |
| 131 | - // Free members | |
| 132 | - free_members(); | |
| 133 | - | |
| 134 | - // Initialise private members | |
| 135 | - init_members(); | |
| 136 | - | |
| 137 | - // Copy members | |
| 138 | - copy_members(key); | |
| 139 | - | |
| 140 | - } // endif: object was not identical | |
| 141 | - | |
| 142 | - // Return this object | |
| 143 | - return *this; | |
| 144 | -} | |
| 145 | - | |
| 146 | - | |
| 147 | -/***********************************************************************//** | |
| 148 | - * @brief Compare operator | |
| 149 | - * | |
| 150 | - * @param[in] key Response cache key. | |
| 151 | - * @return True if instance is smaller than @p key. | |
| 152 | - ***************************************************************************/ | |
| 153 | -bool GResponseCacheKey::operator<(const GResponseCacheKey& key) const | |
| 154 | -{ | |
| 155 | - // Get key size | |
| 156 | - int sz = size(); | |
| 157 | - | |
| 158 | - // Compare key sizes | |
| 159 | - bool smaller = sz < key.size(); | |
| 160 | - | |
| 161 | - // If sizes are identical then continue witl comparison | |
| 162 | - if (sz == key.size()) { | |
| 163 | - | |
| 164 | - // Compare key name | |
| 165 | - smaller = m_name < key.m_name; | |
| 166 | - | |
| 167 | - // If names are identical then compare values. For small sizes we | |
| 168 | - // explicitly unroll the loop to make sure that the code is fast. | |
| 169 | - if (m_name == key.m_name) { | |
| 170 | - if (sz == 1) { | |
| 171 | - smaller = m_values[0] < key.m_values[0]; | |
| 172 | - } | |
| 173 | - else if (sz == 2) { | |
| 174 | - smaller = (m_values[0] < key.m_values[0]) || | |
| 175 | - (m_values[1] < key.m_values[1]); | |
| 176 | - } | |
| 177 | - else if (sz == 3) { | |
| 178 | - smaller = (m_values[0] < key.m_values[0]) || | |
| 179 | - (m_values[1] < key.m_values[1]) || | |
| 180 | - (m_values[2] < key.m_values[2]); | |
| 181 | - } | |
| 182 | - else if (sz == 4) { | |
| 183 | - smaller = (m_values[0] < key.m_values[0]) || | |
| 184 | - (m_values[1] < key.m_values[1]) || | |
| 185 | - (m_values[2] < key.m_values[2]) || | |
| 186 | - (m_values[3] < key.m_values[3]); | |
| 187 | - } | |
| 188 | - else if (sz == 5) { | |
| 189 | - smaller = (m_values[0] < key.m_values[0]) || | |
| 190 | - (m_values[1] < key.m_values[1]) || | |
| 191 | - (m_values[2] < key.m_values[2]) || | |
| 192 | - (m_values[3] < key.m_values[3]) || | |
| 193 | - (m_values[4] < key.m_values[4]); | |
| 194 | - } | |
| 195 | - else { | |
| 196 | - for (int i = 0; i < size(); ++i) { | |
| 197 | - smaller = m_values[i] < key.m_values[i]; | |
| 198 | - if (smaller) { | |
| 199 | - break; | |
| 200 | - } | |
| 201 | - } | |
| 202 | - } | |
| 203 | - } // endif: names were identical | |
| 204 | - | |
| 205 | - } // endif: sizes were identical | |
| 206 | - | |
| 207 | - // Return smaller flag | |
| 208 | - return smaller; | |
| 209 | -} | |
| 210 | - | |
| 211 | - | |
| 212 | -/*========================================================================== | |
| 213 | - = = | |
| 214 | - = Public methods = | |
| 215 | - = = | |
| 216 | - ==========================================================================*/ | |
| 217 | - | |
| 218 | -/***********************************************************************//** | |
| 219 | - * @brief Clear response cache | |
| 220 | - ***************************************************************************/ | |
| 221 | -void GResponseCacheKey::clear(void) | |
| 222 | -{ | |
| 223 | - // Free members | |
| 224 | - free_members(); | |
| 225 | - | |
| 226 | - // Initialise private members | |
| 227 | - init_members(); | |
| 228 | - | |
| 229 | - // Return | |
| 230 | - return; | |
| 231 | -} | |
| 232 | - | |
| 233 | - | |
| 234 | -/***********************************************************************//** | |
| 235 | - * @brief Clone instance | |
| 236 | - * | |
| 237 | - * @return Pointer to deep copy of response cache key | |
| 238 | - * | |
| 239 | - * Makes a deep copy of the response cache key. | |
| 240 | - ***************************************************************************/ | |
| 241 | -GResponseCacheKey* GResponseCacheKey::clone(void) const | |
| 242 | -{ | |
| 243 | - return new GResponseCacheKey(*this); | |
| 244 | -} | |
| 245 | - | |
| 246 | - | |
| 247 | -/***********************************************************************//** | |
| 248 | - * @brief Set key value | |
| 249 | - * | |
| 250 | - * @param[in] index Key value index [0,...,size()-1]. | |
| 251 | - * @param[in] value Key value. | |
| 252 | - * | |
| 253 | - * @exception GException::out_of_range | |
| 254 | - * Invalid key value index. | |
| 255 | - * | |
| 256 | - * Set key value in the container. | |
| 257 | - ***************************************************************************/ | |
| 258 | -void GResponseCacheKey::set(const int& index, const float& value) | |
| 259 | -{ | |
| 260 | - // Compile option: raise exception if index is out of range | |
| 261 | - #if defined(G_RANGE_CHECK) | |
| 262 | - if (index < 0 || index >= size()) { | |
| 263 | - throw GException::out_of_range(G_SET, "Key value index", index, size()); | |
| 264 | - } | |
| 265 | - #endif | |
| 266 | - | |
| 267 | - // Assign key value | |
| 268 | - m_values[index] = value; | |
| 269 | - | |
| 270 | - // Return | |
| 271 | - return; | |
| 272 | -} | |
| 273 | - | |
| 274 | - | |
| 275 | -/***********************************************************************//** | |
| 276 | - * @brief Insert key value | |
| 277 | - * | |
| 278 | - * @param[in] index Key value index [0,...,size()-1]. | |
| 279 | - * @param[in] value Key value. | |
| 280 | - * | |
| 281 | - * @exception GException::out_of_range | |
| 282 | - * Invalid key value index. | |
| 283 | - * | |
| 284 | - * Inserts a key value before the value with the specified @p index. | |
| 285 | - ***************************************************************************/ | |
| 286 | -void GResponseCacheKey::insert(const int& index, const float& value) | |
| 287 | -{ | |
| 288 | - // Compile option: raise exception if index is out of range | |
| 289 | - #if defined(G_RANGE_CHECK) | |
| 290 | - if (index < 0 || index >= size()) { | |
| 291 | - throw GException::out_of_range(G_INSERT, "Key value index", index, size()); | |
| 292 | - } | |
| 293 | - #endif | |
| 294 | - | |
| 295 | - // Inserts key value | |
| 296 | - m_values.insert(m_values.begin()+index, value); | |
| 297 | - | |
| 298 | - // Return | |
| 299 | - return; | |
| 300 | -} | |
| 301 | - | |
| 302 | - | |
| 303 | -/***********************************************************************//** | |
| 304 | - * @brief Remove key value | |
| 305 | - * | |
| 306 | - * @param[in] index Key value index [0,...,size()-1]. | |
| 307 | - * | |
| 308 | - * @exception GException::out_of_range | |
| 309 | - * Invalid key value index. | |
| 310 | - * | |
| 311 | - * Removes key value with the specified @p index. | |
| 312 | - ***************************************************************************/ | |
| 313 | -void GResponseCacheKey::remove(const int& index) | |
| 314 | -{ | |
| 315 | - // Compile option: raise exception if index is out of range | |
| 316 | - #if defined(G_RANGE_CHECK) | |
| 317 | - if (index < 0 || index >= size()) { | |
| 318 | - throw GException::out_of_range(G_REMOVE, "Key value index", index, size()); | |
| 319 | - } | |
| 320 | - #endif | |
| 321 | - | |
| 322 | - // Erase key value | |
| 323 | - m_values.erase(m_values.begin()+index); | |
| 324 | - | |
| 325 | - // Return | |
| 326 | - return; | |
| 327 | -} | |
| 328 | - | |
| 329 | - | |
| 330 | -/***********************************************************************//** | |
| 331 | - * @brief Print response cache key | |
| 332 | - * | |
| 333 | - * @param[in] chatter Chattiness. | |
| 334 | - * @return String containing response cache key information. | |
| 335 | - ***************************************************************************/ | |
| 336 | -std::string GResponseCacheKey::print(const GChatter& chatter) const | |
| 337 | -{ | |
| 338 | - // Initialise result string | |
| 339 | - std::string result; | |
| 340 | - | |
| 341 | - // Continue only if chatter is not silent | |
| 342 | - if (chatter != SILENT) { | |
| 343 | - | |
| 344 | - // Append header | |
| 345 | - result.append("=== GResponseCacheKey ==="); | |
| 346 | - | |
| 347 | - // Append total cache size | |
| 348 | - result.append("\n"+gammalib::parformat("Key name")); | |
| 349 | - result.append(m_name); | |
| 350 | - result.append("\n"+gammalib::parformat("Number of key values")); | |
| 351 | - result.append(gammalib::str(size())); | |
| 352 | - for (int i = 0; i < size(); ++i) { | |
| 353 | - result.append("\n"+gammalib::parformat("Key value "+gammalib::str(i+1))); | |
| 354 | - result.append(gammalib::str(m_values[i])); | |
| 355 | - } | |
| 356 | - | |
| 357 | - } // endif: chatter was not silent | |
| 358 | - | |
| 359 | - // Return result | |
| 360 | - return result; | |
| 361 | -} | |
| 362 | - | |
| 363 | - | |
| 364 | -/*========================================================================== | |
| 365 | - = = | |
| 366 | - = Private methods = | |
| 367 | - = = | |
| 368 | - ==========================================================================*/ | |
| 369 | - | |
| 370 | -/***********************************************************************//** | |
| 371 | - * @brief Initialise class members | |
| 372 | - ***************************************************************************/ | |
| 373 | -void GResponseCacheKey::init_members(void) | |
| 374 | -{ | |
| 375 | - // Initialise members | |
| 376 | - m_name.clear(); | |
| 377 | - m_values.clear(); | |
| 378 | - | |
| 379 | - // Return | |
| 380 | - return; | |
| 381 | -} | |
| 382 | - | |
| 383 | - | |
| 384 | -/***********************************************************************//** | |
| 385 | - * @brief Copy class members | |
| 386 | - * | |
| 387 | - * @param[in] key Response cache key. | |
| 388 | - ***************************************************************************/ | |
| 389 | -void GResponseCacheKey::copy_members(const GResponseCacheKey& key) | |
| 390 | -{ | |
| 391 | - // Copy members | |
| 392 | - m_name = key.m_name; | |
| 393 | - m_values = key.m_values; | |
| 394 | - | |
| 395 | - // Return | |
| 396 | - return; | |
| 397 | -} | |
| 398 | - | |
| 399 | - | |
| 400 | -/***********************************************************************//** | |
| 401 | - * @brief Delete class members | |
| 402 | - ***************************************************************************/ | |
| 403 | -void GResponseCacheKey::free_members(void) | |
| 404 | -{ | |
| 405 | - // Return | |
| 406 | - return; | |
| 407 | -} |