EMANE  1.0.1
wheel.inl
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 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 
33 template<typename T>
34 EMANE::Wheel<T>::Wheel(std::size_t size):
35  size_{size},
36  store_(size,0){}
37 
38 template<typename T>
39 const std::vector<T> & EMANE::Wheel<T>::dump() const
40 {
41  return store_;
42 }
43 
44 template<typename T>
45 void EMANE::Wheel<T>::add(std::size_t begin,std::size_t slots,T value)
46 {
47  if(slots > size_ || begin >= size_)
48  {
49  throw IndexError{};
50  }
51 
52  if(!slots)
53  return;
54 
55  std::size_t remainder{};
56 
57  if(begin + slots > size_)
58  {
59  remainder = (begin + slots) % size_;
60  }
61 
62  for(std::size_t i = begin; i < begin + slots - remainder; ++i)
63  {
64  store_[i] += value;
65  }
66 
67  if(remainder)
68  {
69  for(std::size_t i = 0; i < remainder; ++i)
70  {
71  store_[i] += value;
72  }
73  }
74 }
75 
76 template<typename T>
77 void EMANE::Wheel<T>::set(std::size_t begin,std::size_t slots,T value)
78 {
79  if(slots > size_ || begin >= size_)
80  {
81  throw IndexError{};
82  }
83 
84  if(!slots)
85  return;
86 
87  std::size_t remainder{};
88 
89  if(begin + slots > size_)
90  {
91  remainder = (begin + slots) % size_;
92  }
93 
94 
95  for(std::size_t i = begin; i < begin + slots - remainder; ++i)
96  {
97  store_[i] = value;
98  }
99 
100  if(remainder)
101  {
102  for(std::size_t i = 0; i < remainder; ++i)
103  {
104  store_[i] = value;
105  }
106  }
107 }
108 
109 
110 template<typename T>
111 std::vector<T> EMANE::Wheel<T>::get(std::size_t begin,std::size_t slots)
112 {
113  if(slots > size_ || begin >= size_)
114  {
115  throw IndexError{};
116  }
117 
118  std::vector<T> values(slots);
119 
120  if(begin >= slots - 1)
121  {
122  std::memcpy(&values[0],&store_[begin - slots + 1],slots * sizeof(T));
123  }
124  else
125  {
126  std::size_t remainder = slots - begin -1;
127 
128  std::memcpy(&values[0],&store_[size_ - remainder],remainder * sizeof(T));
129 
130  std::memcpy(&values[remainder],&store_[0],(begin + 1) * sizeof(T));
131  }
132 
133  return values;
134 }
135 
136 
137 template<typename T>
138 void EMANE::Wheel<T>::clear(std::size_t begin,std::size_t slots)
139 {
140  if(slots > size_ || begin >= size_)
141  {
142  throw IndexError{};
143  }
144 
145  if(begin >= slots - 1)
146  {
147  std::memset(&store_[begin - slots + 1],0,slots * sizeof(T));
148  }
149  else
150  {
151  std::size_t remainder = slots - begin -1;
152 
153  std::memset(&store_[size_ - remainder],0,remainder * sizeof(T));
154 
155  std::memset(&store_[0],0,(begin + 1) * sizeof(T));
156  }
157 }
void set(std::size_t begin, std::size_t slots, T value)
Definition: wheel.inl:77
std::vector< T > get(std::size_t begin, std::size_t slots)
Definition: wheel.inl:111
Wheel(std::size_t size)
Definition: wheel.inl:34
void add(std::size_t begin, std::size_t slots, T value)
Definition: wheel.inl:45
void clear(std::size_t begin, std::size_t slots)
Definition: wheel.inl:138
const std::vector< T > & dump() const
Definition: wheel.inl:39