Medical Imaging Interaction Toolkit  2018.4.99-18ddd2ed
Medical Imaging Interaction Toolkit
mitkPropertyFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkPropertyFilter.h"
14 #include <algorithm>
15 
16 namespace mitk
17 {
18  class PropertyFilter::Impl
19  {
20  public:
21  Impl();
22  ~Impl();
23 
24  void AddEntry(const std::string &propertyName, PropertyFilter::List list);
25  std::map<std::string, BaseProperty::Pointer> Apply(
26  const std::map<std::string, BaseProperty::Pointer> &propertyMap) const;
27  bool HasEntry(const std::string &propertyName, PropertyFilter::List list) const;
28  bool IsEmpty() const;
30  void RemoveEntry(const std::string &propertyName, PropertyFilter::List list);
31 
32  private:
33  void AddEntry(const std::string &propertyName, std::vector<std::string> &list);
34  bool HasEntry(const std::string &propertyName, const std::vector<std::string> &list) const;
35  void RemoveAllEntries(std::vector<std::string> &list);
36  void RemoveEntry(const std::string &propertyName, std::vector<std::string> &list);
37 
38  std::vector<std::vector<std::string>> m_Lists;
39  };
40 
41  PropertyFilter::Impl::Impl() : m_Lists(2) {}
42  PropertyFilter::Impl::~Impl() {}
43  void mitk::PropertyFilter::Impl::AddEntry(const std::string &propertyName, List list)
44  {
45  this->AddEntry(propertyName, m_Lists.at(list));
46  }
47 
48  void PropertyFilter::Impl::AddEntry(const std::string &propertyName, std::vector<std::string> &list)
49  {
50  if (std::find(list.begin(), list.end(), propertyName) == list.end())
51  list.push_back(propertyName);
52  }
53 
54  std::map<std::string, BaseProperty::Pointer> PropertyFilter::Impl::Apply(
55  const std::map<std::string, BaseProperty::Pointer> &propertyMap) const
56  {
57  std::map<std::string, BaseProperty::Pointer> ret =
58  !m_Lists[Whitelist].empty() ? std::map<std::string, BaseProperty::Pointer>() : propertyMap;
59 
60  if (!m_Lists[Whitelist].empty())
61  {
62  auto end = propertyMap.end();
63 
64  for (auto iter = propertyMap.begin(); iter != end; ++iter)
65  {
66  if (std::find(m_Lists[Whitelist].begin(), m_Lists[Whitelist].end(), iter->first) != m_Lists[Whitelist].end())
67  ret.insert(*iter);
68  }
69  }
70 
71  if (!m_Lists[Blacklist].empty())
72  {
73  auto end = m_Lists[Blacklist].end();
74 
75  for (auto iter = m_Lists[Blacklist].begin(); iter != end; ++iter)
76  ret.erase(*iter);
77  }
78 
79  return ret;
80  }
81 
82  bool mitk::PropertyFilter::Impl::HasEntry(const std::string &propertyName, List list) const
83  {
84  return this->HasEntry(propertyName, m_Lists.at(list));
85  }
86 
87  bool PropertyFilter::Impl::HasEntry(const std::string &propertyName, const std::vector<std::string> &list) const
88  {
89  return std::find(list.begin(), list.end(), propertyName) != list.end();
90  }
91 
92  bool PropertyFilter::Impl::IsEmpty() const
93  {
94  auto end = m_Lists.end();
95 
96  for (auto iter = m_Lists.begin(); iter != end; ++iter)
97  {
98  if (!iter->empty())
99  return false;
100  }
101 
102  return true;
103  }
104 
105  void mitk::PropertyFilter::Impl::RemoveAllEntries(List list) { this->RemoveAllEntries(m_Lists.at(list)); }
106  void PropertyFilter::Impl::RemoveAllEntries(std::vector<std::string> &list) { list.clear(); }
107  void mitk::PropertyFilter::Impl::RemoveEntry(const std::string &propertyName, List list)
108  {
109  this->RemoveEntry(propertyName, m_Lists.at(list));
110  }
111 
112  void PropertyFilter::Impl::RemoveEntry(const std::string &propertyName, std::vector<std::string> &list)
113  {
114  auto iter = std::find(list.begin(), list.end(), propertyName);
115 
116  if (iter != list.end())
117  list.erase(iter);
118  }
119 }
120 
122 {
123 }
124 
126 {
127  delete m_Impl;
128 }
129 
130 mitk::PropertyFilter::PropertyFilter(const mitk::PropertyFilter &other) : m_Impl(new Impl(*other.m_Impl))
131 {
132 }
133 
135 {
136  std::swap(this->m_Impl, other.m_Impl);
137  return *this;
138 }
139 
140 void mitk::PropertyFilter::AddEntry(const std::string &propertyName, List list)
141 {
142  if (!propertyName.empty())
143  m_Impl->AddEntry(propertyName, list);
144 }
145 
146 std::map<std::string, mitk::BaseProperty::Pointer> mitk::PropertyFilter::Apply(
147  const std::map<std::string, mitk::BaseProperty::Pointer> &propertyMap) const
148 {
149  return m_Impl->Apply(propertyMap);
150 }
151 
152 bool mitk::PropertyFilter::HasEntry(const std::string &propertyName, List list) const
153 {
154  return !propertyName.empty() ? m_Impl->HasEntry(propertyName, list) : false;
155 }
156 
158 {
159  return m_Impl->IsEmpty();
160 }
161 
163 {
164  m_Impl->RemoveAllEntries(list);
165 }
166 
167 void mitk::PropertyFilter::RemoveEntry(const std::string &propertyName, List list)
168 {
169  if (!propertyName.empty())
170  m_Impl->RemoveEntry(propertyName, list);
171 }
List
Specifies the type of a filter entry.
void AddEntry(const std::string &propertyName, List list)
Add a filter entry for a specific property.
DataCollection - Class to facilitate loading/accessing structured data.
void RemoveEntry(const std::string &propertyName, List list)
Remove specific entry from property filter.
void RemoveAllEntries(List list)
Remove all entries from property filter.
PropertyFilter & operator=(PropertyFilter other)
static void swap(T &x, T &y)
Definition: svm.cpp:58
bool IsEmpty() const
Check if filter is empty.
bool HasEntry(const std::string &propertyName, List list) const
Check if filter has specific entry.
std::map< std::string, BaseProperty::Pointer > Apply(const std::map< std::string, BaseProperty::Pointer > &propertyMap) const
Apply the filter to a property list.
Consists of blacklist and whitelist entries.