From 2cb704856168422af0d5d61fa391efd0c62497d8 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Mon, 27 May 2019 15:13:35 +0200 Subject: [PATCH] Add GCTAResponseCache::nnames(), GCTAResponseCache::name() and GCTAResponseCache::remove() methods --- inst/cta/include/GCTAResponseCache.hpp | 20 +++++++++++++++++++- inst/cta/src/GCTAResponseCache.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/inst/cta/include/GCTAResponseCache.hpp b/inst/cta/include/GCTAResponseCache.hpp index d6bd122..1f4c21b 100644 --- a/inst/cta/include/GCTAResponseCache.hpp +++ b/inst/cta/include/GCTAResponseCache.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GCTAResponseCache.hpp - CTA response cache class * * ----------------------------------------------------------------------- * - * copyright (C) 2018 by Juergen Knoedlseder * + * copyright (C) 2018-2019 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -70,6 +70,7 @@ public: std::string classname(void) const; int size(void) const; bool is_empty(void) const; + int nnames(void) const; int ndirs(void) const; int nerecos(void) const; int netrues(void) const; @@ -91,6 +92,8 @@ public: const GEnergy& ereco, const GEnergy& etrue, double* value = NULL) const; + std::string name(const int& index) const; + void remove(const std::string& name); std::string print(const GChatter& chatter = NORMAL) const; protected: @@ -129,4 +132,19 @@ bool GCTAResponseCache::is_empty(void) const return (m_cache.empty()); } + +/***********************************************************************//** + * @brief Return number of names in cache + * + * @return Number of names in cache + * + * Returns the number of names in the response cache. + ***************************************************************************/ +inline +int GCTAResponseCache::nnames(void) const +{ + // Return number of names + return m_cache.size(); +} + #endif /* GCTARESPONSECACHE_HPP */ diff --git a/inst/cta/src/GCTAResponseCache.cpp b/inst/cta/src/GCTAResponseCache.cpp index d206207..6f78a13 100644 --- a/inst/cta/src/GCTAResponseCache.cpp +++ b/inst/cta/src/GCTAResponseCache.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GCTAResponseCache.cpp - CTA response cache class * * ----------------------------------------------------------------------- * - * copyright (C) 2018 by Juergen Knoedlseder * + * copyright (C) 2018-2019 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -33,6 +33,7 @@ #include "GCTAInstDir.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_NAME "GCTAResponseCache::name(int&)" /* __ Macros _____________________________________________________________ */ @@ -470,6 +471,62 @@ bool GCTAResponseCache::contains(const std::string& name, /***********************************************************************//** + * @brief Return name of cache element + * + * @param[in] index Cache element number (0,...,nnames()-1). + * @return Name of cache element. + * + * Returns name of cache element with specified @p index. + ***************************************************************************/ +std::string GCTAResponseCache::name(const int& index) const +{ + // Raise an exception if index is out of range + if (index < 0 || index >= size()) { + throw GException::out_of_range(G_NAME, index, size()); + } + + // Initialise name + std::string name; + + // Initialise counter + int counter = 0; + + // Loop over names and extract the name that corresponds to the index + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin(); + it_name != m_cache.end(); ++it_name, ++counter) { + if (counter == index) { + name = it_name->first; + break; + } + } + + // Return name + return name; +} + + +/***********************************************************************//** + * @brief Remove cache values for a given name + * + * @param[in] name Cache name. + * + * Removes the cache values for a given @p name. If the @p name does not + * exist in the cache the method does nothing. + ***************************************************************************/ +void GCTAResponseCache::remove(const std::string& name) +{ + // Search for name in cache + GCTAResponseCacheName::const_iterator it_name = m_cache.find(name); + if (it_name != m_cache.end()) { + m_cache.erase(it_name); + } + + // Return + return; +} + + +/***********************************************************************//** * @brief Clone CTA response cache * * @return Pointer to deep copy of CTA response cache. -- libgit2 0.21.4