473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ranged Integers

is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?

Sep 2 '07 #1
9 1558
In article <11**********************@o80g2000hse.googlegroups .com>,
fo*********@yahoo.com says...
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?
This might fill the bill:

#include <exception>
#include <iostream>
#include <functional>

template <class T, class less=std::less<T
class bounded {
const T lower_, upper_;
T val_;

bool check(T const &value) {
return less()(value, lower_) || less()(upper_, value);
}

void assign(T const &value) {
if (check(value))
throw std::domain_error("Out of Range");
val_ = value;
}

public:
bounded(T const &lower, T const &upper)
: lower_(lower), upper_(upper) {}

bounded(bounded const &init) { assign(init); }

bounded &operator=(T const &v) { assign(v); return *this; }

operator T() const { return val_; }

friend std::istream &operator>>(std::istream &is, bounded &b) {
T temp;
is >temp;

if (b.check(temp))
is.setstate(std::ios::failbit);
else
b.val_ = temp;
return is;
}
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 2 '07 #2
On Sep 3, 2:06 am, foothomp...@yahoo.com wrote:
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?
Hi

I think the solution by Jerry is good and sophisticated. But a simpler
version of range concept that I usually use, is a simple rangle class
offered by Bjarne Stroustrup in his seminars:
// Range concept (for integers)
class Range { // simple value type
int value, low, high; // invariant: low <= value < high
check(int v) { if (!(low<=v && v<high)) throw Range_error(); }
public:
Range(int lo, int v, int hi) : low(lo), value(v), high(hi)
{ check(v); }
Range& operator=(const Range& a) { check(a.v); value = a.value;
return *this; }
Range& operator=(int a) { check(a); value = a; return *this; }
operator int() { return value; }
};

Regards,
Saeed Amrollahi

Sep 3 '07 #3
On Sep 3, 4:47 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <1188774365.772466.314...@o80g2000hse.googlegroups .com>,
foothomp...@yahoo.com says...
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?

This might fill the bill:

#include <exception>
#include <iostream>
#include <functional>

template <class T, class less=std::less<T
class bounded {
const T lower_, upper_;
T val_;

bool check(T const &value) {
return less()(value, lower_) || less()(upper_, value);
}

void assign(T const &value) {
if (check(value))
throw std::domain_error("Out of Range");
val_ = value;
}

public:
bounded(T const &lower, T const &upper)
: lower_(lower), upper_(upper) {}

bounded(bounded const &init) { assign(init); }
doubt: for copy c'tor here, will the lower_ & upper_ get initialized?
bounded &operator=(T const &v) { assign(v); return *this; }

operator T() const { return val_; }

friend std::istream &operator>>(std::istream &is, bounded &b) {
T temp;
is >temp;

if (b.check(temp))
is.setstate(std::ios::failbit);
else
b.val_ = temp;
return is;
}

};

--
Later,
Jerry.

The universe is a figment of its own imagination.

Sep 3 '07 #4
bu******@googlemail.com wrote:
>[snip]

void assign(T const &value) {
if (check(value))
throw std::domain_error("Out of Range");
val_ = value;
}

public:
bounded(T const &lower, T const &upper)
: lower_(lower), upper_(upper) {}

bounded(bounded const &init) { assign(init); }

doubt: for copy c'tor here, will the lower_ & upper_ get initialized?
No. Since lower_ and upper_ are declared const you cannot assign them any
values. However, you can initialize them in the copy-constructor, like so:

bounded(const bounded& init)
: lower_(init.lower_), upper_(init.upper_)
{assign(init); }

[snip]
Sep 3 '07 #5
On Sun, 2 Sep 2007 17:47:13 -0600, Jerry Coffin <jc*****@taeus.com>
wrote:
>In article <11**********************@o80g2000hse.googlegroups .com>,
fo*********@yahoo.com says...
>is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?

This might fill the bill:
<...>

Or, pressing it harder:

#include <exception>
#include <iostream>
#include <functional>

template <class T, T lower, T higher, class less=std::less<T
class bounded {
T val_;

bool check(T const &value) {
return less()(value, lower) || less()(upper, value);
}

void assign(T const &value) {
if (check(value))
throw std::domain_error("Out of Range");
val_ = value;
}

public:
explicit bounded(T val) {assign(val);}

bounded() :val_(
less()(T(),lower)?lower:
less()(higher,T())?higher:T()) {}

bounded(bounded const &init):val_(init.val_) {}

template<T1,T1 other_lower,T1 other_higher,less1>
bounded(
bounded<T1,other_lower,other_higher,less1>
const &init) {assign(init.val_)}

bounded &operator=(T const &v) { assign(v); return *this; }

bounded &operator=(bounded const &v) {val_=v.val_; return *this;}

template<T1,T1 other_lower,T1 other_higher,less1>
bounded& operator=(
bounded<T1,other_lower,other_higher,less1>
const &v) {assign(v.val_)}

operator T() const { return val_; }

friend std::istream &operator>>(std::istream &is, bounded &b) {
T temp;
is >temp;

if (b.check(temp))
is.setstate(std::ios::failbit);
else
b.val_ = temp;
return is;
}
};

Just to have different types for different ranges, and faster
assignment/copy when maintining range.

Regards,

Zara
Sep 3 '07 #6
In article <11*********************@50g2000hsm.googlegroups.c om>,
bu******@googlemail.com says...

[ ... ]
bounded(bounded const &init) { assign(init); }

doubt: for copy c'tor here, will the lower_ & upper_ get initialized?
Oops -- no, they won't. That's definitely a bug -- thanks for pointing
it out.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 3 '07 #7
In article <qj********************************@4ax.com>,
me*****@dea.spamcon.org says...

[ ... ]
template <class T, T lower, T higher, class less=std::less<T
Yeah, I've played with one like that as well. As you note, it does
improve efficiency, and for the exact question that was asked, it works
quite well. The one major shortcoming is that it can't be applied to
floating point types.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 3 '07 #8
On Sep 3, 2:06 am, foothomp...@yahoo.com wrote:
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?
template<typename Numeric,Numeric low,Numeric up>
struct bounded{
typedef Numeric value_type;
enum{lower=low,upper=up};//no runtime overhead
operator value_type&()const{return value;};
explicit bounded(const Numeric& i);
const value_type& operator=(const Numeric& r);
.....//etc
private:
Numeric value;
};

regards,
FM.

Sep 3 '07 #9
On Sep 3, 8:42 pm, terminator <farid.mehr...@gmail.comwrote:
On Sep 3, 2:06 am, foothomp...@yahoo.com wrote:
is there a simple integer type library around that behaves like normal
integers, i.e has all the same operations, but except the value can
only be in a specific range, like 0 to 100, and if the value is
outside of this range, it will throw an exception or assert fail?

template<typename Numeric,Numeric low,Numeric up>
struct bounded{
typedef Numeric value_type;
enum{lower=low,upper=up};//no runtime overhead
operator value_type&()const{return value;};
explicit bounded(const Numeric& i);
const value_type& operator=(const Numeric& r);
.....//etc
private:
Numeric value;

};

regards,
FM.
that works with integrals for none-integral numerics(float/double...):

template<typename Numeric,Numeric low,Numeric up>//no memory overhead
for bounds
struct bounded_real{
typedef Numeric value_type;
static const value_type& lower(){
static const value_type data=low;
};
static const value_type& upper(){
static const value_type data=up;
};
...//etc
};

Sep 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
0
by: christopher diggins | last post by:
I have written a constrained_value type for things like range checking integers, non-negative doubles, etc. and I have made the source code freely available for all at...
4
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
1
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
10
Cyberdyne
by: Cyberdyne | last post by:
Here is the problem, I have a form with a field named Occurence with a Short Date Value, once entered it subsequently appears in 3 fields SOL1 which adds one year, SOL2 which adds 2 years and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.