Jpp
JHashSet.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 
6 #include "JTools/JHashSet.hh"
7 #include "JLang/JObjectID.hh"
8 #include "JIO/JFileStreamIO.hh"
9 
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 
13 
14 namespace {
15 
16  using namespace JPP;
17 
18 
19  struct __A__ :
20  public JObjectID
21  {
22  /**
23  * Default constructor.
24  */
25  __A__() :
26  JObjectID()
27  {}
28 
29 
30  /**
31  * Constructor.
32  *
33  * \param id identifier
34  */
35  __A__(const int id) :
36  JObjectID(id)
37  {}
38  };
39 }
40 
41 
42 /**
43  * \file
44  *
45  * Example program to test JTOOLS::JHashSet class.
46  * \author mdejong
47  */
48 int main(int argc, char **argv)
49 {
50  using namespace std;
51 
52  string inputFile;
53  string outputFile;
54  int debug;
55 
56  try {
57 
58  JParser<> zap("Example program to test hash set.");
59 
60  zap['f'] = make_field(inputFile) = "";
61  zap['o'] = make_field(outputFile) = "";
62  zap['d'] = make_field(debug) = 0;
63 
64  zap(argc, argv);
65  }
66  catch(const exception &error) {
67  FATAL(error.what() << endl);
68  }
69 
70 
71  using namespace JPP;
72 
73  typedef JHashSet<__A__> hash_set;
74 
75  hash_set buffer;
76 
77  if (inputFile != "") {
78 
79  DEBUG("Reading " << inputFile << "... " << flush);
80 
81  JFileStreamReader in(inputFile.c_str());
82 
83  in >> buffer;
84 
85  in.close();
86 
87  DEBUG("OK" << endl);
88 
89  } else {
90 
91  for (int i = 1<<10; i != 0; i >>= 1) {
92  buffer.insert(__A__(i));
93  }
94 
95  buffer.erase(buffer.find(1<<2), buffer.find(1<<4));
96 
97  if (!buffer.erase(1<<4)) {
98  FATAL("Erasing value failed." << endl);
99  }
100  }
101 
102  for (hash_set::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
103 
104  cout << setw(4) << *i;
105  cout << " == ";
106 
107  hash_set::const_iterator p = buffer.find(*i);
108 
109  if (p != buffer.end())
110  cout << setw(4) << *p << endl;
111  else
112  FATAL("Inconsistent has set at " << *i << endl);
113  }
114 
115  if (outputFile != "") {
116 
117  DEBUG("Writing " << outputFile << "... " << flush);
118 
119  JFileStreamWriter out(outputFile.c_str());
120 
121  out << buffer;
122 
123  out.close();
124 
125  DEBUG("OK" << endl);
126  }
127 }
JIO::JFileStreamWriter
Binary buffered file output.
Definition: JFileStreamIO.hh:72
JIO::JFileStreamReader
Binary buffered file input.
Definition: JFileStreamIO.hh:22
JHashSet.hh
JMessage.hh
main
int main(int argc, char **argv)
Definition: JHashSet.cc:48
JFileStreamIO.hh
JObjectID.hh
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
debug
int debug
debug level
Definition: JSirene.cc:59
JIO::JFileStreamWriter::close
void close()
Close file.
Definition: JFileStreamIO.hh:121
JLANG::JObjectID
Auxiliary class for object identification.
Definition: JObjectID.hh:27
JParser.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JTOOLS::JHashSet
General purpose class for hash set of elements.
Definition: JHashSet.hh:34