souffle  2.0.2-371-g6315b36
EliminateDuplicates.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, 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 EliminateDuplicates.cpp
12  *
13  ***********************************************************************/
14 
16 #include "ram/Condition.h"
17 #include "ram/Node.h"
18 #include "ram/Operation.h"
19 #include "ram/Program.h"
20 #include "ram/Statement.h"
21 #include "ram/utility/Utils.h"
22 #include "ram/utility/Visitor.h"
24 #include <cstddef>
25 #include <functional>
26 #include <memory>
27 #include <vector>
28 
29 namespace souffle::ram::transform {
30 
32  bool changed = false;
33  visitDepthFirst(program, [&](const Query& query) {
34  std::function<Own<Node>(Own<Node>)> filterRewriter = [&](Own<Node> node) -> Own<Node> {
35  if (const Filter* filter = dynamic_cast<Filter*>(node.get())) {
36  const Condition* condition = &filter->getCondition();
37  VecOwn<Condition> conds = toConjunctionList(condition);
38  bool eliminatedDuplicate = false;
39  for (std::size_t i = 0; i < conds.size(); i++) {
40  for (std::size_t j = i + 1; j < conds.size(); j++) {
41  if (*conds[i] == *conds[j]) {
42  conds.erase(conds.begin() + j);
43  i = -1;
44  eliminatedDuplicate = true;
45  break;
46  }
47  }
48  }
49  if (eliminatedDuplicate) {
50  changed = true;
51  node = mk<Filter>(
52  Own<Condition>(toCondition(conds)), souffle::clone(&filter->getOperation()));
53  }
54  }
55  node->apply(makeLambdaRamMapper(filterRewriter));
56  return node;
57  };
58  const_cast<Query*>(&query)->apply(makeLambdaRamMapper(filterRewriter));
59  });
60  return changed;
61 }
62 
63 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::Query
A relational algebra query.
Definition: Query.h:50
souffle::ram::toCondition
Own< Condition > toCondition(const VecOwn< Condition > &conds)
Convert list of conditions to a conjunction.
Definition: Utils.h:84
souffle::ram::toConjunctionList
VecOwn< Condition > toConjunctionList(const Condition *condition)
Convert terms of a conjunction to a list.
Definition: Utils.h:57
souffle::ram::transform::Transformer::apply
bool apply(TranslationUnit &translationUnit)
@Brief apply the transformer to a translation unit @Param translationUnit that will be transformed.
Definition: Transformer.cpp:39
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
MiscUtil.h
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
j
var j
Definition: htmlJsChartistMin.h:15
Program.h
souffle::ram::Filter
Checks whether a given condition holds.
Definition: Filter.h:49
Visitor.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
i
size_t i
Definition: json11.h:663
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
Condition.h
Utils.h
EliminateDuplicates.h
Node.h
Operation.h
souffle::ram::transform::EliminateDuplicatesTransformer::eliminateDuplicates
bool eliminateDuplicates(Program &program)
Eliminate duplicated conjunctive terms.
Definition: EliminateDuplicates.cpp:35
Statement.h
souffle::ram::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the RAM fragments rooted by the given node recursively i...
Definition: Visitor.h:357
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67