Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkPropertyFilter.h"
18 #include <algorithm>
19 
20 namespace mitk
21 {
22  class PropertyFilter::Impl
23  {
24  public:
25  Impl();
26  ~Impl();
27 
28  void AddEntry(const std::string &propertyName, PropertyFilter::List list);
29  std::map<std::string, BaseProperty::Pointer> Apply(
30  const std::map<std::string, BaseProperty::Pointer> &propertyMap) const;
31  bool HasEntry(const std::string &propertyName, PropertyFilter::List list) const;
32  bool IsEmpty() const;
34  void RemoveEntry(const std::string &propertyName, PropertyFilter::List list);
35 
36  private:
37  void AddEntry(const std::string &propertyName, std::vector<std::string> &list);
38  bool HasEntry(const std::string &propertyName, const std::vector<std::string> &list) const;
39  void RemoveAllEntries(std::vector<std::string> &list);
40  void RemoveEntry(const std::string &propertyName, std::vector<std::string> &list);
41 
42  std::vector<std::vector<std::string>> m_Lists;
43  };
44 
45  PropertyFilter::Impl::Impl() : m_Lists(2) {}
46  PropertyFilter::Impl::~Impl() {}
47  void mitk::PropertyFilter::Impl::AddEntry(const std::string &propertyName, List list)
48  {
49  this->AddEntry(propertyName, m_Lists.at(list));
50  }
51 
52  void PropertyFilter::Impl::AddEntry(const std::string &propertyName, std::vector<std::string> &list)
53  {
54  if (std::find(list.begin(), list.end(), propertyName) == list.end())
55  list.push_back(propertyName);
56  }
57 
58  std::map<std::string, BaseProperty::Pointer> PropertyFilter::Impl::Apply(
59  const std::map<std::string, BaseProperty::Pointer> &propertyMap) const
60  {
61  std::map<std::string, BaseProperty::Pointer> ret =
62  !m_Lists[Whitelist].empty() ? std::map<std::string, BaseProperty::Pointer>() : propertyMap;
63 
64  if (!m_Lists[Whitelist].empty())
65  {
66  auto end = propertyMap.end();
67 
68  for (auto iter = propertyMap.begin(); iter != end; ++iter)
69  {
70  if (std::find(m_Lists[Whitelist].begin(), m_Lists[Whitelist].end(), iter->first) != m_Lists[Whitelist].end())
71  ret.insert(*iter);
72  }
73  }
74 
75  if (!m_Lists[Blacklist].empty())
76  {
77  auto end = m_Lists[Blacklist].end();
78 
79  for (auto iter = m_Lists[Blacklist].begin(); iter != end; ++iter)
80  ret.erase(*iter);
81  }
82 
83  return ret;
84  }
85 
86  bool mitk::PropertyFilter::Impl::HasEntry(const std::string &propertyName, List list) const
87  {
88  return this->HasEntry(propertyName, m_Lists.at(list));
89  }
90 
91  bool PropertyFilter::Impl::HasEntry(const std::string &propertyName, const std::vector<std::string> &list) const
92  {
93  return std::find(list.begin(), list.end(), propertyName) != list.end();
94  }
95 
96  bool PropertyFilter::Impl::IsEmpty() const
97  {
98  auto end = m_Lists.end();
99 
100  for (auto iter = m_Lists.begin(); iter != end; ++iter)
101  {
102  if (!iter->empty())
103  return false;
104  }
105 
106  return true;
107  }
108 
109  void mitk::PropertyFilter::Impl::RemoveAllEntries(List list) { this->RemoveAllEntries(m_Lists.at(list)); }
110  void PropertyFilter::Impl::RemoveAllEntries(std::vector<std::string> &list) { list.clear(); }
111  void mitk::PropertyFilter::Impl::RemoveEntry(const std::string &propertyName, List list)
112  {
113  this->RemoveEntry(propertyName, m_Lists.at(list));
114  }
115 
116  void PropertyFilter::Impl::RemoveEntry(const std::string &propertyName, std::vector<std::string> &list)
117  {
118  auto iter = std::find(list.begin(), list.end(), propertyName);
119 
120  if (iter != list.end())
121  list.erase(iter);
122  }
123 }
124 
126 {
127 }
128 
130 {
131  delete m_Impl;
132 }
133 
135 {
136 }
137 
139 {
140  std::swap(this->m_Impl, other.m_Impl);
141  return *this;
142 }
143 
144 void mitk::PropertyFilter::AddEntry(const std::string &propertyName, List list)
145 {
146  if (!propertyName.empty())
147  m_Impl->AddEntry(propertyName, list);
148 }
149 
150 std::map<std::string, mitk::BaseProperty::Pointer> mitk::PropertyFilter::Apply(
151  const std::map<std::string, mitk::BaseProperty::Pointer> &propertyMap) const
152 {
153  return m_Impl->Apply(propertyMap);
154 }
155 
156 bool mitk::PropertyFilter::HasEntry(const std::string &propertyName, List list) const
157 {
158  return !propertyName.empty() ? m_Impl->HasEntry(propertyName, list) : false;
159 }
160 
162 {
163  return m_Impl->IsEmpty();
164 }
165 
167 {
168  m_Impl->RemoveAllEntries(list);
169 }
170 
171 void mitk::PropertyFilter::RemoveEntry(const std::string &propertyName, List list)
172 {
173  if (!propertyName.empty())
174  m_Impl->RemoveEntry(propertyName, list);
175 }
List
Specifies the type of a filter entry.
void AddEntry(const std::string &propertyName, List list)
Add a filter entry for a specific property.
std::map< std::string, BaseProperty::Pointer > Apply(const std::map< std::string, BaseProperty::Pointer > &propertyMap) const
Apply the filter to a property list.
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.
bool IsEmpty() const
Check if filter is empty.
PropertyFilter & operator=(PropertyFilter other)
bool HasEntry(const std::string &propertyName, List list) const
Check if filter has specific entry.
static void swap(T &x, T &y)
Definition: svm.cpp:72
Consists of blacklist and whitelist entries.