TPIE

2362a60
util.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2009, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef __TPIE_UTIL_H__
25 #define __TPIE_UTIL_H__
26 
27 #include <tpie/portability.h>
28 #include <tpie/types.h>
29 #include <cmath>
30 #include <string>
31 
32 namespace tpie {
33 
41 template <typename T>
42 inline void unused(const T & x) {(void)x;}
43 
44 template <typename T> struct sign {typedef T type;};
45 template <> struct sign<uint8_t> {typedef int8_t type;};
46 template <> struct sign<uint16_t> {typedef int16_t type;};
47 template <> struct sign<uint32_t> {typedef int32_t type;};
48 template <> struct sign<uint64_t> {typedef int64_t type;};
49 
50 template <typename T> struct unsign {typedef T type;};
51 template <> struct unsign<int8_t> {typedef uint8_t type;};
52 template <> struct unsign<int16_t> {typedef uint16_t type;};
53 template <> struct unsign<int32_t> {typedef uint32_t type;};
54 template <> struct unsign<int64_t> {typedef uint64_t type;};
55 
56 #ifdef _WIN32
57 const char directory_delimiter = '\\';
58 #else
59 const char directory_delimiter = '/';
60 #endif
61 
72 template <typename child_t>
74 
81  static memory_size_type memory_usage(memory_size_type size) {
82  return static_cast<memory_size_type>(
83  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
84  }
85 
93  inline static memory_size_type memory_fits(memory_size_type memory) {
94  return static_cast<memory_size_type>(
95  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
96  }
97 };
98 
103 template <int t>
104 struct template_log {
105  static const size_t v=1+template_log< t/2 >::v;
106 };
107 
108 template <>
109 struct template_log<1> {
110  static const size_t v=1;
111 };
112 
113 
118 template <typename T, typename C>
119 void pop_and_push_heap(T a, T b, C lt) {
120  size_t i=0;
121  size_t n=(b-a);
122  while (true) {
123  size_t c=2*i+1;
124  if (c+1 >= n) {
125  if (c < n && lt(*(a+i), *(a+c)))
126  std::swap(*(a+c), *(a+i));
127  break;
128  }
129  if (lt(*(a+c+1), *(a+c))) {
130  if (lt(*(a+i), *(a+c))) {
131  std::swap(*(a+c), *(a+i));
132  i=c;
133  } else break;
134  } else {
135  if (lt(*(a+i), *(a+c+1))) {
136  std::swap(*(a+c+1), *(a+i));
137  i=c+1;
138  } else break;
139  }
140  }
141 }
142 
147 template <typename T>
148 void pop_and_push_heap(T a, T b) {
149  pop_and_push_heap(a,b, std::less<typename T::value_type>());
150 }
151 
152 
156 template <typename T>
158  T i;
159  binary_argument_swap(const T & _=T()): i(_) {}
160  template<typename T1, typename T2>
161  bool operator()(const T1 & x, const T2 & y) const {
162  return i(y,x);
163  }
164  template<typename T1, typename T2>
165  bool operator()(const T1 & x, const T2 & y) {
166  return i(y,x);
167  }
168 };
169 
170 void atomic_rename(const std::string & src, const std::string & dst);
171 
172 
178 template <typename T>
179 inline void free_structure_memory(T & v) {
180  T t;
181  std::swap(v, t);
182 }
183 
184 #ifdef _WIN32
185 void throw_getlasterror();
186 #endif
187 
188 } //namespace tpie
189 
190 #endif //__TPIE_UTIL_H__
void pop_and_push_heap(T a, T b, C lt)
Restore heap invariants after the first element has been replaced by some other element.
Definition: util.h:119
Typesafe bitflags.
Base class of data structures with linear memory usage.
Definition: util.h:73
static memory_size_type memory_usage(memory_size_type size)
Return the number of bytes required to create a data structure supporting a given number of elements...
Definition: util.h:81
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
This file contains a few deprecated definitions for legacy code.
A binary functor with the arguments swapped.
Definition: util.h:157
static memory_size_type memory_fits(memory_size_type memory)
Return the maximum number of elements that can be contained in in the structure when it is allowed to...
Definition: util.h:93
Computes the least integer strictly greater than log(t).
Definition: util.h:104
void free_structure_memory(T &v)
Free the memory assosiated with a stl or tpie structure by swapping it with a default constructed str...
Definition: util.h:179