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