EMANE  1.2.1
statistictable.inl
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2015 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
34 #include <algorithm>
35 
36 template<typename Key,typename Compare,std::size_t sortIndex>
38  labels_{labels}{}
39 
40 template<typename Key,typename Compare,std::size_t sortIndex>
42 
43 
44 template<typename Key,typename Compare,std::size_t sortIndex>
46  std::size_t columnIndex,
47  const Any & any)
48 {
49  std::lock_guard<std::mutex> m(mutex_);
50 
51  auto iter = table_.find(key);
52 
53  if(iter != table_.end())
54  {
55  if(columnIndex < labels_.size())
56  {
57  iter->second[columnIndex] = any;
58  }
59  else
60  {
61  throw makeException<StatisticTableException>("column index out of range: %zu",
62  columnIndex);
63  }
64  }
65  else
66  {
67  throw StatisticTableException("unknown row key");
68  }
69 }
70 
71 template<typename Key,typename Compare,std::size_t sortIndex>
73  const std::vector<Any> & anys)
74 {
75  std::lock_guard<std::mutex> m(mutex_);
76 
77  auto iter = table_.find(key);
78 
79  if(iter != table_.end())
80  {
81  if(anys.size() == labels_.size())
82  {
83  iter->second = anys;
84  }
85  else
86  {
87  throw makeException<StatisticTableException>("column count not valid: %zu",
88  anys.size());
89  }
90  }
91  else
92  {
93  throw StatisticTableException("unknown row key");
94  }
95 }
96 
97 template<typename Key,typename Compare,std::size_t sortIndex>
99  const std::vector<Any> & anys)
100 {
101  std::lock_guard<std::mutex> m(mutex_);
102 
103  if(anys.size() == labels_.size())
104  {
105 
106  if(!table_.insert(std::make_pair(key,anys)).second)
107  {
108  throw StatisticTableException("duplicate row key");
109  }
110  }
111  else if(anys.empty())
112  {
113  if(!table_.insert(std::make_pair(key,std::vector<Any>(labels_.size(),Any("")))).second)
114  {
115  throw StatisticTableException("duplicate row key");
116  }
117  }
118  else
119  {
120  throw makeException<StatisticTableException>("column count not valid: %zu",
121  anys.size());
122 
123  }
124 }
125 
126 template<typename Key,typename Compare,std::size_t sortIndex>
128 {
129  std::lock_guard<std::mutex> m(mutex_);
130 
131  table_.erase(key);
132 }
133 
134 template<typename Key,typename Compare,std::size_t sortIndex>
137 {
138  StatisticTableValues values{};
139 
140  // lock guard block scope
141  {
142  std::lock_guard<std::mutex> m(mutex_);
143 
144  std::transform(table_.begin(),
145  table_.end(),
146  std::back_inserter(values),
147  std::bind(&InternalTable::value_type::second,
148  std::placeholders::_1));
149  }
150  Compare cmp{};
151  std::sort(values.begin(),
152  values.end(),
153  [&cmp](const std::vector<Any> & a1,
154  const std::vector<Any> & a2)
155  {
156  return cmp(a1[sortIndex],a2[sortIndex]);
157  });
158 
159  return values;
160 }
161 
162 template<typename Key,typename Compare,std::size_t sortIndex>
165 {
166  // labels_ is immutable - no synchronization required
167  return labels_;
168 }
169 
170 template<typename Key,typename Compare,std::size_t sortIndex>
171 void
173 {
174  std::lock_guard<std::mutex> m(mutex_);
175  table_.clear();
176 }
StatisticTableLabels getLabels() const override
A two dimentional statistic table that holds Any values.
std::vector< std::vector< Any > > StatisticTableValues
void setCell(const Key &key, std::size_t columnIndex, const Any &any)
StatisticTableValues getValues() const override
void setRow(const Key &key, const std::vector< Any > &anys)
std::vector< std::string > StatisticTableLabels
StatistictableException is thrown when an exception occurs during creation or conversion of an Statis...
void addRow(const Key &key, const std::vector< Any > &anys={})
void deleteRow(const Key &key)
The Any class can contain an instance of one of any type in its support type set. ...
Definition: any.h:49