Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > subset of a POD

Reply
Thread Tools

subset of a POD

 
 
Hicham Mouline
Guest
Posts: n/a
 
      12-08-2009
Hello,

I have functions that each take 1 parameter. This parameter is of type

struct S {
<arithmetic_or_bool_type_1> member_name_1;
<arithmetic_or_bool_type_n> member_name_n;
}; // n can be of order 10 or so

class C {
public:
return_type apply( const S& ) const;
};

Now I want to optimize the returned value.

I would optimize by fixing any subset of the n members fixed, ie none at
all, or just member 5, or members 4 and 8, or members 7,3 and n, or n-1
members of the {1....n} set.

How can I do this?

maybe

class C{
public:
pair<return_type, S> optimize(...) const; // returns the optimum S
that gives the best return value
};

Some interface like this looks nice to me (as a user).

The question is how to pass the fixed members to optimize.

regards,


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-09-2009
Hicham Mouline wrote:
> I have functions that each take 1 parameter. This parameter is of type
>
> struct S {
> <arithmetic_or_bool_type_1> member_name_1;
> <arithmetic_or_bool_type_n> member_name_n;
> }; // n can be of order 10 or so
>
> class C {
> public:
> return_type apply( const S& ) const;
> };
>
> Now I want to optimize the returned value.


"Optimize"?

> I would optimize by fixing any subset of the n members fixed, ie none at
> all, or just member 5, or members 4 and 8, or members 7,3 and n, or n-1
> members of the {1....n} set.
>
> How can I do this?


If your 'return_type' has to be the same *type*, there is no better
solution than to return an 'S', if the members that you don't want to
"return" are set to some "invalid" value. As an alternative you could
consider supplying another member to 'S', which would indicate the
"valid" or "active" members. You can have another (nested) struct in
'S' with bit fields:

struct S {
... // as you have now
struct {
unsigned use_1:1;
unsigned use_2:1;
...
unsigned use_n:1;
} which_members_to_use;
};

>
> maybe
>
> class C{
> public:
> pair<return_type, S> optimize(...) const; // returns the optimum S
> that gives the best return value


What's "return_type" here? What does it mean for the return value to be
"the best"?

> };
>
> Some interface like this looks nice to me (as a user).
>
> The question is how to pass the fixed members to optimize.


It is unclear what solution would be better since you've not explained
what problem you're trying to solve with this mechanism.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      12-09-2009
Victor Bazarov wrote:
> Hicham Mouline wrote:
>
>> I would optimize by fixing any subset of the n members fixed, ie none
>> at all, or just member 5, or members 4 and 8, or members 7,3 and n, or
>> n-1 members of the {1....n} set.
>>
>> How can I do this?

>
> If your 'return_type' has to be the same *type*, there is no better
> solution than to return an 'S', if the members that you don't want to


Maybe returning "const S&" would be better?

> "return" are set to some "invalid" value. As an alternative you could
> consider supplying another member to 'S', which would indicate the
> "valid" or "active" members. You can have another (nested) struct in
> 'S' with bit fields:
>
> struct S {
> ... // as you have now
> struct {
> unsigned use_1:1;
> unsigned use_2:1;
> ...
> unsigned use_n:1;
> } which_members_to_use;
> };
>


Using "bool use_1" might be clearer then "unsigned use_1:1".


--
ultrasound www.ezono.com
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-09-2009
Vladimir Jovic wrote:
> Victor Bazarov wrote:
>> Hicham Mouline wrote:
>>
>>> I would optimize by fixing any subset of the n members fixed, ie none
>>> at all, or just member 5, or members 4 and 8, or members 7,3 and n,
>>> or n-1 members of the {1....n} set.
>>>
>>> How can I do this?

>>
>> If your 'return_type' has to be the same *type*, there is no better
>> solution than to return an 'S', if the members that you don't want to

>
> Maybe returning "const S&" would be better?


And what would it refer to?

>
>> "return" are set to some "invalid" value. As an alternative you could
>> consider supplying another member to 'S', which would indicate the
>> "valid" or "active" members. You can have another (nested) struct in
>> 'S' with bit fields:
>>
>> struct S {
>> ... // as you have now
>> struct {
>> unsigned use_1:1;
>> unsigned use_2:1;
>> ...
>> unsigned use_n:1;
>> } which_members_to_use;
>> };
>>

>
> Using "bool use_1" might be clearer then "unsigned use_1:1".


Clearer, maybe. More expensive, for sure.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      12-10-2009
Victor Bazarov wrote:
> Vladimir Jovic wrote:
>> Victor Bazarov wrote:
>>> Hicham Mouline wrote:
>>>
>>>> I would optimize by fixing any subset of the n members fixed, ie
>>>> none at all, or just member 5, or members 4 and 8, or members 7,3
>>>> and n, or n-1 members of the {1....n} set.
>>>>
>>>> How can I do this?
>>>
>>> If your 'return_type' has to be the same *type*, there is no better
>>> solution than to return an 'S', if the members that you don't want to

>>
>> Maybe returning "const S&" would be better?

>
> And what would it refer to?
>


You should know the answer
A static variable, or a class member holding the result.

>>
>>> "return" are set to some "invalid" value. As an alternative you
>>> could consider supplying another member to 'S', which would indicate
>>> the "valid" or "active" members. You can have another (nested)
>>> struct in 'S' with bit fields:
>>>
>>> struct S {
>>> ... // as you have now
>>> struct {
>>> unsigned use_1:1;
>>> unsigned use_2:1;
>>> ...
>>> unsigned use_n:1;
>>> } which_members_to_use;
>>> };
>>>

>>
>> Using "bool use_1" might be clearer then "unsigned use_1:1".

>
> Clearer, maybe. More expensive, for sure.
>


So, next two structures are not the same?
struct A
{
bool use1;
bool use2;
//... more bool
bool use32;
}

struct B
{
unsigned use1:1;
unsigned use2:1;
//... more unsigned:1;
unsigned use32:1;
}

Or, is bool expanded to 8 bits, or something different then 1 bit in
this case?

--
ultrasound www.ezono.com
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-10-2009
On Dec 9, 7:15 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Vladimir Jovic wrote:
> > Victor Bazarov wrote:
> >> Hicham Mouline wrote:


[...]
> >> "return" are set to some "invalid" value. As an alternative you could
> >> consider supplying another member to 'S', which would indicate the
> >> "valid" or "active" members. You can have another (nested) struct in
> >> 'S' with bit fields:


> >> struct S {
> >> ... // as you have now
> >> struct {
> >> unsigned use_1:1;
> >> unsigned use_2:1;
> >> ...
> >> unsigned use_n:1;
> >> } which_members_to_use;
> >> };


> > Using "bool use_1" might be clearer then "unsigned use_1:1".


> Clearer, maybe. More expensive, for sure.


More expensive in memory. Probably significantly less expensive
in runtime.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-10-2009
On Dec 10, 8:26 am, Vladimir Jovic <vladasp...@gmail.com> wrote:
> Victor Bazarov wrote:


[...]
> So, next two structures are not the same?
> struct A
> {
> bool use1;
> bool use2;
> //... more bool
> bool use32;
> }


> struct B
> {
> unsigned use1:1;
> unsigned use2:1;
> //... more unsigned:1;
> unsigned use32:1;
> }


> Or, is bool expanded to 8 bits, or something different then 1
> bit in this case?


bool must be addressable; you can have bool*, and in the first
struct, you can take the address of each of the bool's. And
sizeof(bool) must be an integral value. This imposes a minimum
size equal to that of char. (The actual size is implementation
defined, and it wouldn't surprise me if there were
implementations where bool was the same size as an int.)

A variable declared as a bit field doesn't have an address,
i.e.:

A* pa;
B* pb;
bool* b1 = &pa->use1; // legal...
bool* b2 = &pb->use1; // illegal...

--
James Kanze
 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      12-10-2009
James Kanze wrote:
> On Dec 10, 8:26 am, Vladimir Jovic <vladasp...@gmail.com> wrote:
>> Or, is bool expanded to 8 bits, or something different then 1
>> bit in this case?

>
> bool must be addressable; you can have bool*, and in the first
> struct, you can take the address of each of the bool's. And
> sizeof(bool) must be an integral value. This imposes a minimum
> size equal to that of char. (The actual size is implementation
> defined, and it wouldn't surprise me if there were
> implementations where bool was the same size as an int.)


Great. Thanks for the explanation. Who would have thought of that


--
ultrasound www.ezono.com
 
Reply With Quote
 
Hicham Mouline
Guest
Posts: n/a
 
      12-10-2009

"Victor Bazarov" <> wrote in message
news:hfohnd$vvh$...
> Hicham Mouline wrote:
>> I have functions that each take 1 parameter. This parameter is of type
>>
>> struct S {
>> <arithmetic_or_bool_type_1> member_name_1;
>> <arithmetic_or_bool_type_n> member_name_n;
>> }; // n can be of order 10 or so
>>
>> class C {
>> public:
>> return_type apply( const S& ) const;
>> };
>>
>> Now I want to optimize the returned value.

>
> "Optimize"?
>
>> I would optimize by fixing any subset of the n members fixed, ie none at
>> all, or just member 5, or members 4 and 8, or members 7,3 and n, or n-1
>> members of the {1....n} set.
>>
>> How can I do this?

>
> If your 'return_type' has to be the same *type*, there is no better
> solution than to return an 'S', if the members that you don't want to
> "return" are set to some "invalid" value. As an alternative you could
> consider supplying another member to 'S', which would indicate the "valid"
> or "active" members. You can have another (nested) struct in 'S' with bit
> fields:
>
> struct S {
> ... // as you have now
> struct {
> unsigned use_1:1;
> unsigned use_2:1;
> ...
> unsigned use_n:1;
> } which_members_to_use;
> };
>
>>
>> maybe
>>
>> class C{
>> public:
>> pair<return_type, S> optimize(...) const; // returns the optimum S
>> that gives the best return value

>
> What's "return_type" here? What does it mean for the return value to be
> "the best"?
>
>> };
>>
>> Some interface like this looks nice to me (as a user).
>>
>> The question is how to pass the fixed members to optimize.

>
> It is unclear what solution would be better since you've not explained
> what problem you're trying to solve with this mechanism.
>
> V

I apologize for being unclear.

return_type is just double.

C c;
S s1;
c.apply( s1 ); //// returns double , say 15.0

By optimize, I mean in most cases maximize, but sometimes minimize.
So I'm trying to get the largest double by changing parts or all of s1.

I was thinking of some metaprogramming magic that would generate
automatically S constructors with arguments all the possible subsets of the
member fields, and depending on the ctor selected by the user, would set
those bits or bools in the nested struct appropriately. This could generate
also getters/setters that the optimize function could use .
This could be a metafunction that returns a type SS based on the type S.
I realize this is definitely a question for boost as well.

The C:ptimize function would then return the "optimal" S, along with the
maximum double found.
The details and meaning of the optimization would be specific to
C:ptimize.

Best regards,


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-11-2009
James Kanze wrote:
> On Dec 9, 7:15 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> Vladimir Jovic wrote:
>>> Victor Bazarov wrote:
>>>> Hicham Mouline wrote:

>
> [...]
>>>> "return" are set to some "invalid" value. As an alternative you could
>>>> consider supplying another member to 'S', which would indicate the
>>>> "valid" or "active" members. You can have another (nested) struct in
>>>> 'S' with bit fields:

>
>>>> struct S {
>>>> ... // as you have now
>>>> struct {
>>>> unsigned use_1:1;
>>>> unsigned use_2:1;
>>>> ...
>>>> unsigned use_n:1;
>>>> } which_members_to_use;
>>>> };

>
>>> Using "bool use_1" might be clearer then "unsigned use_1:1".

>
>> Clearer, maybe. More expensive, for sure.

>
> More expensive in memory. Probably significantly less expensive
> in runtime.


Curious, isn't it, how one can easily tell when it's more expensive from
the memory standpoint, and how unknown the expense in terms of the run
time is. We can't tell whether it's "significantly less expensive"
until we actually measure, can we? <g>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
validators subset news.microsoft.com ASP .Net 1 05-27-2004 10:28 PM
Datagrid - populate with subset of an XML file Andy ASP .Net 0 05-24-2004 03:53 PM
subset of data using dataview?? Guoqi Zheng ASP .Net 2 01-19-2004 01:54 PM
Is array of POD still a POD type? Ajax Chelsea C++ 1 12-01-2003 01:56 PM
A (probable) error in perltoot ( perl5/5.8.0/pod/perltoot.pod, line number 756 ) Himanshu Garg Perl Misc 1 09-21-2003 03:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57