Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algorithms Library Toolkit Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Algorithms Library Toolkit
Algorithms Library Toolkit Core
Merge requests
!153
Jirakjan bp rebase
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Jirakjan bp rebase
jirakjan-bp-rebase
into
master
Overview
0
Commits
10
Pipelines
1
Changes
7
Merged
Jan Trávníček
requested to merge
jirakjan-bp-rebase
into
master
4 years ago
Overview
0
Commits
10
Pipelines
1
Changes
7
Expand
Merge code by Jan Jirak that resulted from his BP
0
0
Merge request reports
Viewing commit
5bc1596c
Prev
Next
Show latest version
7 files
+
27
−
7
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Search (e.g. *.vue) (Ctrl+P)
5bc1596c
added measurements
· 5bc1596c
Jan Jirák
authored
4 years ago
alib2algo/src/stringology/exact/CGR.h
0 → 100644
+
88
−
0
Options
/*
* CGR.h
*
* Created on: 19. 3. 2020
* Author: Jan Jirak
*/
#ifndef _STRINGOLOGY_CGR_H_
#define _STRINGOLOGY_CGR_H_
#include <alib/measure>
#include <alib/set>
#include <alib/vector>
#include <string/LinearString.h>
#include <string/properties/Repetition.h>
namespace stringology {
namespace exact {
/**
* Implementation of the CGR algorithm from article "Constant-space string-matching in sublinear average time"
* Maxim Crochemore and Leszek Gasieniec and Wojciech Rytter
*/
class CGR {
public:
/**
* Search for pattern in linear string.
* @return set set of occurences
*/
template < class SymbolType >
static ext::set < unsigned > match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern );
};
template < class SymbolType >
ext::set < unsigned > CGR::match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) {
ext::set<unsigned> occ;
const auto & text = subject.getContent();
const auto & pat = pattern.getContent();
size_t n = text.size(), m = pat.size();
size_t repSize, p , q ;
measurements::start ( "Preprocess", measurements::Type::PREPROCESS );
std::tie( repSize , p , q ) = string::properties::Repetition::construct(pattern) ;
measurements::end ( );
measurements::start ( "Algorithm", measurements::Type::ALGORITHM );
// for repSize == 0 or 1 use naive solution
if ( repSize == 0 || repSize == 1 ) {
for ( size_t i = 0 ; i <= n - m ; ++ i ) {
size_t j = 0 ;
while ( j < m && text[i + j ] == pat[j] ) ++ j ;
if ( j == m ) occ.insert(i) ;
}
measurements::end();
return occ ;
}
size_t i = repSize/2 ;
while ( i <= n - m ) {
bool leftmostMismatch = std::equal(text.begin() + i + p , text.begin() + i + p + repSize/2 , text.begin() + i + q ) ;
if ( leftmostMismatch ) {
for ( size_t i_0 = i - repSize / 2 ; i_0 <= i ; ++ i_0 ){
if ( std::equal( pat.begin() , pat.end() , text.begin() + i_0 ) ) occ.insert(i_0) ;
}
}
i += repSize / 2 ;
}
// check last positions, where i jump into last window
for ( size_t j = i - repSize / 2 ; j <= n - m ; ++ j ) {
if ( std::equal( pat.begin() , pat.end() , text.begin() + j ) ) occ.insert(j) ;
}
measurements::end();
return occ ;
}
} /* namespace exact */
} /* namespace stringology */
#endif /* _STRINGOLOGY_TAILED_SUBSTRING_H_ */
Loading