TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
lazy_bracket.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2017-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2023 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./tuple_tools.hpp"
28
29#include <tuple>
30#include <type_traits>
31#include <utility>
32
33namespace triqs::utility {
34
35 // Implementation details for make_lazy_bracket.
36 namespace details {
37
38 template <int r, typename Lambda, typename Tu> struct _bra;
39
40 template <int r, typename F, typename Tu> _bra<r, F, Tu> make_bra(F &&f, Tu &&tu) { return {std::forward<F>(f), std::forward<Tu>(tu)}; }
41
42 template <int r, typename Lambda, typename Tu> struct _bra {
43 static_assert(r > 1, "Internal error in calling make_lazy_bracket : rank is incorrect");
44 Lambda f;
45 Tu tu;
46 template <typename U> decltype(auto) operator[](U const &u) { return make_bra<r - 1>(f, std::tuple_cat(tu, std::tie(u))); }
47 template <typename T> void operator=(T &&) = delete;
48 _bra &operator=(_bra const &) = delete;
49 };
50
51 template <typename Lambda, typename Tu> struct _bra<1, Lambda, Tu> {
52 Lambda f;
53 Tu tu;
54 template <typename U> decltype(auto) operator[](U const &u) { return triqs::tuple::apply(f, std::tuple_cat(tu, std::tie(u))); }
55 template <typename T> void operator=(T &&) = delete;
56 _bra &operator=(_bra const &) = delete;
57 };
58
59 template <int NArgs, typename Lambda, typename T> decltype(auto) _make_lazy_bracket(Lambda &&f, T const &x, std::false_type) {
60 return details::make_bra<NArgs - 1>(std::forward<Lambda>(f), std::tie(x));
61 }
62
63 template <int NArgs, typename Lambda, typename T> decltype(auto) _make_lazy_bracket(Lambda &&f, T const &x, std::true_type) {
64 return std::forward<Lambda>(f)(x);
65 }
66
67 } // namespace details
68
82 template <int NArgs, typename Lambda, typename T> decltype(auto) make_lazy_bracket(Lambda &&f, T const &x) {
83 return details::_make_lazy_bracket<NArgs>(std::forward<Lambda>(f), x, std::integral_constant<bool, (NArgs == 1)>{});
84 }
85
86} // namespace triqs::utility
decltype(auto) make_lazy_bracket(Lambda &&f, T const &x)
Invoke a callable lazily by accumulating arguments through chained operator[] calls.
decltype(auto) apply(F &&f, T &&t)
Call a function with the elements of a tuple as its arguments.
Generic tuple manipulation tools.