filter.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. void verify_integrity(){ };
  50. public:
  51. ObsFilter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
  52. :DerivedValue<bool>(name),
  53. filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
  54. /** Return a new filter that is the conjuction of the two source filters.
  55. */
  56. static ObsFilter* conj(ObsFilter *f1, ObsFilter *f2){
  57. auto new_name = f1->get_name() + "&&" + f2->get_name();
  58. return new ObsFilter(new_name, [f1,f2](){return f1->get_value() && f2->get_value();});
  59. }
  60. /** Return a new filter that is the conjuction of the two source filters.
  61. */
  62. static ObsFilter* disj(ObsFilter *f1, ObsFilter *f2){
  63. auto new_name = f1->get_name() + "||" + f2->get_name();
  64. return new ObsFilter(new_name, [f1, f2](){return f1->get_value() && f2->get_value();});
  65. }
  66. /** Return a new filter that is the conjuction of the two source filters.
  67. */
  68. ObsFilter* operator*(ObsFilter *f){
  69. auto new_name = this->get_name() + "&&" + f->get_name();
  70. return new ObsFilter(new_name, [this, f](){return this->get_value() && f->get_value();});
  71. }
  72. /** Return a new filter that is the disjunction of the two source
  73. * filters.
  74. */
  75. ObsFilter* operator+(ObsFilter *f){
  76. auto new_name = this->get_name() + "||" + f->get_name();
  77. return new ObsFilter(new_name, [this, f](){return this->get_value() || f->get_value();});
  78. }
  79. /** Return a new filter that is the negation of the source filter.
  80. */
  81. ObsFilter* operator!(){
  82. auto new_name = std::string("!(") + this->get_name() + std::string(")");
  83. return new ObsFilter(new_name, [this](){return !this->get_value();});
  84. }
  85. };
  86. template <typename T>
  87. class RangeObsFilter : public ObsFilter{
  88. private:
  89. public:
  90. RangeObsFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
  91. ObsFilter(name, [test_value, range_low, range_high]{
  92. T val = test_value->get_value();
  93. return (val >= range_low) && (val < range_high);
  94. }) { }
  95. };
  96. }
  97. #endif // filter_h