design question regarding multiple update

P

puzzlecracker

Say you have a class:

stuct Updater{
int _v1;
int _v2
int _v3;

update(int v1, int v2, int v2);
bool isBad(int val);
};


Each time update is called, you want to update all the members
respectively, However, if you one of the v's is bad, you try other
vals, if they are good, to update it.

Say v1 is bad, then you try set it to v2, if v2 is bad, then you set
it v3, otherwise, you set it to 0.

The same for v2, and v3 respectively.


Thanks
 
P

Puppet_Sock

Say you have a class:

stuct Updater{
  int _v1;
  int  _v2
  int  _v3;

update(int v1, int v2, int v2);
bool isBad(int val);

};

Each time update is called, you want to update all the members
respectively, However, if you one of the v's is bad, you try other
vals, if they are good, to update it.

Say v1 is bad, then you try set it to v2, if v2 is bad, then you set
it v3, otherwise, you set it to 0.

The same for v2, and v3 respectively.

Thanks

Ok, did you have a question?
Socks
 
P

puzzlecracker

Ok, did you have a question?
Socks

How to design this sort architecture? I thought it was straightforward
from my description as well as subject line.
 
Z

Zeppe

puzzlecracker said:
dude, I am looking for a design pattern for this sort of problem,
not a jerk-like response.

The pattern is sitting in front of a PC with a C++ book and study. This
is at least the second exercise you post in the newsgroup, don't expect
to have easy time in finding people that let you dodge your homework.

Best wishes,

Zeppe
 
P

puzzlecracker

The pattern is sitting in front of a PC with a C++ book and study. This
is at least the second exercise you post in the newsgroup, don't expect
to have easy time in finding people that let you dodge your homework.

Best wishes,

Zeppe

Funny, really.... I am actually working professional and this is a
simple version of a problem I am working on... done with school, many,
many years ago... For now, I use lots of ifs to solve it, and I think
there is a general pattern.

Anyway
 
G

Gennaro Prota

puzzlecracker said:
Funny, really.... I am actually working professional and this is a
simple version of a problem I am working on... done with school, many,
many years ago... For now, I use lots of ifs to solve it, and I think
there is a general pattern.

"Professional" is a really strong word.
 
M

Michael DOUBEZ

puzzlecracker a écrit :
Funny, really.... I am actually working professional and this is a
simple version of a problem I am working on... done with school, many,
many years ago... For now, I use lots of ifs to solve it, and I think
there is a general pattern.

You can define a function that returns the first value valid and a
fallback otherwise:

int getFirstValid(int a, int b, int c,int fallback)
{
return isBad(a)?isBad(b)?isBad(c)?
fallback:c:b:a;
}

And then:
void update(int v1, int v2, int v2)
{
_v1=getFirstValid(v1,v2,v3,0);
_v2=getFirstValid(v2,v3,v1,0);
_v3=getFirstValid(v3,v1,v2,0);
}

If it bothers you to test multiple times the validity of a value, there
is a more obfuscated version with any number of parameter:

void update(int v1, int v2, int v3,...,v42)
{
//computing first value valid in range
const int after_v41=isBad(v41)?v42:v41;
const int after_v40=isBad(v40)?after_v41:v40;
...
const int after_v1 =isBad(1)?after_2:v1;

//values valid ?

if(isBad(after_v1))
{ //all parameters are bad
//set to fallback
_v1=_v2=_v3=..=_v42=0;
retun;
}


_v1=after_v1;
_v2=isBad(after_v2)?after_v1:after_v2;
_v3=isBad(after_v3)?after_v1:after_v3;
...
//here you can even gain some comparison
// as soon as sBad(after_v3)=true,
//all values vX-v42 takes value after_v1
...
_v42=isBad(42)?after_v1:v42;
}
 
P

puzzlecracker

puzzlecracker a écrit :





You can define a function that returns the first value valid and a
fallback otherwise:

int getFirstValid(int a, int b, int c,int fallback)
{
return isBad(a)?isBad(b)?isBad(c)?
fallback:c:b:a;

}

And then:
void update(int v1, int v2, int v2)
{
_v1=getFirstValid(v1,v2,v3,0);
_v2=getFirstValid(v2,v3,v1,0);
_v3=getFirstValid(v3,v1,v2,0);

}

If it bothers you to test multiple times the validity of a value, there
is a more obfuscated version with any number of parameter:

void update(int v1, int v2, int v3,...,v42)
{
//computing first value valid in range
const int after_v41=isBad(v41)?v42:v41;
const int after_v40=isBad(v40)?after_v41:v40;
...
const int after_v1 =isBad(1)?after_2:v1;

//values valid ?

if(isBad(after_v1))
{ //all parameters are bad
//set to fallback
_v1=_v2=_v3=..=_v42=0;
retun;
}

_v1=after_v1;
_v2=isBad(after_v2)?after_v1:after_v2;
_v3=isBad(after_v3)?after_v1:after_v3;
...
//here you can even gain some comparison
// as soon as sBad(after_v3)=true,
//all values vX-v42 takes value after_v1
...
_v42=isBad(42)?after_v1:v42;

}


Thanks, I found a simpler solution:


Updater::update(int v1, int v2, int v2)
{
bool a= isbad(v1),
b= isBad(v2),
c= isbad(v3);

_v1 = a ? (b ? (c ? 0 : v3) : v2) : v1;
_v2 = b ? (a ? (c ? 0 : v3) : v1) : v2;
_v3 = c ? (a ? (b ? 0 : v2) : v1) : v3;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top