1
/*
2
    RawSpeed - RAW file decoder.
3

4
    Copyright (C) 2018 Stefan Löffler
5
    Copyright (C) 2018 Roman Lebedev
6

7
    This library is free software; you can redistribute it and/or
8
    modify it under the terms of the GNU Lesser General Public
9
    License as published by the Free Software Foundation; either
10
    version 2 of the License, or (at your option) any later version.
11

12
    This library is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
    Lesser General Public License for more details.
16

17
    You should have received a copy of the GNU Lesser General Public
18
    License along with this library; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21

22
#pragma once
23

24
#include "common/Common.h" // for clampBits
25
#include <algorithm>       // for generate_n
26
#include <cassert>         // for assert
27
#include <functional>      // for function
28
#include <iterator>        // for back_inserter
29
#include <type_traits>     // for enable_if, is_convertible
30
#include <vector>          // for vector
31

32
namespace rawspeed {
33

34
template <typename T, int TableBitWidth> class SimpleLUT final {
35
public:
36
  using value_type = T;
37

38 1
  SimpleLUT() = default;
39

40
private:
41
  std::vector<value_type> table;
42

43
public:
44
  template <typename F,
45
            typename = std::enable_if<std::is_convertible<
46
                F, std::function<value_type(
47
                       typename decltype(table)::size_type,
48
                       typename decltype(table)::size_type)>>::value>>
49 1
  explicit SimpleLUT(F&& f) {
50 1
    const auto fullTableSize = 1U << TableBitWidth;
51 1
    table.reserve(fullTableSize);
52 1
    std::generate_n(std::back_inserter(table), fullTableSize,
53 1
                    [&f, table = &table]() {
54
                      // which row [0..fullTableSize) are we filling?
55 1
                      const auto i = table->size();
56 1
                      return f(i, fullTableSize);
57
                    });
58 0
    assert(table.size() == fullTableSize);
59
  }
60

61 1
  inline value_type operator[](int x) const {
62 1
    unsigned clampedX = clampBits(x, TableBitWidth);
63 1
    return table[clampedX];
64
  }
65
};
66

67
} // namespace rawspeed

Read our documentation on viewing source code .

Loading