Tulip  5.3.1
Large graphs analysis and drawing
TlpTools.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef _TLPTOOLS_H
21 #define _TLPTOOLS_H
22 
23 #include <iostream>
24 #include <climits>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <tulip/tulipconf.h>
28 
29 #ifdef WIN32
30 typedef struct _stat tlp_stat_t;
31 #else
32 typedef struct stat tlp_stat_t;
33 #endif
34 
35 // The hash_combine function from the boost library
36 // Call it repeatedly to incrementally create a hash value from several variables.
37 
38 // Explanation of the formula from StackOverflow
39 // (http://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine) :
40 // The magic number 0x9e3779b9 = 2^32 / ((1 + sqrt(5)) / 2) is the reciprocal of the golden ratio.
41 // It is supposed to be 32 random bits, where each is equally likely to be 0 or 1, and with no
42 // simple correlation between the bits.
43 // So including this number "randomly" changes each bit of the seed; as you say, this means that
44 // consecutive values will be far apart.
45 // Including the shifted versions of the old seed makes sure that, even if hash_value() has a fairly
46 // small range of values,
47 // differences will soon be spread across all the bits.
48 namespace std {
49 template <class T>
50 inline void tlp_hash_combine(std::size_t &seed, const T &v) {
51  hash<T> hasher;
52  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
53 }
54 } // namespace std
55 
56 namespace tlp {
57 extern TLP_SCOPE const char PATH_DELIMITER;
58 extern TLP_SCOPE std::string TulipLibDir;
59 extern TLP_SCOPE std::string TulipPluginsPath;
60 extern TLP_SCOPE std::string TulipBitmapDir;
61 extern TLP_SCOPE std::string TulipShareDir;
62 
63 /**
64  * @ingroup Plugins
65  *
66  * @brief Initializes the Tulip library.
67  * Looks for the Tulip plug-ins directory.
68  * The plug-ins directory can be defined in different ways, given by order of prevalence :
69  * 1. the TLP_DIR environment variable, if it has a value
70  * 2. the appDirPath parameter, if it is not nullptr
71  * 3. at that point, the Tulip paths will be retrieved from the path of the loaded Tulip shared
72  * library
73  * (you must dispose of a standard Tulip installation for that feature to work).
74  * 4. a fallback value of 'C:/Tulip/lib/' on windows, or '/usr/local/lib/' on Unix.
75  */
76 extern TLP_SCOPE void initTulipLib(const char *appDirPath = nullptr);
77 
78 /**
79  * @ingroup Plugins
80  *
81  * @brief Demangles the name of a C++ class
82  * @param className The mangled name of a class
83  * @param hideTlp a flag to indicate if the 'tlp::' prefix
84  * @return string The demangled name of a Tulip C++ class.
85  */
86 TLP_SCOPE std::string demangleClassName(const char *className, bool hideTlp = false);
87 
88 /**
89  * @ingroup Plugins
90  *
91  * @brief Demangles the name of a C++ class defined in the tlp namespace.
92  * @param className The mangled name of a class
93  * @return string The demangled name of a Tulip C++ class
94  * without the tlp:: prefix
95  */
96 inline std::string demangleTlpClassName(const char *className) {
97  return demangleClassName(className, true);
98 }
99 
100 /**
101  * @brief Returns an istream to read from a gzipped file (uses gzstream lib).
102  * The stream has to be deleted after use.
103  * @param name The name of the file to read from.
104  * @param open_mode The mode to open the file with. Defaults to std::ios::in.
105  * @return istream gzipped input stream from a file.
106  */
107 TLP_SCOPE std::istream *getIgzstream(const std::string &name, int open_mode = std::ios::in);
108 
109 /**
110  * @brief Returns an ostream to write to a gzipped file (uses gzstream lib).
111  * The stream has to be deleted after use.
112  * @warning Don't forget to check the stream with ios::bad()!
113  * @param name The name of the file to write to.
114  * @param open_mode The mode to open the file with. Defaults to std::ios::out.
115  * @return ostream gzipped output stream to a file.
116  */
117 TLP_SCOPE std::ostream *getOgzstream(const std::string &name, int open_mode = std::ios::out);
118 
119 /**
120  * @brief Gives the value of the seed used for further initialization
121  * of a random sequence (with further calls to rand() or random()).
122  * @param seed the value of the seed.
123  * Set seed to UINT_MAX if you need a random choice of the seed.
124  */
125 TLP_SCOPE void setSeedOfRandomSequence(unsigned int seed = UINT_MAX);
126 
127 /**
128  * @brief Returns the value of the seed used for further initialization
129  * of a random sequence
130  */
131 TLP_SCOPE unsigned int getSeedOfRandomSequence();
132 
133 /**
134  * @brief Initializes a random sequence with the seed previously set
135  * Further calls to rand() or random() will return the elements of
136  * that sequence
137  */
138 TLP_SCOPE void initRandomSequence();
139 
140 /**
141  * @brief Returns a random integer in the range [0, max] if max is positive or in the range [max, 0]
142  * if max is negative
143  */
144 TLP_SCOPE int randomInteger(int max);
145 
146 /**
147  * @brief Returns a random unsigned integer in the range [0, max]
148  */
149 TLP_SCOPE unsigned int randomUnsignedInteger(unsigned int max);
150 
151 /**
152  * @brief Returns a random double in the range [0, max]
153  */
154 TLP_SCOPE double randomDouble(double max = 1.0);
155 
156 /**
157  * @brief Cross-platform function to stat a path on a filesystem. Its purpose is to support
158  * paths on windows platform containing non-ascii characters.
159  * @param pathname an utf-8 encoded string containing the path name to stat
160  * @param buf a pointer to a tlp_stat_t structure (a typedef for struct stat)
161  */
162 TLP_SCOPE int statPath(const std::string &pathname, tlp_stat_t *buf);
163 
164 /**
165  * @brief Cross-platform function to get an input file stream. Its purpose is to support
166  * paths on windows platform containing non-ascii characters.
167  * @param filename an utf-8 encoded string containing the path of the file to open for performing
168  * input operations
169  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
170  * constants).
171  */
172 TLP_SCOPE std::istream *getInputFileStream(const std::string &filename,
173  std::ios_base::openmode open_mode = std::ios::in);
174 
175 /**
176  * @brief Cross-platform function to get an output file stream. Its purpose is to support
177  * paths on windows platform containing non-ascii characters.
178  * @param filename an utf-8 encoded string containing the path of the file to open for performing
179  * output operations
180  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
181  * constants).
182  */
183 TLP_SCOPE std::ostream *getOutputFileStream(const std::string &filename,
184  std::ios_base::openmode open_mode = std::ios::out);
185 
186 ///@cond DOXYGEN_HIDDEN
187 // Gui test mode
188 TLP_SCOPE bool inGuiTestingMode();
189 
190 TLP_SCOPE void setGuiTestingMode(bool);
191 ///@endcond
192 } // namespace tlp
193 #endif
double randomDouble(double max=1.0)
Returns a random double in the range [0, max].
int statPath(const std::string &pathname, tlp_stat_t *buf)
Cross-platform function to stat a path on a filesystem. Its purpose is to support paths on windows pl...
Definition: TlpTools.h:48
std::ostream * getOutputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::out)
Cross-platform function to get an output file stream. Its purpose is to support paths on windows plat...
void initTulipLib(const char *appDirPath=nullptr)
Initializes the Tulip library. Looks for the Tulip plug-ins directory. The plug-ins directory can be ...
int randomInteger(int max)
Returns a random integer in the range [0, max] if max is positive or in the range [max...
unsigned int randomUnsignedInteger(unsigned int max)
Returns a random unsigned integer in the range [0, max].
unsigned int getSeedOfRandomSequence()
Returns the value of the seed used for further initialization of a random sequence.
std::string demangleTlpClassName(const char *className)
Demangles the name of a C++ class defined in the tlp namespace.
Definition: TlpTools.h:96
std::istream * getInputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::in)
Cross-platform function to get an input file stream. Its purpose is to support paths on windows platf...
std::istream * getIgzstream(const std::string &name, int open_mode=std::ios::in)
Returns an istream to read from a gzipped file (uses gzstream lib). The stream has to be deleted afte...
void initRandomSequence()
Initializes a random sequence with the seed previously set Further calls to rand() or random() will r...
std::ostream * getOgzstream(const std::string &name, int open_mode=std::ios::out)
Returns an ostream to write to a gzipped file (uses gzstream lib). The stream has to be deleted after...
void setSeedOfRandomSequence(unsigned int seed=UINT_MAX)
Gives the value of the seed used for further initialization of a random sequence (with further calls ...
std::string demangleClassName(const char *className, bool hideTlp=false)
Demangles the name of a C++ class.