Commit 2cb704856168422af0d5d61fa391efd0c62497d8

Authored by Jürgen Knödlseder
1 parent 08490e56

Add GCTAResponseCache::nnames(), GCTAResponseCache::name() and GCTAResponseCache::remove() methods

inst/cta/include/GCTAResponseCache.hpp
1 1 /***************************************************************************
2 2 * GCTAResponseCache.hpp - CTA response cache class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2018 by Juergen Knoedlseder *
  4 + * copyright (C) 2018-2019 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -70,6 +70,7 @@ public:
70 70 std::string classname(void) const;
71 71 int size(void) const;
72 72 bool is_empty(void) const;
  73 + int nnames(void) const;
73 74 int ndirs(void) const;
74 75 int nerecos(void) const;
75 76 int netrues(void) const;
... ... @@ -91,6 +92,8 @@ public:
91 92 const GEnergy& ereco,
92 93 const GEnergy& etrue,
93 94 double* value = NULL) const;
  95 + std::string name(const int& index) const;
  96 + void remove(const std::string& name);
94 97 std::string print(const GChatter& chatter = NORMAL) const;
95 98  
96 99 protected:
... ... @@ -129,4 +132,19 @@ bool GCTAResponseCache::is_empty(void) const
129 132 return (m_cache.empty());
130 133 }
131 134  
  135 +
  136 +/***********************************************************************//**
  137 + * @brief Return number of names in cache
  138 + *
  139 + * @return Number of names in cache
  140 + *
  141 + * Returns the number of names in the response cache.
  142 + ***************************************************************************/
  143 +inline
  144 +int GCTAResponseCache::nnames(void) const
  145 +{
  146 + // Return number of names
  147 + return m_cache.size();
  148 +}
  149 +
132 150 #endif /* GCTARESPONSECACHE_HPP */
... ...
inst/cta/src/GCTAResponseCache.cpp
1 1 /***************************************************************************
2 2 * GCTAResponseCache.cpp - CTA response cache class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2018 by Juergen Knoedlseder *
  4 + * copyright (C) 2018-2019 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -33,6 +33,7 @@
33 33 #include "GCTAInstDir.hpp"
34 34  
35 35 /* __ Method name definitions ____________________________________________ */
  36 +#define G_NAME "GCTAResponseCache::name(int&)"
36 37  
37 38 /* __ Macros _____________________________________________________________ */
38 39  
... ... @@ -470,6 +471,62 @@ bool GCTAResponseCache::contains(const std::string& name,
470 471  
471 472  
472 473 /***********************************************************************//**
  474 + * @brief Return name of cache element
  475 + *
  476 + * @param[in] index Cache element number (0,...,nnames()-1).
  477 + * @return Name of cache element.
  478 + *
  479 + * Returns name of cache element with specified @p index.
  480 + ***************************************************************************/
  481 +std::string GCTAResponseCache::name(const int& index) const
  482 +{
  483 + // Raise an exception if index is out of range
  484 + if (index < 0 || index >= size()) {
  485 + throw GException::out_of_range(G_NAME, index, size());
  486 + }
  487 +
  488 + // Initialise name
  489 + std::string name;
  490 +
  491 + // Initialise counter
  492 + int counter = 0;
  493 +
  494 + // Loop over names and extract the name that corresponds to the index
  495 + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin();
  496 + it_name != m_cache.end(); ++it_name, ++counter) {
  497 + if (counter == index) {
  498 + name = it_name->first;
  499 + break;
  500 + }
  501 + }
  502 +
  503 + // Return name
  504 + return name;
  505 +}
  506 +
  507 +
  508 +/***********************************************************************//**
  509 + * @brief Remove cache values for a given name
  510 + *
  511 + * @param[in] name Cache name.
  512 + *
  513 + * Removes the cache values for a given @p name. If the @p name does not
  514 + * exist in the cache the method does nothing.
  515 + ***************************************************************************/
  516 +void GCTAResponseCache::remove(const std::string& name)
  517 +{
  518 + // Search for name in cache
  519 + GCTAResponseCacheName::const_iterator it_name = m_cache.find(name);
  520 + if (it_name != m_cache.end()) {
  521 + m_cache.erase(it_name);
  522 + }
  523 +
  524 + // Return
  525 + return;
  526 +}
  527 +
  528 +
  529 +/***********************************************************************//**
473 530 * @brief Clone CTA response cache
474 531 *
475 532 * @return Pointer to deep copy of CTA response cache.
... ...