2008-01-07 09:09:04 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2004, Valient Gough
|
2012-10-03 07:12:17 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
2013-10-20 00:35:26 +02:00
|
|
|
* option) any later version.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
2012-10-03 07:12:17 +02:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-07 09:09:04 +01:00
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
|
2008-01-07 09:09:04 +01:00
|
|
|
#ifndef _Range_incl_
|
|
|
|
#define _Range_incl_
|
|
|
|
|
2013-03-05 07:32:27 +01:00
|
|
|
#include <ostream>
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
class Range {
|
|
|
|
int minVal;
|
|
|
|
int maxVal;
|
|
|
|
int increment;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Range();
|
|
|
|
Range(int minMax);
|
|
|
|
Range(int min, int max, int increment);
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool allowed(int value) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int closest(int value) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int min() const;
|
|
|
|
int max() const;
|
|
|
|
int inc() const;
|
2008-01-07 09:09:04 +01:00
|
|
|
};
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline std::ostream &operator<<(std::ostream &st, const Range &r) {
|
2013-03-05 07:32:27 +01:00
|
|
|
bool separator = false;
|
|
|
|
for (int size = r.min(); size <= r.max(); size += r.inc()) {
|
|
|
|
if (separator)
|
|
|
|
st << ", ";
|
|
|
|
else
|
|
|
|
separator = true;
|
|
|
|
st << size;
|
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline Range::Range(int minMax) {
|
|
|
|
this->minVal = minMax;
|
|
|
|
this->maxVal = minMax;
|
|
|
|
this->increment = 1;
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline Range::Range(int min_, int max_, int increment_) {
|
|
|
|
this->minVal = min_;
|
|
|
|
this->maxVal = max_;
|
|
|
|
this->increment = increment_;
|
|
|
|
if (increment == 0) this->increment = 1;
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline Range::Range() : minVal(-1), maxVal(-1), increment(1) {}
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline bool Range::allowed(int value) const {
|
|
|
|
if (minVal < 0 && maxVal < 0) return true;
|
|
|
|
if (value >= minVal && value <= maxVal) {
|
|
|
|
int tmp = value - minVal;
|
|
|
|
if ((tmp % increment) == 0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline int Range::closest(int value) const {
|
|
|
|
if (allowed(value))
|
|
|
|
return value;
|
|
|
|
else if (value < minVal)
|
|
|
|
return minVal;
|
|
|
|
else if (value > maxVal)
|
|
|
|
return maxVal;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
// must be inbetween but not matched with increment
|
|
|
|
int tmp = value - minVal;
|
|
|
|
// try rounding to the nearest increment..
|
|
|
|
tmp += (increment >> 1);
|
|
|
|
tmp -= (tmp % increment);
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
return closest(value + tmp);
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline int Range::min() const { return minVal; }
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline int Range::max() const { return maxVal; }
|
|
|
|
|
|
|
|
inline int Range::inc() const { return increment; }
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
} // namespace encfs
|
2008-01-07 09:09:04 +01:00
|
|
|
#endif
|