Nix  2.93.0-dev
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
pos-table.hh
Go to the documentation of this file.
1#pragma once
3
4#include <cinttypes>
5#include <numeric>
6#include <vector>
7
11#include "lix/libutil/sync.hh"
12
13namespace nix {
14
16{
17public:
18 class Origin
19 {
20 friend PosTable;
21 private:
22 uint32_t offset;
23
24 Origin(Pos::Origin origin, uint32_t offset, size_t size):
25 offset(offset), origin(origin), size(size)
26 {}
27
28 public:
29 const Pos::Origin origin;
30 const size_t size;
31
32 uint32_t offsetOf(PosIdx p) const
33 {
34 return p.id - 1 - offset;
35 }
36 };
37
38private:
39 using Lines = std::vector<uint32_t>;
40
41 std::map<uint32_t, Origin> origins;
42 mutable Sync<std::map<uint32_t, Lines>> lines;
43
44 const Origin * resolve(PosIdx p) const
45 {
46 if (p.id == 0)
47 return nullptr;
48
49 const auto idx = p.id - 1;
50 /* we want the last key <= idx, so we'll take prev(first key > idx).
51 this is guaranteed to never rewind origin.begin because the first
52 key is always 0. */
53 const auto pastOrigin = origins.upper_bound(idx);
54 return &std::prev(pastOrigin)->second;
55 }
56
57public:
58 Origin addOrigin(Pos::Origin origin, size_t size)
59 {
60 uint32_t offset = 0;
61 if (auto it = origins.rbegin(); it != origins.rend())
62 offset = it->first + it->second.size;
63 // +1 because all PosIdx are offset by 1 to begin with (because noPos == 0), and
64 // another +1 to ensure that all origins can point to EOF, eg on (invalid) empty inputs.
65 if (2 + offset + size < offset)
66 return Origin{origin, offset, 0};
67 return origins.emplace(offset, Origin{origin, offset, size}).first->second;
68 }
69
70 PosIdx add(const Origin & origin, size_t offset)
71 {
72 if (offset > origin.size)
73 return PosIdx();
74 return PosIdx(1 + origin.offset + offset);
75 }
76
77 Pos operator[](PosIdx p) const;
78
79 Pos::Origin originOf(PosIdx p) const
80 {
81 if (auto o = resolve(p))
82 return o->origin;
83 return std::monostate{};
84 }
85};
86
87}
Definition pos-idx.hh:9
Definition pos-table.hh:19
Definition pos-table.hh:16
Definition sync.hh:37
Pos and AbstractPos.