From e7dd8edf26a397e015a3129b38e0eed4420cf905 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 22 Apr 2016 22:10:26 -0400 Subject: [PATCH] Filter: Merged Filter and Interval, removing Filter --- src/CMakeLists.txt | 1 - src/Filter.cpp | 80 -------------------------------------- src/Filter.h | 53 ------------------------- src/commands/CmdExport.cpp | 2 +- src/commands/CmdReport.cpp | 2 +- src/commands/CmdTrack.cpp | 7 ++-- src/helper.cpp | 39 ++++++------------- src/timew.h | 8 ++-- 8 files changed, 20 insertions(+), 172 deletions(-) delete mode 100644 src/Filter.cpp delete mode 100644 src/Filter.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 116059f3..e5523a04 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,6 @@ set (timew_SRCS CLI.cpp CLI.h Daterange.cpp Daterange.h Exclusion.cpp Exclusion.h Extensions.cpp Extensions.h - Filter.cpp Filter.h Interval.cpp Interval.h Lexer.cpp Lexer.h Rules.cpp Rules.h diff --git a/src/Filter.cpp b/src/Filter.cpp deleted file mode 100644 index 563ebb34..00000000 --- a/src/Filter.cpp +++ /dev/null @@ -1,80 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -// http://www.opensource.org/licenses/mit-license.php -// -//////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include - -//////////////////////////////////////////////////////////////////////////////// -bool Filter::empty () const -{ - return _range.start ().toEpoch () == 0 && - _range.end ().toEpoch () == 0 && - _tags.size () == 0; -} - -//////////////////////////////////////////////////////////////////////////////// -Daterange Filter::range () const -{ - return _range; -} - -//////////////////////////////////////////////////////////////////////////////// -void Filter::range (const Daterange& range) -{ - _range = range; -} - -//////////////////////////////////////////////////////////////////////////////// -std::set Filter::tags () const -{ - return _tags; -} - -//////////////////////////////////////////////////////////////////////////////// -void Filter::tag (const std::string& tag) -{ - if (_tags.find (tag) == _tags.end ()) - _tags.insert (tag); -} - -//////////////////////////////////////////////////////////////////////////////// -std::string Filter::dump () const -{ - std::stringstream out; - - out << "Filter _range " - << _range.dump () - << " _tags"; - - for (auto& tag : _tags) - out << " '" << tag << "'"; - - out << "\n"; - return out.str (); -} - -//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Filter.h b/src/Filter.h deleted file mode 100644 index e93bf458..00000000 --- a/src/Filter.h +++ /dev/null @@ -1,53 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -// http://www.opensource.org/licenses/mit-license.php -// -//////////////////////////////////////////////////////////////////////////////// - -#ifndef INCLUDED_FILTER -#define INCLUDED_FILTER - -#include -#include -#include - -class Filter -{ -public: - Filter () = default; - bool empty () const; - - Daterange range () const; - void range (const Daterange&); - - std::set tags () const; - void tag (const std::string&); - - std::string dump () const; - -private: - Daterange _range {}; - std::set _tags {}; -}; - -#endif diff --git a/src/commands/CmdExport.cpp b/src/commands/CmdExport.cpp index 350fa419..cf7588a8 100644 --- a/src/commands/CmdExport.cpp +++ b/src/commands/CmdExport.cpp @@ -35,7 +35,7 @@ int CmdExport ( Rules& rules, Database& database) { - auto filter = createFilterFromCLI (cli); + auto filter = createFilterIntervalFromCLI (cli); auto timeline = createTimelineFromData (rules, database, filter); std::cout << jsonFromIntervals (timeline.tracked (rules)); return 0; diff --git a/src/commands/CmdReport.cpp b/src/commands/CmdReport.cpp index b83ade3b..e709219b 100644 --- a/src/commands/CmdReport.cpp +++ b/src/commands/CmdReport.cpp @@ -79,7 +79,7 @@ int CmdReport ( throw std::string ("Specify which report to run."); // Filter the data. - auto filter = createFilterFromCLI (cli); + auto filter = createFilterIntervalFromCLI (cli); auto timeline = createTimelineFromData (rules, database, filter); auto intervals = timeline.tracked (rules); diff --git a/src/commands/CmdTrack.cpp b/src/commands/CmdTrack.cpp index 25b7b488..0a2fce9b 100644 --- a/src/commands/CmdTrack.cpp +++ b/src/commands/CmdTrack.cpp @@ -35,13 +35,12 @@ int CmdTrack ( Rules& rules, Database& database) { - auto filter = createFilterFromCLI (cli); - auto tracked = createIntervalFromFilter (filter); - database.addInterval (tracked); + auto filter = createFilterIntervalFromCLI (cli); + database.addInterval (filter); // User feedback. if (rules.getBoolean ("verbose")) - std::cout << intervalSummarize (rules, tracked); + std::cout << intervalSummarize (rules, filter); return 0; } diff --git a/src/helper.cpp b/src/helper.cpp index e06594e2..92be3cd8 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -115,17 +115,16 @@ bool expandIntervalHint ( } //////////////////////////////////////////////////////////////////////////////// -// A filter is a placeholder for a start datetime, end datetime and a set of -// tags, which makes it essentially an interval. +// A filter is just another interval, containing start, end and tags. // // Supported interval forms: // ["from"] ["to"|"-" ] // ["from"] "for" // ["before"|"after" ] // -Filter createFilterFromCLI (const CLI& cli) +Interval createFilterIntervalFromCLI (const CLI& cli) { - Filter filter; + Interval filter; std::string start; std::string end; std::string duration; @@ -275,20 +274,6 @@ Filter createFilterFromCLI (const CLI& cli) return filter; } -//////////////////////////////////////////////////////////////////////////////// -// An interval and a filter are almost identical. -// TODO Why aren't they identical? Fix this. -Interval createIntervalFromFilter (const Filter& filter) -{ - Interval interval; - interval.range (filter.range ()); - - for (auto& tag : filter.tags ()) - interval.tag (tag); - - return interval; -} - //////////////////////////////////////////////////////////////////////////////// // The five different overlap possibilities: // @@ -307,7 +292,7 @@ Interval createIntervalFromFilter (const Filter& filter) Timeline createTimelineFromData ( const Rules& rules, Database& database, - const Filter& filter) + const Interval& filter) { Timeline t; t.range (filter.range ()); @@ -320,7 +305,7 @@ Timeline createTimelineFromData ( Interval i; i.initialize (line); - if (intervalMatchesFilter (i, filter)) + if (intervalMatchesFilterInterval (i, filter)) t.include (i); } else if (line[0] == 'e') @@ -350,17 +335,17 @@ Interval getLatestInterval (Database& database) } //////////////////////////////////////////////////////////////////////////////// -// An interval matches a filter if the start/end overlaps, and all filter tags -// are found in the interval. -bool intervalMatchesFilter (const Interval& interval, const Filter& filter) +// An interval matches a filter interval if the start/end overlaps, and all +// filter interval tags are found in the interval. +bool intervalMatchesFilterInterval (const Interval& interval, const Interval& filter) { - if ((filter.range ().start ().toEpoch () == 0 && - filter.range ().end ().toEpoch () == 0) + if ((filter.start ().toEpoch () == 0 && + filter.end ().toEpoch () == 0) || - (interval.end () > filter.range ().start () && - interval.start () < filter.range ().end ())) + (interval.end () > filter.start () && + interval.start () < filter.end ())) { for (auto& tag : filter.tags ()) if (! interval.hasTag (tag)) diff --git a/src/timew.h b/src/timew.h index dc335d31..7755ec6a 100644 --- a/src/timew.h +++ b/src/timew.h @@ -32,7 +32,6 @@ #include #include #include -#include #include #include @@ -48,11 +47,10 @@ int dispatchCommand (const CLI&, Database&, Rules&, const Extensions&); Color tagColor (const Rules&, const std::string&); std::string intervalSummarize (const Rules&, const Interval&); bool expandIntervalHint (const std::string&, std::string&, std::string&); -Filter createFilterFromCLI (const CLI&); -Interval createIntervalFromFilter (const Filter&); -Timeline createTimelineFromData (const Rules&, Database&, const Filter&); +Interval createFilterIntervalFromCLI (const CLI&); +Timeline createTimelineFromData (const Rules&, Database&, const Interval&); Interval getLatestInterval (Database&); -bool intervalMatchesFilter (const Interval&, const Filter&); +bool intervalMatchesFilterInterval (const Interval&, const Interval&); std::string jsonFromIntervals (const std::vector &); std::vector rangesFromHolidays (const Rules&); std::vector addRanges (const Daterange&, const std::vector &, const std::vector &);