filter.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * A Filter is a special type of derived value that can only return a boolean.
  33. * Container objects have a vector of filters that control if a "fill" call
  34. * actually places data into the container or not. This file contains a variety
  35. * of generic filters to aide in analysis.
  36. */
  37. #ifndef filter_h
  38. #define filter_h
  39. #include <iostream>
  40. #include <functional>
  41. #include "value.hpp"
  42. namespace fv {
  43. class ObsFilter : public DerivedValue<bool>{
  44. private:
  45. Function<bool()>& filter_function;
  46. void update_value(){
  47. value = filter_function();
  48. }
  49. public:
  50. ObsFilter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
  51. :DerivedValue<bool>(name),
  52. filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
  53. };
  54. /** Return a new filter that is the conjuction of a vector of source filters
  55. */
  56. ObsFilter* all(const std::vector<ObsFilter*>&& fs){
  57. if(fs.size() == 0){
  58. return nullptr;
  59. } else{
  60. std::stringstream ss;
  61. ss << fs[0]->get_name();
  62. for(size_t i=1; i<fs.size(); i++) ss << "AND" << fs[i]->get_name();
  63. return new ObsFilter(ss.str(), [fs](){
  64. return std::all_of(std::begin(fs), std::end(fs), [](ObsFilter* f){return f->get_value();});
  65. });
  66. }
  67. }
  68. /** Return a new filter that is the disjunction of a vector of source filters
  69. */
  70. ObsFilter* any(const std::vector<ObsFilter*>&& fs){
  71. if(fs.size() == 0){
  72. return nullptr;
  73. } else{
  74. std::stringstream ss;
  75. ss << fs[0]->get_name();
  76. for(size_t i=1; i<fs.size(); i++) ss << "OR" << fs[i]->get_name();
  77. return new ObsFilter(ss.str(), [fs](){
  78. return std::any_of(std::begin(fs), std::end(fs), [](ObsFilter* f){return f->get_value();});
  79. });
  80. }
  81. }
  82. /** Return a new filter that is the conjuction of the two source filters.
  83. */
  84. ObsFilter* all(ObsFilter *f1, ObsFilter *f2){
  85. return all({f1, f2});
  86. }
  87. /** Return a new filter that is the conjuction of the two source filters.
  88. */
  89. ObsFilter* any(ObsFilter *f1, ObsFilter *f2){
  90. return any({f1, f2});
  91. }
  92. }
  93. #endif // filter_h