TPIE

11a2c2d
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 #include <tpie/tpie_export.h>
27 #include <tpie/config.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 
63  const double coefficient;
64  const double overhead;
65  constexpr linear_memory_usage(const double coefficient, const double overhead) noexcept :
66  coefficient(coefficient), overhead(overhead) {}
67 
68  constexpr memory_size_type operator()(memory_size_type size) const noexcept {
69  return static_cast<memory_size_type>(
70  static_cast<double>(size) * coefficient + overhead);
71  }
72 
73  constexpr friend linear_memory_usage operator * (const linear_memory_usage & o, double scale) noexcept {
74  return linear_memory_usage(o.coefficient * scale, o.overhead * scale);
75  }
76 
77  constexpr friend linear_memory_usage operator + (const linear_memory_usage & o, double value) noexcept {
78  return linear_memory_usage(o.coefficient, o.overhead + value);
79  }
80 
81  constexpr friend linear_memory_usage operator + (const linear_memory_usage & l, const linear_memory_usage & r) noexcept {
82  return linear_memory_usage(l.coefficient + r.coefficient, l.overhead * r.overhead);
83  }
84 };
85 
86 
97 template <typename child_t>
99 
106  static constexpr memory_size_type memory_usage(memory_size_type size) noexcept {
107  return static_cast<memory_size_type>(
108  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
109  }
110 
118  static constexpr memory_size_type memory_fits(memory_size_type memory) noexcept {
119  return static_cast<memory_size_type>(
120  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
121  }
122 
123 
124  static constexpr linear_memory_usage memory_usage() noexcept {
125  return linear_memory_usage(child_t::memory_coefficient(), child_t::memory_overhead());
126  }
127 };
128 
133 template <int t>
134 struct template_log {
135  static const size_t v=1+template_log< t/2 >::v;
136 };
137 
138 template <>
139 struct template_log<1> {
140  static const size_t v=1;
141 };
142 
143 
148 template <typename T, typename C>
149 void pop_and_push_heap(T a, T b, C lt) {
150  size_t i=0;
151  size_t n=(b-a);
152  while (true) {
153  size_t c=2*i+1;
154  if (c+1 >= n) {
155  if (c < n && lt(*(a+i), *(a+c)))
156  std::swap(*(a+c), *(a+i));
157  break;
158  }
159  if (lt(*(a+c+1), *(a+c))) {
160  if (lt(*(a+i), *(a+c))) {
161  std::swap(*(a+c), *(a+i));
162  i=c;
163  } else break;
164  } else {
165  if (lt(*(a+i), *(a+c+1))) {
166  std::swap(*(a+c+1), *(a+i));
167  i=c+1;
168  } else break;
169  }
170  }
171 }
172 
177 template <typename T>
178 void pop_and_push_heap(T a, T b) {
179  pop_and_push_heap(a,b, std::less<typename T::value_type>());
180 }
181 
182 
186 template <typename T>
188  T i;
189  binary_argument_swap(const T & _=T()): i(_) {}
190  template<typename T1, typename T2>
191  bool operator()(const T1 & x, const T2 & y) const {
192  return i(y,x);
193  }
194  template<typename T1, typename T2>
195  bool operator()(const T1 & x, const T2 & y) {
196  return i(y,x);
197  }
198 };
199 
203 TPIE_EXPORT void atomic_rename(const std::string & src, const std::string & dst);
204 
205 
209 TPIE_EXPORT std::string pretty_print_size(stream_size_type size);
210 
216 template <typename T>
217 inline void free_structure_memory(T & v) {
218  T t;
219  std::swap(v, t);
220 }
221 
222 #ifdef _WIN32
223 TPIE_EXPORT void throw_getlasterror();
224 #endif
225 
226 } //namespace tpie
227 
228 #endif //__TPIE_UTIL_H__
tpie::linear_memory_base
Base class of data structures with linear memory usage.
Definition: util.h:98
types.h
tpie::linear_memory_usage
Definition: util.h:62
tpie::free_structure_memory
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:217
tpie::unsign
Definition: util.h:50
tpie::sign
Definition: util.h:44
tpie::atomic_rename
TPIE_EXPORT void atomic_rename(const std::string &src, const std::string &dst)
atomicly rename a src to dst
tpie::unused
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
tpie::pretty_print_size
TPIE_EXPORT std::string pretty_print_size(stream_size_type size)
pretty print a size
tpie::linear_memory_base::memory_fits
static constexpr memory_size_type memory_fits(memory_size_type memory) noexcept
Return the maximum number of elements that can be contained in in the structure when it is allowed to...
Definition: util.h:118
tpie::binary_argument_swap
A binary functor with the arguments swapped.
Definition: util.h:187
tpie::template_log
Computes the least integer strictly greater than log(t).
Definition: util.h:134
tpie::linear_memory_base::memory_usage
static constexpr memory_size_type memory_usage(memory_size_type size) noexcept
Return the number of bytes required to create a data structure supporting a given number of elements.
Definition: util.h:106
tpie::pop_and_push_heap
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:149
tpie
Definition: access_type.h:26