/usr/include/scilab/tools.hxx is in scilab-include 6.0.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | /*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
*
* Copyright (C) 2012 - 2016 - Scilab Enterprises
*
* This file is hereby licensed under the terms of the GNU GPL v2.0,
* pursuant to article 5.3.4 of the CeCILL v.2.1.
* This file was originally licensed under the terms of the CeCILL v2.1,
* and continues to be available under such terms.
* For more information, see the COPYING file which you should have received
* along with this program.
*
*/
#ifndef __TOOLS_HXX__
#define __TOOLS_HXX__
#include <bitset>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#ifdef _MSC_VER
#include "stdint.h"
#include <intrin.h>
#endif
#include "core_math.h"
#include "symbol.hxx"
namespace analysis
{
namespace tools
{
#ifdef _MSC_VER
inline static double trunc(const double x)
{
return x > 0 ? floor(x) : ceil(x);
}
inline static uint32_t clz(const uint32_t x)
{
unsigned long r = 0;
_BitScanForward(&r, x);
return r;
}
inline static uint32_t clzll(const uint64_t x)
{
#ifdef _WIN64
unsigned long r = 0;
_BitScanForward64(&r, x);
return r;
#else
const uint32_t u32 = x >> 32;
if (u32)
{
return clz(u32);
}
return 32 + clz(x & 0xFFFFFFFFUL);
#endif
}
#else
inline static double trunc(const double x)
{
#ifdef __APPLE__
// Needed for compilation with GCC 4.8.2
return x > 0 ? floor(x) : ceil(x);
#else
return std::trunc(x);
#endif
}
inline static uint32_t clz(const uint32_t x)
{
return x ? __builtin_clz(x) : 32;
}
inline static uint32_t clzll(const uint64_t x)
{
return x ? __builtin_clzll(x) : 64;
}
#endif
inline static double NaN()
{
return std::numeric_limits<double>::quiet_NaN();
}
inline static bool isNaN(const double x)
{
return ISNAN(x) != 0;
}
inline static bool isFinite(const double x)
{
return finite(x) != 0;
}
inline static bool isInfinite(const double x)
{
return !isFinite(x);
}
enum IntType { NOTANINT, SIGNED, UNSIGNED };
inline static IntType getIntType(const double x)
{
if (x == trunc(x))
{
if (x >= 0)
{
if (x <= (double)std::numeric_limits<uint64_t>::max())
{
return UNSIGNED;
}
}
else if (x >= (double)std::numeric_limits<int64_t>::min())
{
return SIGNED;
}
}
return NOTANINT;
}
template<typename T>
inline static bool asInteger(const double x, T & ival)
{
if (x == trunc(x))
{
if (x >= 0)
{
if (x <= (double)std::numeric_limits<T>::max())
{
ival = (T)x;
return true;
}
}
else if (x >= (double)std::numeric_limits<T>::min())
{
ival = (T)x;
return true;
}
}
return false;
}
inline static bool isAnInt(const double x)
{
return getIntType(x) != NOTANINT;
}
template<typename T>
inline static T cast(const double x)
{
if (x < static_cast<double>(std::numeric_limits<T>::max()))
{
if (x > static_cast<double>(std::numeric_limits<T>::min()))
{
return static_cast<T>(x);
}
else
{
return std::numeric_limits<T>::min();
}
}
else
{
return std::numeric_limits<T>::max();
}
}
template<typename T>
inline static T powui(T x, uint64_t n)
{
T p = x;
T y = (n & 1) ? x : 1;
while (n >>= 1)
{
p *= p;
if (n & 1)
{
y *= p;
}
}
return y;
}
inline std::wostream & operator<<(std::wostream & out, const IntType & it)
{
switch (it)
{
case IntType::NOTANINT :
out << L"NAI";
break;
case IntType::SIGNED :
out << L'S';
break;
case IntType::UNSIGNED :
out << L'U';
break;
}
return out;
}
template<typename T>
inline static unsigned char popcount(const T x)
{
return std::bitset<sizeof(T)>(x).count();
}
inline static unsigned char log2(const unsigned int x)
{
return (unsigned char)((sizeof(unsigned int) << 3) - clz(x) - 1);
}
inline static unsigned char log2(const unsigned long long x)
{
return (unsigned char)((sizeof(unsigned long long) << 3) - clzll(x) - 1);
}
template<typename T>
static void printSet(const T & set, std::wostream & out)
{
if (set.empty())
{
out << L"{}";
}
else
{
out << L'{';
for (typename T::const_iterator i = set.begin(); i != set.end(); ++i)
{
if (std::next(i) == set.end())
{
out << *i << L'}';
}
else
{
out << *i << L',';
}
}
}
}
template<typename T>
static void printMap(const T & map, std::wostream & out, const bool newLine = false )
{
if (map.empty())
{
out << L"{}";
}
else
{
out << L'{';
for (typename T::const_iterator i = map.begin(); i != map.end(); ++i)
{
out << i->first << L" -> " << i->second;
if (std::next(i) == map.end())
{
out << L'}';
}
else
{
out << L',';
if (newLine)
{
out << L'\n';
}
}
}
}
}
template<typename T>
static void printMapInfo(std::wostream & out, const T & map, const bool show_collisions = false)
{
double mean = 0;
double variance = 0;
const unsigned int count = map.bucket_count();
unsigned int empty_bucket_count = 0;
unsigned int collision_count = 0;
out << L"Map size: " << map.size() << std::endl;
out << L"Number of buckets: " << count << std::endl;
for (unsigned int i = 0; i < count; ++i)
{
if (const unsigned int s = map.bucket_size(i))
{
mean += (double)s;
if (s > 1)
{
++collision_count;
}
}
else
{
++empty_bucket_count;
}
}
mean /= (double)count;
for (unsigned int i = 0; i < count; ++i)
{
const double ms = mean - (double)map.bucket_size(i);
variance += ms * ms;
}
variance /= (double)count;
out << L"Number of elements by buckets: mean=" << mean << L", sigma=" << std::sqrt(variance) << std::endl;
out << L"Number of empty buckets: " << empty_bucket_count << std::endl;
out << L"Number of collisions: " << collision_count << std::endl;
if (show_collisions)
{
std::multimap<unsigned int, typename T::key_type> collisions;
for (const auto & p : map)
{
collisions.emplace(map.bucket(p.first), p.first);
}
for (const auto & p : collisions)
{
out << L"Bucket " << p.first << L": " << p.second << L", hash=" << (typename T::hasher()(p.second)) << std::endl;
}
}
}
inline static std::size_t hash_combine(const std::size_t seed)
{
return seed;
}
template<typename... Args>
inline static std::size_t hash_combine(const std::size_t seed, Args... args)
{
// it is the way Boost has implemented hash_combine:
// http://www.boost.org/doc/libs/1_35_0/doc/html/boost/hash_combine_id241013.html
return seed ^ (hash_combine(args...) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
struct HashSymbol
{
inline std::size_t operator()(const symbol::Symbol & sym) const
{
return std::hash<std::wstring>()(sym.getName());
}
};
struct EqSymbol
{
inline bool operator()(const symbol::Symbol & L, const symbol::Symbol & R) const
{
return L == R;
}
};
typedef std::set<symbol::Symbol> SymbolOrdSet;
typedef std::unordered_set<symbol::Symbol, HashSymbol> SymbolSet;
template<typename T>
using SymbolMap = std::unordered_map<symbol::Symbol, T, HashSymbol, EqSymbol>;
} // namespace tools
} // namespace analysis
#endif // __TOOLS_HXX__
|