souffle  2.0.2-371-g6315b36
Table.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. 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 Table.h
12  *
13  * An implementation of a generic Table storing a position-fixed collection
14  * of objects in main memory.
15  *
16  ***********************************************************************/
17 
18 #pragma once
19 
20 #include <iosfwd>
21 #include <iterator>
22 
23 namespace souffle {
24 
25 template <typename T, unsigned blockSize = 4096>
26 class Table {
27  struct Block {
28  Block* next;
29  std::size_t used = 0;
30  T data[blockSize];
31 
32  Block() : next(nullptr) {}
33 
34  bool isFull() const {
35  return used == blockSize;
36  }
37 
38  const T& append(const T& element) {
39  const T& res = data[used];
40  data[used] = element;
41  used++;
42  return res;
43  }
44  };
45 
46  Block* head;
47  Block* tail;
48 
49  std::size_t count = 0;
50 
51 public:
52  class iterator : public std::iterator<std::forward_iterator_tag, T> {
54  unsigned pos;
55 
56  public:
57  iterator(Block* block = nullptr, unsigned pos = 0) : block(block), pos(pos) {}
58 
59  iterator(const iterator&) = default;
60  iterator(iterator&&) = default;
61  iterator& operator=(const iterator&) = default;
62 
63  // the equality operator as required by the iterator concept
64  bool operator==(const iterator& other) const {
65  return (block == nullptr && other.block == nullptr) || (block == other.block && pos == other.pos);
66  }
67 
68  // the not-equality operator as required by the iterator concept
69  bool operator!=(const iterator& other) const {
70  return !(*this == other);
71  }
72 
73  // the deref operator as required by the iterator concept
74  const T& operator*() const {
75  return block->data[pos];
76  }
77 
78  // the increment operator as required by the iterator concept
79  iterator& operator++() {
80  // move on in block
81  if (++pos < block->used) {
82  return *this;
83  }
84  // or to next block
85  block = block->next;
86  pos = 0;
87  return *this;
88  }
89  };
90 
91  Table() : head(nullptr), tail(nullptr) {}
92 
93  ~Table() {
94  clear();
95  }
96 
97  bool empty() const {
98  return (!head);
99  }
100 
101  std::size_t size() const {
102  return count;
103  }
104 
105  const T& insert(const T& element) {
106  // check whether the head is initialized
107  if (!head) {
108  head = new Block();
109  tail = head;
110  }
111 
112  // check whether tail is full
113  if (tail->isFull()) {
114  tail->next = new Block();
116  }
117 
118  // increment counter
119  count++;
120 
121  // add another element
122  return tail->append(element);
123  }
124 
125  iterator begin() const {
126  return iterator(head);
127  }
128 
129  iterator end() const {
130  return iterator();
131  }
132 
133  void clear() {
134  while (head != nullptr) {
135  auto cur = head;
136  head = head->next;
137  delete cur;
138  }
139  count = 0;
140  head = nullptr;
141  tail = nullptr;
142  }
143 };
144 
145 } // end namespace souffle
souffle::Table::size
std::size_t size() const
Definition: Table.h:115
souffle::Table::begin
iterator begin() const
Definition: Table.h:139
souffle::Table::Block::Block
Block()
Definition: Table.h:53
souffle::Table::Block
Definition: Table.h:41
souffle::Table::Block::append
const T & append(const T &element)
Definition: Table.h:59
souffle::Table::iterator::operator==
bool operator==(const iterator &other) const
Definition: Table.h:78
souffle::Table::iterator::operator*
const T & operator*() const
Definition: Table.h:88
souffle::Table::Block::used
std::size_t used
Definition: Table.h:50
souffle::Table::iterator::block
Block * block
Definition: Table.h:67
souffle::Table::iterator::operator=
iterator & operator=(const iterator &)=default
souffle::Table::Block::data
T data[blockSize]
Definition: Table.h:51
souffle::Table::iterator::operator!=
bool operator!=(const iterator &other) const
Definition: Table.h:83
souffle::Table::head
Block * head
Definition: Table.h:60
souffle::Table::iterator::operator++
iterator & operator++()
Definition: Table.h:93
souffle::Table::iterator::iterator
iterator(Block *block=nullptr, unsigned pos=0)
Definition: Table.h:71
souffle::Table::Block::next
Block * next
Definition: Table.h:49
souffle::Table::empty
bool empty() const
Definition: Table.h:111
souffle::Table::count
std::size_t count
Definition: Table.h:63
souffle::Table::tail
Block * tail
Definition: Table.h:61
souffle::Table::iterator
Definition: Table.h:66
souffle::Table::insert
const T & insert(const T &element)
Definition: Table.h:119
souffle
Definition: AggregateOp.h:25
souffle::Table::Table
Table()
Definition: Table.h:105
souffle::Table::end
iterator end() const
Definition: Table.h:143
souffle::Table::clear
void clear()
Definition: Table.h:147
souffle::Table::iterator::pos
unsigned pos
Definition: Table.h:68
souffle::Table::Block::isFull
bool isFull() const
Definition: Table.h:55
souffle::Table::~Table
~Table()
Definition: Table.h:107