Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb_re2::PrefilterTree Class Reference

Classes

struct  Entry

Public Member Functions

 PrefilterTree (int min_atom_len)
void Add (Prefilter *prefilter)
void Compile (std::vector< std::string > *atom_vec)
void RegexpsGivenStrings (const std::vector< int > &matched_atoms, std::vector< int > *regexps) const
void PrintPrefilter (int regexpid)

Private Types

typedef SparseArray< int > IntMap
typedef std::map< std::string, Prefilter * > NodeMap

Private Member Functions

bool KeepNode (Prefilter *node) const
void AssignUniqueIds (NodeMap *nodes, std::vector< std::string > *atom_vec)
void PropagateMatch (const std::vector< int > &atom_ids, IntMap *regexps) const
Prefilter * CanonicalNode (NodeMap *nodes, Prefilter *node)
std::string NodeString (Prefilter *node) const
std::string DebugNodeString (Prefilter *node) const
void PrintDebugInfo (NodeMap *nodes)
 PrefilterTree (const PrefilterTree &)=delete
PrefilterTree & operator= (const PrefilterTree &)=delete

Private Attributes

std::vector< Entry > entries_
std::vector< int > unfiltered_
std::vector< Prefilter * > prefilter_vec_
std::vector< int > atom_index_to_id_
bool compiled_
const int min_atom_len_

Constructor & Destructor Documentation

◆ PrefilterTree() [1/2]

duckdb_re2::PrefilterTree::PrefilterTree ( )
470011 : compiled_(false),
470012 min_atom_len_(3) {
470013}

◆ PrefilterTree() [2/2]

duckdb_re2::PrefilterTree::PrefilterTree ( int min_atom_len)
explicit
470016 : compiled_(false),
470017 min_atom_len_(min_atom_len) {
470018}

◆ ~PrefilterTree()

duckdb_re2::PrefilterTree::~PrefilterTree ( )
470020 {
470021 for (size_t i = 0; i < prefilter_vec_.size(); i++)
470022 delete prefilter_vec_[i];
470023}

Member Function Documentation

◆ Add()

void duckdb_re2::PrefilterTree::Add ( Prefilter * prefilter)
470025 {
470026 if (compiled_) {
470027 LOG(DFATAL) << "Add called after Compile.";
470028 return;
470029 }
470030 if (prefilter != NULL && !KeepNode(prefilter)) {
470031 delete prefilter;
470032 prefilter = NULL;
470033 }
470034
470035 prefilter_vec_.push_back(prefilter);
470036}

◆ Compile()

void duckdb_re2::PrefilterTree::Compile ( std::vector< std::string > * atom_vec)
470038 {
470039 if (compiled_) {
470040 LOG(DFATAL) << "Compile called already.";
470041 return;
470042 }
470043
470044 // Some legacy users of PrefilterTree call Compile() before
470045 // adding any regexps and expect Compile() to have no effect.
470046 if (prefilter_vec_.empty())
470047 return;
470048
470049 compiled_ = true;
470050
470051 NodeMap nodes;
470052 AssignUniqueIds(&nodes, atom_vec);
470053}

◆ RegexpsGivenStrings()

void duckdb_re2::PrefilterTree::RegexpsGivenStrings ( const std::vector< int > & matched_atoms,
std::vector< int > * regexps ) const
470268 {
470269 regexps->clear();
470270 if (!compiled_) {
470271 // Some legacy users of PrefilterTree call Compile() before
470272 // adding any regexps and expect Compile() to have no effect.
470273 // This kludge is a counterpart to that kludge.
470274 if (prefilter_vec_.empty())
470275 return;
470276
470277 LOG(ERROR) << "RegexpsGivenStrings called before Compile.";
470278 for (size_t i = 0; i < prefilter_vec_.size(); i++)
470279 regexps->push_back(static_cast<int>(i));
470280 } else {
470281 IntMap regexps_map(static_cast<int>(prefilter_vec_.size()));
470282 std::vector<int> matched_atom_ids;
470283 for (size_t j = 0; j < matched_atoms.size(); j++)
470284 matched_atom_ids.push_back(atom_index_to_id_[matched_atoms[j]]);
470285 PropagateMatch(matched_atom_ids, &regexps_map);
470286 for (IntMap::iterator it = regexps_map.begin();
470287 it != regexps_map.end();
470288 ++it)
470289 regexps->push_back(it->index());
470290
470291 regexps->insert(regexps->end(), unfiltered_.begin(), unfiltered_.end());
470292 }
470293 std::sort(regexps->begin(), regexps->end());
470294}

◆ PrintPrefilter()

void duckdb_re2::PrefilterTree::PrintPrefilter ( int regexpid)
470330 {
470331 LOG(ERROR) << DebugNodeString(prefilter_vec_[regexpid]);
470332}

◆ KeepNode()

bool duckdb_re2::PrefilterTree::KeepNode ( Prefilter * node) const
private
470078 {
470079 if (node == NULL)
470080 return false;
470081
470082 switch (node->op()) {
470083 default:
470084 LOG(DFATAL) << "Unexpected op in KeepNode: " << node->op();
470085 return false;
470086
470087 case Prefilter::ALL:
470088 case Prefilter::NONE:
470089 return false;
470090
470091 case Prefilter::ATOM:
470092 return node->atom().size() >= static_cast<size_t>(min_atom_len_);
470093
470094 case Prefilter::AND: {
470095 int j = 0;
470096 std::vector<Prefilter*>* subs = node->subs();
470097 for (size_t i = 0; i < subs->size(); i++)
470098 if (KeepNode((*subs)[i]))
470099 (*subs)[j++] = (*subs)[i];
470100 else
470101 delete (*subs)[i];
470102
470103 subs->resize(j);
470104 return j > 0;
470105 }
470106
470107 case Prefilter::OR:
470108 for (size_t i = 0; i < node->subs()->size(); i++)
470109 if (!KeepNode((*node->subs())[i]))
470110 return false;
470111 return true;
470112 }
470113}
GOpaque< Size > size(const GMat &src)

◆ AssignUniqueIds()

void duckdb_re2::PrefilterTree::AssignUniqueIds ( NodeMap * nodes,
std::vector< std::string > * atom_vec )
private
470116 {
470117 atom_vec->clear();
470118
470119 // Build vector of all filter nodes, sorted topologically
470120 // from top to bottom in v.
470121 std::vector<Prefilter*> v;
470122
470123 // Add the top level nodes of each regexp prefilter.
470124 for (size_t i = 0; i < prefilter_vec_.size(); i++) {
470125 Prefilter* f = prefilter_vec_[i];
470126 if (f == NULL)
470127 unfiltered_.push_back(static_cast<int>(i));
470128
470129 // We push NULL also on to v, so that we maintain the
470130 // mapping of index==regexpid for level=0 prefilter nodes.
470131 v.push_back(f);
470132 }
470133
470134 // Now add all the descendant nodes.
470135 for (size_t i = 0; i < v.size(); i++) {
470136 Prefilter* f = v[i];
470137 if (f == NULL)
470138 continue;
470139 if (f->op() == Prefilter::AND || f->op() == Prefilter::OR) {
470140 const std::vector<Prefilter*>& subs = *f->subs();
470141 for (size_t j = 0; j < subs.size(); j++)
470142 v.push_back(subs[j]);
470143 }
470144 }
470145
470146 // Identify unique nodes.
470147 int unique_id = 0;
470148 for (int i = static_cast<int>(v.size()) - 1; i >= 0; i--) {
470149 Prefilter *node = v[i];
470150 if (node == NULL)
470151 continue;
470152 node->set_unique_id(-1);
470153 Prefilter* canonical = CanonicalNode(nodes, node);
470154 if (canonical == NULL) {
470155 // Any further nodes that have the same node string
470156 // will find this node as the canonical node.
470157 nodes->emplace(NodeString(node), node);
470158 if (node->op() == Prefilter::ATOM) {
470159 atom_vec->push_back(node->atom());
470160 atom_index_to_id_.push_back(unique_id);
470161 }
470162 node->set_unique_id(unique_id++);
470163 } else {
470164 node->set_unique_id(canonical->unique_id());
470165 }
470166 }
470167 entries_.resize(unique_id);
470168
470169 // Fill the entries.
470170 for (int i = static_cast<int>(v.size()) - 1; i >= 0; i--) {
470171 Prefilter* prefilter = v[i];
470172 if (prefilter == NULL)
470173 continue;
470174 if (CanonicalNode(nodes, prefilter) != prefilter)
470175 continue;
470176 int id = prefilter->unique_id();
470177 switch (prefilter->op()) {
470178 default:
470179 LOG(DFATAL) << "Unexpected op: " << prefilter->op();
470180 return;
470181
470182 case Prefilter::ATOM:
470183 entries_[id].propagate_up_at_count = 1;
470184 break;
470185
470186 case Prefilter::OR:
470187 case Prefilter::AND: {
470188 // For each child, we append our id to the child's list of
470189 // parent ids... unless we happen to have done so already.
470190 // The number of appends is the number of unique children,
470191 // which allows correct upward propagation from AND nodes.
470192 int up_count = 0;
470193 for (size_t j = 0; j < prefilter->subs()->size(); j++) {
470194 int child_id = (*prefilter->subs())[j]->unique_id();
470195 std::vector<int>& parents = entries_[child_id].parents;
470196 if (parents.empty() || parents.back() != id) {
470197 parents.push_back(id);
470198 up_count++;
470199 }
470200 }
470201 entries_[id].propagate_up_at_count =
470202 prefilter->op() == Prefilter::AND ? up_count : 1;
470203 break;
470204 }
470205 }
470206 }
470207
470208 // For top level nodes, populate regexp id.
470209 for (size_t i = 0; i < prefilter_vec_.size(); i++) {
470210 if (prefilter_vec_[i] == NULL)
470211 continue;
470212 int id = CanonicalNode(nodes, prefilter_vec_[i])->unique_id();
470213 DCHECK_LE(0, id);
470214 Entry* entry = &entries_[id];
470215 entry->regexps.push_back(static_cast<int>(i));
470216 }
470217
470218 // Lastly, using probability-based heuristics, we identify nodes
470219 // that trigger too many parents and then we try to prune edges.
470220 // We use logarithms below to avoid the likelihood of underflow.
470221 double log_num_regexps = std::log(prefilter_vec_.size() - unfiltered_.size());
470222 // Hoisted this above the loop so that we don't thrash the heap.
470223 std::vector<std::pair<size_t, int>> entries_by_num_edges;
470224 for (int i = static_cast<int>(v.size()) - 1; i >= 0; i--) {
470225 Prefilter* prefilter = v[i];
470226 // Pruning applies only to AND nodes because it "just" reduces
470227 // precision; applied to OR nodes, it would break correctness.
470228 if (prefilter == NULL || prefilter->op() != Prefilter::AND)
470229 continue;
470230 if (CanonicalNode(nodes, prefilter) != prefilter)
470231 continue;
470232 int id = prefilter->unique_id();
470233
470234 // Sort the current node's children by the numbers of parents.
470235 entries_by_num_edges.clear();
470236 for (size_t j = 0; j < prefilter->subs()->size(); j++) {
470237 int child_id = (*prefilter->subs())[j]->unique_id();
470238 const std::vector<int>& parents = entries_[child_id].parents;
470239 entries_by_num_edges.emplace_back(parents.size(), child_id);
470240 }
470241 std::stable_sort(entries_by_num_edges.begin(), entries_by_num_edges.end());
470242
470243 // A running estimate of how many regexps will be triggered by
470244 // pruning the remaining children's edges to the current node.
470245 // Our nominal target is one, so the threshold is log(1) == 0;
470246 // pruning occurs iff the child has more than nine edges left.
470247 double log_num_triggered = log_num_regexps;
470248 for (const auto& pair : entries_by_num_edges) {
470249 int child_id = pair.second;
470250 std::vector<int>& parents = entries_[child_id].parents;
470251 if (log_num_triggered > 0.) {
470252 log_num_triggered += std::log(parents.size());
470253 log_num_triggered -= log_num_regexps;
470254 } else if (parents.size() > 9) {
470255 auto it = std::find(parents.begin(), parents.end(), id);
470256 if (it != parents.end()) {
470257 parents.erase(it);
470258 entries_[id].propagate_up_at_count--;
470259 }
470260 }
470261 }
470262 }
470263}
uint32_t v
cv::String canonical(const cv::String &path)
Definition duckdb.cpp:464821

◆ PropagateMatch()

void duckdb_re2::PrefilterTree::PropagateMatch ( const std::vector< int > & atom_ids,
IntMap * regexps ) const
private
470297 {
470298 IntMap count(static_cast<int>(entries_.size()));
470299 IntMap work(static_cast<int>(entries_.size()));
470300 for (size_t i = 0; i < atom_ids.size(); i++)
470301 work.set(atom_ids[i], 1);
470302 for (IntMap::iterator it = work.begin(); it != work.end(); ++it) {
470303 const Entry& entry = entries_[it->index()];
470304 // Record regexps triggered.
470305 for (size_t i = 0; i < entry.regexps.size(); i++)
470306 regexps->set(entry.regexps[i], 1);
470307 int c;
470308 // Pass trigger up to parents.
470309 for (int j : entry.parents) {
470310 const Entry& parent = entries_[j];
470311 // Delay until all the children have succeeded.
470312 if (parent.propagate_up_at_count > 1) {
470313 if (count.has_index(j)) {
470314 c = count.get_existing(j) + 1;
470315 count.set_existing(j, c);
470316 } else {
470317 c = 1;
470318 count.set_new(j, c);
470319 }
470320 if (c < parent.propagate_up_at_count)
470321 continue;
470322 }
470323 // Trigger the parent.
470324 work.set(j, 1);
470325 }
470326 }
470327}

◆ CanonicalNode()

Prefilter * duckdb_re2::PrefilterTree::CanonicalNode ( NodeMap * nodes,
Prefilter * node )
private
470055 {
470056 std::string node_string = NodeString(node);
470057 NodeMap::iterator iter = nodes->find(node_string);
470058 if (iter == nodes->end())
470059 return NULL;
470060 return (*iter).second;
470061}

◆ NodeString()

std::string duckdb_re2::PrefilterTree::NodeString ( Prefilter * node) const
private
470063 {
470064 // Adding the operation disambiguates AND/OR/atom nodes.
470065 std::string s = StringPrintf("%d", node->op()) + ":";
470066 if (node->op() == Prefilter::ATOM) {
470067 s += node->atom();
470068 } else {
470069 for (size_t i = 0; i < node->subs()->size(); i++) {
470070 if (i > 0)
470071 s += ',';
470072 s += StringPrintf("%d", (*node->subs())[i]->unique_id());
470073 }
470074 }
470075 return s;
470076}

◆ DebugNodeString()

std::string duckdb_re2::PrefilterTree::DebugNodeString ( Prefilter * node) const
private
470353 {
470354 std::string node_string = "";
470355 if (node->op() == Prefilter::ATOM) {
470356 DCHECK(!node->atom().empty());
470357 node_string += node->atom();
470358 } else {
470359 // Adding the operation disambiguates AND and OR nodes.
470360 node_string += node->op() == Prefilter::AND ? "AND" : "OR";
470361 node_string += "(";
470362 for (size_t i = 0; i < node->subs()->size(); i++) {
470363 if (i > 0)
470364 node_string += ',';
470365 node_string += StringPrintf("%d", (*node->subs())[i]->unique_id());
470366 node_string += ":";
470367 node_string += DebugNodeString((*node->subs())[i]);
470368 }
470369 node_string += ")";
470370 }
470371 return node_string;
470372}

◆ PrintDebugInfo()

void duckdb_re2::PrefilterTree::PrintDebugInfo ( NodeMap * nodes)
private
470334 {
470335 LOG(ERROR) << "#Unique Atoms: " << atom_index_to_id_.size();
470336 LOG(ERROR) << "#Unique Nodes: " << entries_.size();
470337
470338 for (size_t i = 0; i < entries_.size(); i++) {
470339 const std::vector<int>& parents = entries_[i].parents;
470340 const std::vector<int>& regexps = entries_[i].regexps;
470341 LOG(ERROR) << "EntryId: " << i
470342 << " N: " << parents.size() << " R: " << regexps.size();
470343 for (int parent : parents)
470344 LOG(ERROR) << parent;
470345 }
470346 LOG(ERROR) << "Map:";
470347 for (NodeMap::const_iterator iter = nodes->begin();
470348 iter != nodes->end(); ++iter)
470349 LOG(ERROR) << "NodeId: " << (*iter).second->unique_id()
470350 << " Str: " << (*iter).first;
470351}

The documentation for this class was generated from the following file: