The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
MapAlg.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11// ============================================================================
12// Include files
13// ============================================================================
14// from GaudiKernel
15// ============================================================================
16#include <GaudiKernel/HashMap.h>
18#include <GaudiKernel/Map.h>
20#include <GaudiKernel/System.h>
23// ============================================================================
25// ============================================================================
37// ============================================================================
49class MapAlg : public Algorithm {
50public:
51 typedef int Key;
52 typedef double Value;
53
54public:
56 StatusCode finalize() override {
57 always() << endmsg << " FINALIZE " << endmsg << " \tTested maps : " << endmsg << " \t1) "
58 << System::typeinfoName( typeid( m_map1 ) ) << endmsg << " \t2) "
59 << System::typeinfoName( typeid( m_map2 ) ) << endmsg << " \t3) "
60 << System::typeinfoName( typeid( m_map3 ) ) << endmsg << " \t4) "
61 << System::typeinfoName( typeid( m_map4 ) ) << endmsg;
62 // finalize the base class
63 return Algorithm::finalize();
64 };
65
66 StatusCode execute() override;
67
68public:
74
76 MapAlg( const MapAlg& ) = delete;
78 MapAlg& operator=( const MapAlg& ) = delete;
79
80private:
82 void print1( Key key ) const;
84 void print2( Key key );
85
86private:
87 typedef std::map<Key, Value> Map1;
91
96};
97// ==========================================================================
99// ==========================================================================
101
102// ==========================================================================
104// ==========================================================================
106 using namespace Gaudi::Utils;
107
108 Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
109 Rndm::Numbers gauss2( randSvc(), Rndm::Gauss( 0.0, 10.0 ) );
110
111 const Key key = Key( gauss2() );
112 const Value value1 = Value( int( 100 * gauss() ) ) / 100.0;
113
114 always() << " Inserting key " << toString( key ) << " 1st: "
115 << " " << toString( m_map1.insert( std::make_pair( key, value1 ) ).second ) << " "
116 << toString( m_map2.insert( std::make_pair( key, value1 ) ).second ) << " "
117 << toString( m_map3.insert( std::make_pair( key, value1 ) ).second ) << " "
118 << toString( m_map4.insert( std::make_pair( key, value1 ) ).second ) << endmsg;
119
120 always() << "1 Map1: " << toString( m_map1 ) << endmsg;
121 always() << "1 Map2: " << toString( m_map2 ) << endmsg;
122 always() << "1 Map3: " << toString( m_map3 ) << endmsg;
123 always() << "1 Map4: " << toString( m_map4 ) << endmsg;
124
125 print1( (Key)1 );
126
127 always() << "2 Map1: " << toString( m_map1 ) << endmsg;
128 always() << "2 Map2: " << toString( m_map2 ) << endmsg;
129 always() << "2 Map3: " << toString( m_map3 ) << endmsg;
130 always() << "2 Map4: " << toString( m_map4 ) << endmsg;
131
132 print2( (Key)7 );
133
134 always() << "3 Map1: " << toString( m_map1 ) << endmsg;
135 always() << "3 Map2: " << toString( m_map2 ) << endmsg;
136 always() << "3 Map3: " << toString( m_map3 ) << endmsg;
137 always() << "3 Map4: " << toString( m_map4 ) << endmsg;
138
139 const Value value2 = gauss();
140
141 always() << " Inserting key " << toString( key ) << " 2nd: "
142 << " " << toString( m_map1.insert( std::make_pair( key, value2 ) ).second ) << " "
143 << toString( m_map2.insert( std::make_pair( key, value2 ) ).second ) << " "
144 << toString( m_map3.insert( std::make_pair( key, value2 ) ).second ) << " "
145 << toString( m_map4.insert( std::make_pair( key, value2 ) ).second ) << endmsg;
146
147 always() << "4 Map1: " << toString( m_map1 ) << endmsg;
148 always() << "4 Map2: " << toString( m_map2 ) << endmsg;
149 always() << "4 Map3: " << toString( m_map3 ) << endmsg;
150 always() << "4 Map4: " << toString( m_map4 ) << endmsg;
151
152 if ( 0 == ::labs( key ) % 2 ) {
153 always() << " Erased : "
154 << " " << toString( 0 != m_map1.erase( key ) ) << " " << toString( 0 != m_map2.erase( key ) ) << " "
155 << toString( 0 != m_map3.erase( key ) ) << " " << toString( 0 != m_map4.erase( key ) ) << endmsg;
156 }
157
158 always() << "5 Map1: " << toString( m_map1 ) << endmsg;
159 always() << "5 Map2: " << toString( m_map2 ) << endmsg;
160 always() << "5 Map3: " << toString( m_map3 ) << endmsg;
161 always() << "5 Map4: " << toString( m_map4 ) << endmsg;
162
163 always() << " Count key 0 : "
164 << " " << m_map1.count( 0 ) << " " << m_map2.count( 0 ) << " " << m_map3.count( 0 ) << " "
165 << m_map4.count( 0 ) << endmsg;
166 always() << " Count key 1 : "
167 << " " << m_map1.count( 1 ) << " " << m_map2.count( 1 ) << " " << m_map3.count( 1 ) << " "
168 << m_map4.count( 1 ) << endmsg;
169 always() << " Count key 7 : "
170 << " " << m_map1.count( 7 ) << " " << m_map2.count( 7 ) << " " << m_map3.count( 7 ) << " "
171 << m_map4.count( 7 ) << endmsg;
172 always() << " Count key -100 : "
173 << " " << m_map1.count( -100 ) << " " << m_map2.count( -100 ) << " " << m_map3.count( -100 ) << " "
174 << m_map4.count( -100 ) << endmsg;
175
176 return StatusCode::SUCCESS;
177}
178// ============================================================================
179void MapAlg::print1( MapAlg::Key key ) const {
180 always() << " CONST: "
181 << " map2['" << key << "']: " << m_map2[key] << ", map3['" << key << "']: " << m_map3[key] << ", map4['"
182 << key << "']: " << m_map4[key] << endmsg;
183}
184// ============================================================================
186 always() << " NON-CONST: "
187 << " map1['" << key << "']: " << m_map1[key] << ", map2['" << key << "']: " << m_map2[key] << ", map3['"
188 << key << "']: " << m_map3[key] << ", map4['" << key << "']: " << m_map4[key] << endmsg;
189}
190// ============================================================================
191
192// ==========================================================================
193// The END
194// ==========================================================================
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
implementation of various functions for streaming.
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
Common class providing an architecture-independent hash map.
Definition HashMap.h:80
Extension of the STL map.
Definition Map.h:83
A bit modified version of 'Loki::AssocVector' associative vector from Loki library by Andrei Alexandr...
Definition VectorMap.h:100
Simple algorithm which shows various "map-like" containers available in Gaudi.
Definition MapAlg.cpp:49
Map1 m_map1
Definition MapAlg.cpp:92
double Value
Definition MapAlg.cpp:52
void print2(Key key)
non-const printout
Definition MapAlg.cpp:185
void print1(Key key) const
const-printout
Definition MapAlg.cpp:179
StatusCode execute() override
the main execution method
Definition MapAlg.cpp:105
Map3 m_map3
Definition MapAlg.cpp:94
MapAlg & operator=(const MapAlg &)=delete
The assignement is disabled.
StatusCode finalize() override
finalization
Definition MapAlg.cpp:56
GaudiUtils::VectorMap< Key, Value > Map2
Definition MapAlg.cpp:88
int Key
Definition MapAlg.cpp:51
GaudiUtils::Map< Key, Value > Map3
Definition MapAlg.cpp:89
std::map< Key, Value > Map1
Definition MapAlg.cpp:87
MapAlg(const MapAlg &)=delete
The copy constructor is disabled.
Map2 m_map2
Definition MapAlg.cpp:93
GaudiUtils::HashMap< Key, Value > Map4
Definition MapAlg.cpp:90
Map4 m_map4
Definition MapAlg.cpp:95
Parameters for the Gauss random number generation.
Random number accessor This small class encapsulates the use of the random number generator.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition ToStream.h:326
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260