Skip to content

Commit

Permalink
Use binary flags for FilteredItemType
Browse files Browse the repository at this point in the history
  • Loading branch information
gin-ahirsch committed Jun 15, 2018
1 parent 7b57a0c commit 6cf165e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/crawlerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void CrawlerWidget::markLineFromFiltered( qint64 line )
if ( line < logFilteredData_->getNbLine() ) {
qint64 line_in_file = logFilteredData_->getMatchingLineNumber( line );
if ( logFilteredData_->filteredLineTypeByIndex( line )
== LogFilteredData::Mark )
& LogFilteredData::Mark )
logFilteredData_->deleteMark( line_in_file );
else
logFilteredData_->addMark( line_in_file );
Expand Down
14 changes: 8 additions & 6 deletions src/data/logfiltereddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,21 @@ void LogFilteredData::regenerateFilteredItemsCache() const
( j != marks_.end() ) ? j->lineNumber() : std::numeric_limits<qint64>::max();
qint64 next_match =
( i != matching_lines_.cend() ) ? i->lineNumber() : std::numeric_limits<qint64>::max();
// We choose a Mark over a Match if a line is both, just an arbitrary choice really.
FilteredLineType type = static_cast<FilteredLineType>( 0 );
LineNumber line;
if ( next_mark <= next_match ) {
// LOG(logDEBUG) << "Add mark at " << next_mark;
filteredItemsCache_.push_back( FilteredItem( next_mark, Mark ) );
type |= Mark;
line = next_mark;
++j;
if ( ( next_mark == next_match ) )
++i; // Case when it's both match and mark.
}
else {
if ( next_mark >= next_match ) {
// LOG(logDEBUG) << "Add match at " << next_match;
filteredItemsCache_.push_back( FilteredItem( next_match, Match ) );
type |= Match;
line = next_match;
++i;
}
filteredItemsCache_.push_back( FilteredItem( line, type ) );
}

filteredItemsCacheDirty_ = false;
Expand Down
32 changes: 30 additions & 2 deletions src/data/logfiltereddata.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ class LogFilteredData : public AbstractLogData {
// Returns the number of marks (independently of the visibility)
LineNumber getNbMarks() const;

// Flags indicating why the line is filtered.
enum FilteredLineType {
Match = 0x1,
Mark = 0x2,
};
// Returns the reason why the line at the passed index is in the filtered
// data. It can be because it is either a mark or a match.
enum FilteredLineType { Match, Mark };
// data.
FilteredLineType filteredLineTypeByIndex( int index ) const;

// Marks interface (delegated to a Marks object)
Expand Down Expand Up @@ -164,6 +168,23 @@ class LogFilteredData : public AbstractLogData {
void regenerateFilteredItemsCache() const;
};

static LogFilteredData::FilteredLineType& operator|=(LogFilteredData::FilteredLineType& a, LogFilteredData::FilteredLineType b)
{
a = LogFilteredData::FilteredLineType( a | b );
return a;
}

static LogFilteredData::FilteredLineType& operator&=(LogFilteredData::FilteredLineType& a, LogFilteredData::FilteredLineType b)
{
a = LogFilteredData::FilteredLineType( a & b );
return a;
}

static LogFilteredData::FilteredLineType operator~(LogFilteredData::FilteredLineType a)
{
return LogFilteredData::FilteredLineType( ~static_cast<int>( a ) );
}

// A class representing a Mark or Match.
// Conceptually it should be a base class for Mark and MatchingLine,
// but we implement it this way for performance reason as we create plenty of
Expand All @@ -183,6 +204,13 @@ class LogFilteredData::FilteredItem {
FilteredLineType type() const
{ return type_; }

void add( FilteredLineType type )
{ type_ |= type; }

// Returns whether any type-flag is left.
bool remove( FilteredLineType type )
{ return type_ &= ~type; }

bool operator <( const LogFilteredData::FilteredItem& other ) const
{ return lineNumber_ < other.lineNumber_; }

Expand Down
2 changes: 1 addition & 1 deletion src/filteredview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ AbstractLogView::LineType FilteredView::lineType( int lineNumber ) const
{
LogFilteredData::FilteredLineType type =
logFilteredData_->filteredLineTypeByIndex( lineNumber );
if ( type == LogFilteredData::Mark )
if ( type & LogFilteredData::Mark ) // If both Mark and Match, Mark wins
return Marked;
else
return Match;
Expand Down
2 changes: 1 addition & 1 deletion src/overview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void Overview::recalculatesLines()
logFilteredData_->filteredLineTypeByIndex( i );
int line = (int) logFilteredData_->getMatchingLineNumber( i );
int position = (int)( (qint64)line * height_ / linesInFile_ );
if ( line_type == LogFilteredData::Match ) {
if ( line_type & LogFilteredData::Match ) {
if ( ( ! matchLines_.isEmpty() ) && matchLines_.last().position() == position ) {
// If the line is already there, we increase its weight
matchLines_.last().load();
Expand Down

0 comments on commit 6cf165e

Please sign in to comment.