about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/attr-set.hh
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-21T18·20+0100
committerVincent Ambo <tazjin@google.com>2020-05-21T18·21+0100
commit28e347effe1ba4325fc485e920bda45c838e0450 (patch)
treede476a8bd0138bce979c14bab45c6f22714585a1 /third_party/nix/src/libexpr/attr-set.hh
parent1bb9cd7749748ee7019efcde834bbdb2b56e68e1 (diff)
refactor(3p/nix/libexpr): Use absl::btree_map for AttrSets r/799
This is the first step towards replacing the implementation of
attribute sets with an absl::btree_map.

Currently many access are done using array offsets and pointer
arithmetic, so this change is currently causing Nix to fail in various
ways.
Diffstat (limited to 'third_party/nix/src/libexpr/attr-set.hh')
-rw-r--r--third_party/nix/src/libexpr/attr-set.hh83
1 files changed, 34 insertions, 49 deletions
diff --git a/third_party/nix/src/libexpr/attr-set.hh b/third_party/nix/src/libexpr/attr-set.hh
index 5357e58a1dec..551cddaae4eb 100644
--- a/third_party/nix/src/libexpr/attr-set.hh
+++ b/third_party/nix/src/libexpr/attr-set.hh
@@ -1,12 +1,13 @@
+// This file implements the underlying structure of Nix attribute sets.
 #pragma once
 
-#include <algorithm>
+#include <absl/container/btree_map.h>
 
 #include "nixexpr.hh"
 #include "symbol-table.hh"
 #include "types.hh"  // TODO(tazjin): audit this include
 
-namespace nix {
+namespace nix {  // TODO(tazjin): ::expr
 
 class EvalState;
 struct Value;
@@ -22,65 +23,49 @@ struct Attr {
   bool operator<(const Attr& a) const { return name < a.name; }
 };
 
-/* Bindings contains all the attributes of an attribute set. It is defined
-   by its size and its capacity, the capacity being the number of Attr
-   elements allocated after this structure, while the size corresponds to
-   the number of elements already inserted in this structure. */
-class Bindings {
- public:
-  typedef uint32_t size_t;
-
- private:
-  size_t size_, capacity_;
-  Attr attrs[0];
-
-  Bindings(size_t capacity) : size_(0), capacity_(capacity) {}
-  Bindings(const Bindings& bindings) = delete;
+// TODO: remove this, it only exists briefly while I get rid of the
+// current Attr struct
+inline bool operator==(const Attr& lhs, const Attr& rhs) {
+  return lhs.name == rhs.name;
+}
 
+class Bindings {
  public:
-  size_t size() const { return size_; }
+  typedef Attr* iterator;  // TODO: type, and also 'using'?
 
-  bool empty() const { return !size_; }
+  // Return the number of contained elements.
+  size_t size();
 
-  typedef Attr* iterator;
+  // Is this attribute set empty?
+  bool empty();
 
-  void push_back(const Attr& attr) {
-    assert(size_ < capacity_);
-    attrs[size_++] = attr;
-  }
+  // TODO(tazjin): rename
+  // TODO(tazjin): does this need to copy?
+  void push_back(const Attr& attr);
 
-  iterator find(const Symbol& name) {
-    Attr key(name, 0);
-    iterator i = std::lower_bound(begin(), end(), key);
-    if (i != end() && i->name == name) {
-      return i;
-    }
-    return end();
-  }
+  // Look up a specific element of the attribute set.
+  iterator find(const Symbol& name);
 
-  iterator begin() { return &attrs[0]; }
-  iterator end() { return &attrs[size_]; }
+  // TODO
+  iterator begin();
+  iterator end();
 
-  Attr& operator[](size_t pos) { return attrs[pos]; }
+  // ???
+  [[deprecated]] void sort();
 
-  void sort();
+  // ???
+  [[deprecated]] size_t capacity();
 
-  size_t capacity() { return capacity_; }
+  // oh no
+  // Attr& operator[](size_t pos); //  { return attrs[pos]; }
 
-  /* Returns the attributes in lexicographically sorted order. */
-  std::vector<const Attr*> lexicographicOrder() const {
-    std::vector<const Attr*> res;
-    res.reserve(size_);
-    for (size_t n = 0; n < size_; n++) {
-      res.emplace_back(&attrs[n]);
-    }
-    std::sort(res.begin(), res.end(), [](const Attr* a, const Attr* b) {
-      return (const std::string&)a->name < (const std::string&)b->name;
-    });
-    return res;
-  }
+  // TODO: can callers just iterate?
+  [[deprecated]] std::vector<const Attr*> lexicographicOrder();
 
+  // oh no
   friend class EvalState;
-};
 
+ private:
+  absl::btree_map<Symbol, Attr> attributes_;
+};
 }  // namespace nix