473,382 Members | 1,369 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,382 software developers and data experts.

A simple validity check. But need your kind help!

Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below

class Domain
{
private:
double mLowerCorner[3];
double mUpperCorner[3];

public:
double* GetLowerCorner() {return mLowerCorner;}
double* GetUpperCorner() {return mUpperCorner;}

void SetLowerCorner(double lowerCorner[3])
{
if (mUpperCorner[0] < lowerCorner[0] || // Validity check
mUpperCorner[1] < lowerCorner[1] ||
mUpperCorner[2] < lowerCorner[2] )
{
throw out_of_range("Error");
}
mLowerCorner[0] = lowerCorner[0];
mLowerCorner[1] = lowerCorner[1];
mLowerCorner[2] = lowerCorner[2];
}

// My question is here: I need similar functions
// void SetUpperCorner(double upperCorner[3]),
// void SetDomain(double lowerCorner[3], double upperCorner[3]),
// and constructors.
// and so on. The check make me very uncomfortable.
};

Anyone can give me a good suggestion to solve the problem?

Thanks a lot!

Shuisheng

Feb 16 '07 #1
2 1900
shuisheng wrote:
Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below

class Domain
{
private:
double mLowerCorner[3];
double mUpperCorner[3];

public:
double* GetLowerCorner() {return mLowerCorner;}
double* GetUpperCorner() {return mUpperCorner;}
These methods expose the arrays. Client code could say:

(GetLowerCorner())[1] = 2.0;

and change the entries. In that case, the client will bypass all validity
checks whatsoever. At least, you need:

double const * GetLowerCorner() const {return mLowerCorner;}
double const * GetUpperCorner() const {return mUpperCorner;}
void SetLowerCorner(double lowerCorner[3])
{
if (mUpperCorner[0] < lowerCorner[0] || // Validity check
mUpperCorner[1] < lowerCorner[1] ||
mUpperCorner[2] < lowerCorner[2] )
{
throw out_of_range("Error");
}
mLowerCorner[0] = lowerCorner[0];
mLowerCorner[1] = lowerCorner[1];
mLowerCorner[2] = lowerCorner[2];
}

// My question is here: I need similar functions
// void SetUpperCorner(double upperCorner[3]),
// void SetDomain(double lowerCorner[3], double upperCorner[3]),
// and constructors.
// and so on. The check make me very uncomfortable.
};
What should make you uncomfortable is not the checks but the sheer
complexity you are inviting just to deal with two points in space.

What about making domains immutable and putting a little more intelligence
into the points:

#include <tr1/array>
#include <stdexcept>

// warning: [bad hack, there is no guarantee that we can derive from array]
/*
(a) We use derivation rather than a typedef so that operator- only applies
to this type.
(b) Some people will prefer to derive privately and import all methods via
using directives.
*/
struct vector3d : public std::tr1::array<double,3{

vector3d ( double x = 0.0,
double y = 0.0,
double z = 0.0 )
{
(*this)[0] = x;
(*this)[1] = y;
(*this)[2] = z;
}

};

vector3d operator- ( vector3d const & lhs, vector3d const & rhs ) {
vector3d result;
for ( unsigned i = 0; i < 3; ++i ) {
result[i] = lhs[i] - rhs[i];
}
return ( result );
}
bool is_strictly_positive ( vector3d const & v ) {
for ( unsigned i = 0; i < 3; ++i ) {
if ( v[i] <= 0 ) {
return ( false );
}
}
return ( true );
}

bool does_define_box ( vector3d const & bot,
vector3d const & top ) {
return ( is_strictly_positive( top - bot ) );
}

class immutableDomain {

vector3d bot;
vector3d top;

public:

immutableDomain ( vector3d const & b, vector3d t )
: bot(b)
, top(t)
{
if ( ! does_define_box( bot, top ) ) {
throw std::out_of_range( "Error" );
}
}

vector3d const & lowerCorner ( void ) const {
return ( bot );
}

vector3d const & upperCorner ( void ) const {
return ( top );
}

};

If you want to, you can define a mutable Domain class wrapping around the
immutable one:

class Domain {

immutableDomain the_data;

public:

Domain ( vector3d const & b, vector3d const & t )
: the_data( b, t )
{}

vector3d const & getLowerCorner ( void ) const {
return ( the_data.lowerCorner() );
}

vector3d const & getUpperCorner ( void ) const {
return ( the_data.upperCorner() );
}

void setLowerCorner ( vector3d const & bot ) {
the_data = immutableDomain( bot, the_data.upperCorner() );
}

void setUpperCorner ( vector3d const & top ) {
the_data = immutableDomain( the_data.lowerCorner(), top );
}

};

The constructor of immutableDomain will be the only place where the
invariant is checked.
Best

Kai-Uwe Bux
Feb 16 '07 #2
In article <11**********************@v45g2000cwv.googlegroups .com>,
sh*********@yahoo.com says...
Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below
I'd add private functions to check the condition, and do the assignment.
Using this, the rest become pretty trivial:

class Domain {
double mLowerCorner[3];
double mUpperCorner[3];

bool check(double *lower, double *upper) {
if (upper[0] < lower[0] ||
upper[1] < lower[1] ||
upper[2] < lower[2])
throw out_of_range("Error");
}

void assign(double &a, double const &b) {
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
}

public:
void SetLowerCorner(double lower[3]) {
check(lower, mUpperCorner);
assign(mLowerCorner, lower);
}

void SetUpperCorner(double upper[3]) {
check(mLowerCorner, upper);
assign(mUpperCorner, upper);
}

void SetDomain(double *lower, double *upper) {
check(lower, upper);
assign(mLowerCorner, lower);
assign(mUpperCorner, upper);
}

Domain(double *lower, double *upper) {
SetDomain(lower, upper);
}
// ...
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 16 '07 #3

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

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I donīt know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
33
by: a | last post by:
Hi, I have a pointer that points to an unknown heap memory block, is it possible to check the pointer + 3 is valid or not? If it is impossible, how can I do the check? Thanks
9
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r");...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
6
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with...
4
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.