[constrained] constrained_vector

I'm interested in something like constrained, but applied to a vector of values. The case I'm interested in is a homogeneous vector of bounded int values, with bounds set at runtime. It is trivial to have a vector<bounded<int> >, but this would not be space efficient in the case where the policies, bounds on all the values are the same. I'm thinking that maybe some parts of constrained (such as policies) could be reused, independent of the rest of constrained. Has anyone looked into the idea of constrained vector?

On Dec 22, 2008, at 1:47 PM, Neal Becker wrote:
I'm interested in something like constrained, but applied to a vector of values. The case I'm interested in is a homogeneous vector of bounded int values, with bounds set at runtime.
It is trivial to have a vector<bounded<int> >, but this would not be space efficient in the case where the policies, bounds on all the values are the same.
Not as general, but possible right now: use bounded with the bounds as static members. This gives you as many classes of constraints as you care to spell out. Example follows. You're right though, a way to impart constraints from container to contained would be really cool. #include <iostream> #include <boost/constrained_value.hpp> #include <boost/mpl/assert.hpp> template<typename Tag, typename T> struct static_bound { operator T() const { return s_t; } static T s_t; }; struct Lower {}; struct Upper {}; template<> double static_bound<Lower, double>::s_t = -10.0; template<> double static_bound<Upper, double>::s_t = 10.0; int main(int argc, char *argv[]) { typedef boost::constrained_value::bounded<double, static_bound<Lower,double>, static_bound<Upper,double> >::type within_10; // there have been reports that this will fail on msvc? worx on gcc BOOST_MPL_ASSERT_RELATION( sizeof(within_10), ==, sizeof(double) ); within_10 x; x = 9; x = -7.5; try { x = 12; } catch(std::exception const& xep) { std::cout << "expect except: " << xep.what() << std::endl; } return 0; } Cheers, Gordon

Gordon Woodhull wrote:
On Dec 22, 2008, at 1:47 PM, Neal Becker wrote:
I'm interested in something like constrained, but applied to a vector of values. The case I'm interested in is a homogeneous vector of bounded int values, with bounds set at runtime.
It is trivial to have a vector<bounded<int> >, but this would not be space efficient in the case where the policies, bounds on all the values are the same.
Not as general, but possible right now: use bounded with the bounds as static members. This gives you as many classes of constraints as you care to spell out. Example follows.
Interesting example. IIUC, though, what I need is run-time bounds and the example is compile-time.

On Dec 23, 2008, at 9:20 AM, Neal Becker wrote:
Interesting example. IIUC, though, what I need is run-time bounds and the example is compile-time.
It is runtime in that it's a static field which you could change at runtime, which is shared by all uses of that bounds type. But presumably you would want change_*_bound to recheck all items in the container to make sure that the new bound is valid, and there is no sensible way to do that with this technique. (I can think of some ridiculous ways.) Clearly there is still plenty of room for generalization of the concepts introduced by this library: associating constraints with groups or classes (English not C++ definition) of values is going to be way more natural and efficient. Gordon

Hi,
From: Neal Becker
I'm interested in something like constrained, but applied to a vector of values. The case I'm interested in is a homogeneous vector of bounded int values, with bounds set at runtime.
It is trivial to have a vector<bounded<int> >, but this would not be space efficient in the case where the policies, bounds on all the values are the same.
Maybe you could use the policy described here: http://lists.boost.org/boost-users/2008/12/42959.php It will let you set the bounds at runtime, but not change them once any object using this policy is created. However, it will have no space overhead on the individual objects. Regards, Robert
participants (3)
-
Gordon Woodhull
-
Neal Becker
-
Robert Kawulak