design question regarding multiple update

Discussion in 'C++' started by puzzlecracker, Sep 11, 2008.

  1. 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
     
    puzzlecracker, Sep 11, 2008
    #1
    1. Advertising

  2. puzzlecracker

    Puppet_Sock Guest

    On Sep 11, 11:05 am, puzzlecracker <> wrote:
    > 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
     
    Puppet_Sock, Sep 11, 2008
    #2
    1. Advertising

  3. On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    > On Sep 11, 11:05 am, puzzlecracker <> wrote:
    >
    >
    >
    > > 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


    How to design this sort architecture? I thought it was straightforward
    from my description as well as subject line.
     
    puzzlecracker, Sep 11, 2008
    #3
  4. puzzlecracker

    anon Guest

    puzzlecracker wrote:
    > On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    >> On Sep 11, 11:05 am, puzzlecracker <> wrote:
    >>
    >>
    >>
    >>> 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

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


    Like this:
    stuct Updater{
    int _v1;
    int _v2
    int _v3;

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

    You only need to implement update and isBad methods.

    This might help you solve the problem :
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
     
    anon, Sep 11, 2008
    #4
  5. On Sep 11, 12:39 pm, anon <> wrote:
    > puzzlecracker wrote:
    > > On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    > >> On Sep 11, 11:05 am, puzzlecracker <> wrote:

    >
    > >>> 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

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

    >
    > Like this:
    > stuct Updater{
    >    int _v1;
    >    int  _v2
    >    int  _v3;
    >
    > void update(int v1, int v2, int v2);
    > bool isBad(int val);
    >
    > };
    >
    > You only need to implement update and isBad methods.
    >
    > This might help you solve the problem :http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2


    dude, I am looking for a design pattern for this sort of problem,
    not a jerk-like response.
     
    puzzlecracker, Sep 11, 2008
    #5
  6. puzzlecracker

    Zeppe Guest

    puzzlecracker wrote:
    > On Sep 11, 12:39 pm, anon <> wrote:
    >> puzzlecracker wrote:
    >>> On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    >>>> On Sep 11, 11:05 am, puzzlecracker <> wrote:
    >>>>> 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
    >>> How to design this sort architecture? I thought it was straightforward
    >>> from my description as well as subject line.

    >> Like this:
    >> stuct Updater{
    >> int _v1;
    >> int _v2
    >> int _v3;
    >>
    >> void update(int v1, int v2, int v2);
    >> bool isBad(int val);
    >>
    >> };
    >>
    >> You only need to implement update and isBad methods.
    >>
    >> This might help you solve the problem :http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2

    >
    > 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
     
    Zeppe, Sep 11, 2008
    #6
  7. On Sep 11, 12:47 pm, Zeppe
    <> wrote:
    > puzzlecracker wrote:
    > > On Sep 11, 12:39 pm, anon <> wrote:
    > >> puzzlecracker wrote:
    > >>> On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    > >>>> On Sep 11, 11:05 am, puzzlecracker <> wrote:
    > >>>>> 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
    > >>> How to design this sort architecture? I thought it was straightforward
    > >>> from my description as well as subject line.
    > >> Like this:
    > >> stuct Updater{
    > >>    int _v1;
    > >>    int  _v2
    > >>    int  _v3;

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

    >
    > >> };

    >
    > >> You only need to implement update and isBad methods.

    >
    > >> This might help you solve the problem :http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2

    >
    > > 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


    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
     
    puzzlecracker, Sep 11, 2008
    #7
  8. puzzlecracker wrote:
    >>> 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

    >
    > 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.

    --
    Gennaro Prota | name.surname yahoo.com
    Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
    Do you need expertise in C++? I'm available.
     
    Gennaro Prota, Sep 11, 2008
    #8
  9. puzzlecracker a écrit :
    > On Sep 11, 12:47 pm, Zeppe
    > <> wrote:
    >> puzzlecracker wrote:
    >>> On Sep 11, 12:39 pm, anon <> wrote:
    >>>> puzzlecracker wrote:
    >>>>> On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    >>>>>> On Sep 11, 11:05 am, puzzlecracker <> wrote:
    >>>>>>> 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
    >>>>> How to design this sort architecture? I thought it was straightforward
    >>>>> from my description as well as subject line.
    >>>> Like this:
    >>>> stuct Updater{
    >>>> int _v1;
    >>>> int _v2
    >>>> int _v3;
    >>>> void update(int v1, int v2, int v2);
    >>>> bool isBad(int val);
    >>>> };
    >>>> You only need to implement update and isBad methods.
    >>>> This might help you solve the problem :http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
    >>> 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

    >
    > 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;
    }

    --
    Michael
     
    Michael DOUBEZ, Sep 12, 2008
    #9
  10. On Sep 12, 5:05 am, Michael DOUBEZ <> wrote:
    > puzzlecracker a écrit :
    >
    >
    >
    > > On Sep 11, 12:47 pm, Zeppe
    > > <> wrote:
    > >> puzzlecracker wrote:
    > >>> On Sep 11, 12:39 pm, anon <> wrote:
    > >>>> puzzlecracker wrote:
    > >>>>> On Sep 11, 12:13 pm, Puppet_Sock <> wrote:
    > >>>>>> On Sep 11, 11:05 am, puzzlecracker <> wrote:
    > >>>>>>> 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
    > >>>>> How to design this sort architecture? I thought it was straightforward
    > >>>>> from my description as well as subject line.
    > >>>> Like this:
    > >>>> stuct Updater{
    > >>>> int _v1;
    > >>>> int _v2
    > >>>> int _v3;
    > >>>> void update(int v1, int v2, int v2);
    > >>>> bool isBad(int val);
    > >>>> };
    > >>>> You only need to implement update and isBad methods.
    > >>>> This might help you solve the problem :http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
    > >>> 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

    >
    > > 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;
    >
    > }
    >
    > --
    > Michael



    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;
    }
     
    puzzlecracker, Sep 14, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=

    Design question regarding use of Gridview

    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 6, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    405
    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=
    Feb 6, 2007
  2. Rob Meade

    Question regarding oo design / layers

    Rob Meade, Jun 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    345
    Rob Meade
    Jun 22, 2007
  3. asincero
    Replies:
    2
    Views:
    239
    Bruno Desthuilliers
    Jul 18, 2007
  4. Angus
    Replies:
    3
    Views:
    340
    H. S. Lahman
    Jan 4, 2009
  5. Robert Chapman

    Design question regarding use of Gridview

    Robert Chapman, Feb 6, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    143
    Robert Chapman
    Feb 6, 2007
Loading...

Share This Page