| 1 | /*
 | 
| 2 |  * Souffle - A Datalog Compiler
 | 
| 3 |  * Copyright (c) 2022, The Souffle Developers. All rights reserved
 | 
| 4 |  * Licensed under the Universal Permissive License v 1.0 as shown at:
 | 
| 5 |  * - https://opensource.org/licenses/UPL
 | 
| 6 |  * - <souffle root>/licenses/SOUFFLE-UPL.txt
 | 
| 7 |  */
 | 
| 8 | 
 | 
| 9 | /************************************************************************
 | 
| 10 |  *
 | 
| 11 |  * @file Info.h
 | 
| 12 |  *
 | 
| 13 |  * Datastructure for Info relations
 | 
| 14 |  *
 | 
| 15 |  ***********************************************************************/
 | 
| 16 | 
 | 
| 17 | #pragma once
 | 
| 18 | 
 | 
| 19 | #include "souffle/RamTypes.h"
 | 
| 20 | #include "souffle/SouffleInterface.h"
 | 
| 21 | #include "souffle/utility/ParallelUtil.h"
 | 
| 22 | 
 | 
| 23 | namespace souffle {
 | 
| 24 | 
 | 
| 25 | /** Info relations */
 | 
| 26 | template <Relation::arity_type Arity_>
 | 
| 27 | class t_info {
 | 
| 28 | public:
 | 
| 29 |     static constexpr Relation::arity_type Arity = Arity_;
 | 
| 30 | 
 | 
| 31 |     t_info() = default;
 | 
| 32 |     using t_tuple = Tuple<RamDomain, Arity>;
 | 
| 33 |     struct context {};
 | 
| 34 |     context createContext() {
 | 
| 35 |         return context();
 | 
| 36 |     }
 | 
| 37 |     class iterator : public std::iterator<std::forward_iterator_tag, Tuple<RamDomain, Arity>> {
 | 
| 38 |         typename std::vector<Tuple<RamDomain, Arity>>::const_iterator it;
 | 
| 39 | 
 | 
| 40 |     public:
 | 
| 41 |         iterator(const typename std::vector<t_tuple>::const_iterator& o) : it(o) {}
 | 
| 42 | 
 | 
| 43 |         const t_tuple operator*() {
 | 
| 44 |             return *it;
 | 
| 45 |         }
 | 
| 46 | 
 | 
| 47 |         bool operator==(const iterator& other) const {
 | 
| 48 |             return other.it == it;
 | 
| 49 |         }
 | 
| 50 | 
 | 
| 51 |         bool operator!=(const iterator& other) const {
 | 
| 52 |             return !(*this == other);
 | 
| 53 |         }
 | 
| 54 | 
 | 
| 55 |         iterator& operator++() {
 | 
| 56 |             it++;
 | 
| 57 |             return *this;
 | 
| 58 |         }
 | 
| 59 |     };
 | 
| 60 |     iterator begin() const {
 | 
| 61 |         return iterator(data.begin());
 | 
| 62 |     }
 | 
| 63 |     iterator end() const {
 | 
| 64 |         return iterator(data.end());
 | 
| 65 |     }
 | 
| 66 |     void insert(const t_tuple& t) {
 | 
| 67 |         insert_lock.lock();
 | 
| 68 |         if (!contains(t)) {
 | 
| 69 |             data.push_back(t);
 | 
| 70 |         }
 | 
| 71 |         insert_lock.unlock();
 | 
| 72 |     }
 | 
| 73 |     void insert(const t_tuple& t, context& /* ctxt */) {
 | 
| 74 |         insert(t);
 | 
| 75 |     }
 | 
| 76 |     void insert(const RamDomain* ramDomain) {
 | 
| 77 |         insert_lock.lock();
 | 
| 78 |         t_tuple t;
 | 
| 79 |         for (std::size_t i = 0; i < Arity; ++i) {
 | 
| 80 |             t.data[i] = ramDomain[i];
 | 
| 81 |         }
 | 
| 82 |         data.push_back(t);
 | 
| 83 |         insert_lock.unlock();
 | 
| 84 |     }
 | 
| 85 |     bool contains(const t_tuple& t) const {
 | 
| 86 |         for (const auto& o : data) {
 | 
| 87 |             if (t == o) {
 | 
| 88 |                 return true;
 | 
| 89 |             }
 | 
| 90 |         }
 | 
| 91 |         return false;
 | 
| 92 |     }
 | 
| 93 |     bool contains(const t_tuple& t, context& /* ctxt */) const {
 | 
| 94 |         return contains(t);
 | 
| 95 |     }
 | 
| 96 |     std::size_t size() const {
 | 
| 97 |         return data.size();
 | 
| 98 |     }
 | 
| 99 |     bool empty() const {
 | 
| 100 |         return data.size() == 0;
 | 
| 101 |     }
 | 
| 102 |     void purge() {
 | 
| 103 |         data.clear();
 | 
| 104 |     }
 | 
| 105 |     void printStatistics(std::ostream& /* o */) const {}
 | 
| 106 | 
 | 
| 107 | private:
 | 
| 108 |     std::vector<Tuple<RamDomain, Arity>> data;
 | 
| 109 |     Lock insert_lock;
 | 
| 110 | };
 | 
| 111 | 
 | 
| 112 | }  // namespace souffle
 |