473,230 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

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 1554
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.