ALPSCore reference
slice.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 /* $Id: obsvalue.h 3435 2009-11-28 14:45:38Z troyer $ */
8 
9 #ifndef ALPS_TYPE_TRAITS_SLICE_HPP
10 #define ALPS_TYPE_TRAITS_SLICE_HPP
11 
14 
15 #include <type_traits>
16 
17 namespace alps {
18 
19 template <class T>
21 {
22  typedef std::size_t type;
23 };
24 
25 template <class T>
26 typename std::enable_if<is_sequence<T>::value,
27  std::pair<typename slice_index<T>::type,typename slice_index<T>::type >
28 >::type
29 slices (T const& x)
30 {
31  return std::pair<typename slice_index<T>::type,typename slice_index<T>::type >(0,x.size());
32 }
33 
34 template <class T>
35 typename std::enable_if<!is_sequence<T>::value,
36  std::pair<typename slice_index<T>::type,typename slice_index<T>::type >
37 >::type
38 slices (T const&)
39 {
40  return std::pair<typename slice_index<T>::type,typename slice_index<T>::type >(0,1);
41 }
42 
43 template <class ValueType>
44 typename std::enable_if<is_sequence<ValueType>::value, std::string>::type
45 slice_name(ValueType const& ,unsigned i)
46 {
47  return std::to_string(i);
48 }
49 
50 template <class ValueType>
51 typename std::enable_if<!is_sequence<ValueType>::value, std::string>::type
52 slice_name(ValueType const& ,unsigned)
53 {
54  return "";
55 }
56 
57 template <class ValueType>
58 typename std::enable_if<is_sequence<ValueType>::value,
60 slice_value(ValueType const& x ,unsigned i)
61 {
62  return i < x.size() ? x[i] : typename element_type<ValueType>::type();
63 }
64 
65 
66 template <class ValueType>
67 typename std::enable_if<!is_sequence<ValueType>::value, ValueType const&>::type
68 slice_value(ValueType const& x ,unsigned)
69 {
70  return x;
71 }
72 
73 template <class ValueType>
74 typename std::enable_if<is_sequence<ValueType>::value,
76 slice_value(ValueType& x,unsigned i)
77 {
78  return x[i];
79 }
80 
81 
82 template <class ValueType>
83 typename std::enable_if<!is_sequence<ValueType>::value, ValueType&>::type
84 slice_value(ValueType& x ,unsigned)
85 {
86  return x;
87 }
88 
89 template <class ValueType>
90 struct slice_it
91 {
93  typedef ValueType const& first_argument_type;
95 
96  result_type operator()(ValueType const& x, second_argument_type i) const
97  {
98  return slice_value(x,i);
99  }
100 };
101 
102 
103 } // end namespace alps
104 
105 #endif // ALPS_TYPE_TRAITS_ELEMENT_TYPE_H
element_type< ValueType >::type result_type
Definition: slice.hpp:92
slice_index< ValueType >::type second_argument_type
Definition: slice.hpp:94
std::enable_if< is_sequence< ValueType >::value, std::string >::type slice_name(ValueType const &, unsigned i)
Definition: slice.hpp:45
ValueType const & first_argument_type
Definition: slice.hpp:93
std::size_t type
Definition: slice.hpp:22
result_type operator()(ValueType const &x, second_argument_type i) const
Definition: slice.hpp:96
std::enable_if< is_sequence< T >::value, std::pair< typename slice_index< T >::type, typename slice_index< T >::type >>::type slices(T const &x)
Definition: slice.hpp:29
std::enable_if< is_sequence< ValueType >::value, typename element_type< ValueType >::type >::type slice_value(ValueType const &x, unsigned i)
Definition: slice.hpp:60