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::NFA Class Reference
Collaboration diagram for duckdb_re2::NFA:

Classes

struct  Thread
struct  AddState

Public Member Functions

 NFA (Prog *prog)
bool Search (const StringPiece &text, const StringPiece &context, bool anchored, bool longest, StringPiece *submatch, int nsubmatch)

Private Types

typedef SparseArray< Thread * > Threadq

Private Member Functions

Thread * AllocThread ()
Thread * Incref (Thread *t)
void Decref (Thread *t)
void AddToThreadq (Threadq *q, int id0, int c, const StringPiece &context, const char *p, Thread *t0)
int Step (Threadq *runq, Threadq *nextq, int c, const StringPiece &context, const char *p)
std::string FormatCapture (const char **capture)
void CopyCapture (const char **dst, const char **src)
 NFA (const NFA &)=delete
NFA & operator= (const NFA &)=delete

Private Attributes

Prog * prog_
int start_
int ncapture_
bool longest_
bool endmatch_
const char * btext_
const char * etext_
Threadq q0_
Threadq q1_
PODArray< AddState > stack_
std::deque< Thread > arena_
Thread * freelist_
const char ** match_
bool matched_

Constructor & Destructor Documentation

◆ NFA()

duckdb_re2::NFA::NFA ( Prog * prog)
465366 {
465367 prog_ = prog;
465368 start_ = prog_->start();
465369 ncapture_ = 0;
465370 longest_ = false;
465371 endmatch_ = false;
465372 btext_ = NULL;
465373 etext_ = NULL;
465374 q0_.resize(prog_->size());
465375 q1_.resize(prog_->size());
465376 // See NFA::AddToThreadq() for why this is so.
465377 int nstack = 2*prog_->inst_count(kInstCapture) +
465378 prog_->inst_count(kInstEmptyWidth) +
465379 prog_->inst_count(kInstNop) + 1; // + 1 for start inst
465380 stack_ = PODArray<AddState>(nstack);
465381 freelist_ = NULL;
465382 match_ = NULL;
465383 matched_ = false;
465384}

◆ ~NFA()

duckdb_re2::NFA::~NFA ( )
465386 {
465387 delete[] match_;
465388 for (const Thread& t : arena_)
465389 delete[] t.capture;
465390}
Definition duckdb.cpp:465301

Member Function Documentation

◆ Search()

bool duckdb_re2::NFA::Search ( const StringPiece & text,
const StringPiece & context,
bool anchored,
bool longest,
StringPiece * submatch,
int nsubmatch )
465677 {
465678 if (start_ == 0)
465679 return false;
465680
465681 StringPiece context = const_context;
465682 if (context.data() == NULL)
465683 context = text;
465684
465685 // Sanity check: make sure that text lies within context.
465686 if (BeginPtr(text) < BeginPtr(context) || EndPtr(text) > EndPtr(context)) {
465687 LOG(DFATAL) << "context does not contain text";
465688 return false;
465689 }
465690
465691 if (prog_->anchor_start() && BeginPtr(context) != BeginPtr(text))
465692 return false;
465693 if (prog_->anchor_end() && EndPtr(context) != EndPtr(text))
465694 return false;
465695 anchored |= prog_->anchor_start();
465696 if (prog_->anchor_end()) {
465697 longest = true;
465698 endmatch_ = true;
465699 }
465700
465701 if (nsubmatch < 0) {
465702 LOG(DFATAL) << "Bad args: nsubmatch=" << nsubmatch;
465703 return false;
465704 }
465705
465706 // Save search parameters.
465707 ncapture_ = 2*nsubmatch;
465708 longest_ = longest;
465709
465710 if (nsubmatch == 0) {
465711 // We need to maintain match[0], both to distinguish the
465712 // longest match (if longest is true) and also to tell
465713 // whether we've seen any matches at all.
465714 ncapture_ = 2;
465715 }
465716
465717 match_ = new const char*[ncapture_];
465718 memset(match_, 0, ncapture_*sizeof match_[0]);
465719 matched_ = false;
465720
465721 // For debugging prints.
465722 btext_ = context.data();
465723 // For convenience.
465724 etext_ = text.data() + text.size();
465725
465726 // Set up search.
465727 Threadq* runq = &q0_;
465728 Threadq* nextq = &q1_;
465729 runq->clear();
465730 nextq->clear();
465731
465732 // Loop over the text, stepping the machine.
465733 for (const char* p = text.data();; p++) {
465734 // This is a no-op the first time around the loop because runq is empty.
465735 int id = Step(runq, nextq, p < etext_ ? p[0] & 0xFF : -1, context, p);
465736 DCHECK_EQ(runq->size(), 0);
465737 using std::swap;
465738 swap(nextq, runq);
465739 nextq->clear();
465740 if (id != 0) {
465741 // We're done: full match ahead.
465742 p = etext_;
465743 for (;;) {
465744 Prog::Inst* ip = prog_->inst(id);
465745 switch (ip->opcode()) {
465746 default:
465747 LOG(DFATAL) << "Unexpected opcode in short circuit: " << ip->opcode();
465748 break;
465749
465750 case kInstCapture:
465751 if (ip->cap() < ncapture_)
465752 match_[ip->cap()] = p;
465753 id = ip->out();
465754 continue;
465755
465756 case kInstNop:
465757 id = ip->out();
465758 continue;
465759
465760 case kInstMatch:
465761 match_[1] = p;
465762 matched_ = true;
465763 break;
465764 }
465765 break;
465766 }
465767 break;
465768 }
465769
465770 if (p > etext_)
465771 break;
465772
465773 // Start a new thread if there have not been any matches.
465774 // (No point in starting a new thread if there have been
465775 // matches, since it would be to the right of the match
465776 // we already found.)
465777 if (!matched_ && (!anchored || p == text.data())) {
465778 // Try to use prefix accel (e.g. memchr) to skip ahead.
465779 // The search must be unanchored and there must be zero
465780 // possible matches already.
465781 if (!anchored && runq->size() == 0 &&
465782 p < etext_ && prog_->can_prefix_accel()) {
465783 p = reinterpret_cast<const char*>(prog_->PrefixAccel(p, etext_ - p));
465784 if (p == NULL)
465785 p = etext_;
465786 }
465787
465788 Thread* t = AllocThread();
465789 CopyCapture(t->capture, match_);
465790 t->capture[0] = p;
465791 AddToThreadq(runq, start_, p < etext_ ? p[0] & 0xFF : -1, context, p,
465792 t);
465793 Decref(t);
465794 }
465795
465796 // If all the threads have died, stop early.
465797 if (runq->size() == 0) {
465798 break;
465799 }
465800
465801 // Avoid invoking undefined behavior (arithmetic on a null pointer)
465802 // by simply not continuing the loop.
465803 // This complements the special case in NFA::Step().
465804 if (p == NULL) {
465805 (void) Step(runq, nextq, -1, context, p);
465806 DCHECK_EQ(runq->size(), 0);
465807 using std::swap;
465808 swap(nextq, runq);
465809 nextq->clear();
465810 break;
465811 }
465812 }
465813
465814 for (Threadq::iterator i = runq->begin(); i != runq->end(); ++i) {
465815 if (i->value() != NULL)
465816 Decref(i->value());
465817 }
465818
465819 if (matched_) {
465820 for (int i = 0; i < nsubmatch; i++)
465821 submatch[i] =
465822 StringPiece(match_[2 * i],
465823 static_cast<size_t>(match_[2 * i + 1] - match_[2 * i]));
465824 return true;
465825 }
465826 return false;
465827}
void swap(Mat &a, Mat &b)

◆ AllocThread()

NFA::Thread * duckdb_re2::NFA::AllocThread ( )
inlineprivate
465392 {
465393 Thread* t = freelist_;
465394 if (t != NULL) {
465395 freelist_ = t->next;
465396 t->ref = 1;
465397 // We don't need to touch t->capture because
465398 // the caller will immediately overwrite it.
465399 return t;
465400 }
465401 arena_.emplace_back();
465402 t = &arena_.back();
465403 t->ref = 1;
465404 t->capture = new const char*[ncapture_];
465405 return t;
465406}

◆ Incref()

NFA::Thread * duckdb_re2::NFA::Incref ( Thread * t)
inlineprivate
465408 {
465409 DCHECK(t != NULL);
465410 t->ref++;
465411 return t;
465412}

◆ Decref()

void duckdb_re2::NFA::Decref ( Thread * t)
inlineprivate
465414 {
465415 DCHECK(t != NULL);
465416 t->ref--;
465417 if (t->ref > 0)
465418 return;
465419 DCHECK_EQ(t->ref, 0);
465420 t->next = freelist_;
465421 freelist_ = t;
465422}

◆ AddToThreadq()

void duckdb_re2::NFA::AddToThreadq ( Threadq * q,
int id0,
int c,
const StringPiece & context,
const char * p,
Thread * t0 )
private
465429 {
465430 if (id0 == 0)
465431 return;
465432
465433 // Use stack_ to hold our stack of instructions yet to process.
465434 // It was preallocated as follows:
465435 // two entries per Capture;
465436 // one entry per EmptyWidth; and
465437 // one entry per Nop.
465438 // This reflects the maximum number of stack pushes that each can
465439 // perform. (Each instruction can be processed at most once.)
465440 AddState* stk = stack_.data();
465441 int nstk = 0;
465442
465443 stk[nstk++] = {id0, NULL};
465444 while (nstk > 0) {
465445 DCHECK_LE(nstk, stack_.size());
465446 AddState a = stk[--nstk];
465447
465448 Loop:
465449 if (a.t != NULL) {
465450 // t0 was a thread that we allocated and copied in order to
465451 // record the capture, so we must now decref it.
465452 Decref(t0);
465453 t0 = a.t;
465454 }
465455
465456 int id = a.id;
465457 if (id == 0)
465458 continue;
465459 if (q->has_index(id)) {
465460 continue;
465461 }
465462
465463 // Create entry in q no matter what. We might fill it in below,
465464 // or we might not. Even if not, it is necessary to have it,
465465 // so that we don't revisit id0 during the recursion.
465466 q->set_new(id, NULL);
465467 Thread** tp = &q->get_existing(id);
465468 int j;
465469 Thread* t;
465470 Prog::Inst* ip = prog_->inst(id);
465471 switch (ip->opcode()) {
465472 default:
465473 LOG(DFATAL) << "unhandled " << ip->opcode() << " in AddToThreadq";
465474 break;
465475
465476 case kInstFail:
465477 break;
465478
465479 case kInstAltMatch:
465480 // Save state; will pick up at next byte.
465481 t = Incref(t0);
465482 *tp = t;
465483
465484 DCHECK(!ip->last());
465485 a = {id+1, NULL};
465486 goto Loop;
465487
465488 case kInstNop:
465489 if (!ip->last())
465490 stk[nstk++] = {id+1, NULL};
465491
465492 // Continue on.
465493 a = {ip->out(), NULL};
465494 goto Loop;
465495
465496 case kInstCapture:
465497 if (!ip->last())
465498 stk[nstk++] = {id+1, NULL};
465499
465500 if ((j=ip->cap()) < ncapture_) {
465501 // Push a dummy whose only job is to restore t0
465502 // once we finish exploring this possibility.
465503 stk[nstk++] = {0, t0};
465504
465505 // Record capture.
465506 t = AllocThread();
465507 CopyCapture(t->capture, t0->capture);
465508 t->capture[j] = p;
465509 t0 = t;
465510 }
465511 a = {ip->out(), NULL};
465512 goto Loop;
465513
465514 case kInstByteRange:
465515 if (!ip->Matches(c))
465516 goto Next;
465517
465518 // Save state; will pick up at next byte.
465519 t = Incref(t0);
465520 *tp = t;
465521
465522 if (ip->hint() == 0)
465523 break;
465524 a = {id+ip->hint(), NULL};
465525 goto Loop;
465526
465527 case kInstMatch:
465528 // Save state; will pick up at next byte.
465529 t = Incref(t0);
465530 *tp = t;
465531
465532 Next:
465533 if (ip->last())
465534 break;
465535 a = {id+1, NULL};
465536 goto Loop;
465537
465538 case kInstEmptyWidth:
465539 if (!ip->last())
465540 stk[nstk++] = {id+1, NULL};
465541
465542 // Continue on if we have all the right flag bits.
465543 if (ip->empty() & ~Prog::EmptyFlags(context, p))
465544 break;
465545 a = {ip->out(), NULL};
465546 goto Loop;
465547 }
465548 }
465549}
tp
An enumeration class of flags to be used in the bitmask template parameter of BS::thread_pool to enab...
Definition BS_thread_pool.hpp:250
Definition duckdb.cpp:465310

◆ Step()

int duckdb_re2::NFA::Step ( Threadq * runq,
Threadq * nextq,
int c,
const StringPiece & context,
const char * p )
private
465559 {
465560 nextq->clear();
465561
465562 for (Threadq::iterator i = runq->begin(); i != runq->end(); ++i) {
465563 Thread* t = i->value();
465564 if (t == NULL)
465565 continue;
465566
465567 if (longest_) {
465568 // Can skip any threads started after our current best match.
465569 if (matched_ && match_[0] < t->capture[0]) {
465570 Decref(t);
465571 continue;
465572 }
465573 }
465574
465575 int id = i->index();
465576 Prog::Inst* ip = prog_->inst(id);
465577
465578 switch (ip->opcode()) {
465579 default:
465580 // Should only see the values handled below.
465581 LOG(DFATAL) << "Unhandled " << ip->opcode() << " in step";
465582 break;
465583
465584 case kInstByteRange:
465585 AddToThreadq(nextq, ip->out(), c, context, p, t);
465586 break;
465587
465588 case kInstAltMatch:
465589 if (i != runq->begin())
465590 break;
465591 // The match is ours if we want it.
465592 if (ip->greedy(prog_) || longest_) {
465593 CopyCapture(match_, t->capture);
465594 matched_ = true;
465595
465596 Decref(t);
465597 for (++i; i != runq->end(); ++i) {
465598 if (i->value() != NULL)
465599 Decref(i->value());
465600 }
465601 runq->clear();
465602 if (ip->greedy(prog_))
465603 return ip->out1();
465604 return ip->out();
465605 }
465606 break;
465607
465608 case kInstMatch: {
465609 // Avoid invoking undefined behavior (arithmetic on a null pointer)
465610 // by storing p instead of p-1. (What would the latter even mean?!)
465611 // This complements the special case in NFA::Search().
465612 if (p == NULL) {
465613 CopyCapture(match_, t->capture);
465614 match_[1] = p;
465615 matched_ = true;
465616 break;
465617 }
465618
465619 if (endmatch_ && p-1 != etext_)
465620 break;
465621
465622 if (longest_) {
465623 // Leftmost-longest mode: save this match only if
465624 // it is either farther to the left or at the same
465625 // point but longer than an existing match.
465626 if (!matched_ || t->capture[0] < match_[0] ||
465627 (t->capture[0] == match_[0] && p-1 > match_[1])) {
465628 CopyCapture(match_, t->capture);
465629 match_[1] = p-1;
465630 matched_ = true;
465631 }
465632 } else {
465633 // Leftmost-biased mode: this match is by definition
465634 // better than what we've already found (see next line).
465635 CopyCapture(match_, t->capture);
465636 match_[1] = p-1;
465637 matched_ = true;
465638
465639 // Cut off the threads that can only find matches
465640 // worse than the one we just found: don't run the
465641 // rest of the current Threadq.
465642 Decref(t);
465643 for (++i; i != runq->end(); ++i) {
465644 if (i->value() != NULL)
465645 Decref(i->value());
465646 }
465647 runq->clear();
465648 return 0;
465649 }
465650 break;
465651 }
465652 }
465653 Decref(t);
465654 }
465655 runq->clear();
465656 return 0;
465657}

◆ FormatCapture()

std::string duckdb_re2::NFA::FormatCapture ( const char ** capture)
private
465659 {
465660 std::string s;
465661 for (int i = 0; i < ncapture_; i+=2) {
465662 if (capture[i] == NULL)
465663 s += "(?,?)";
465664 else if (capture[i+1] == NULL)
465665 s += StringPrintf("(%td,?)",
465666 capture[i] - btext_);
465667 else
465668 s += StringPrintf("(%td,%td)",
465669 capture[i] - btext_,
465670 capture[i+1] - btext_);
465671 }
465672 return s;
465673}

◆ CopyCapture()

void duckdb_re2::NFA::CopyCapture ( const char ** dst,
const char ** src )
inlineprivate
465344 {
465345 memmove(dst, src, ncapture_*sizeof src[0]);
465346 }

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