pointer, union, something...

Z

zacariaz

typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.
 
V

Victor Bazarov

typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.

You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
 
Z

zacariaz

typedef unsigned long long uint64 // just so we dont get confused ;)
ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.
@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.
so, do i have to write a function which i have to call constantly, or
is there a smarter way?
NB. the y[n] variable must of course not be altered in anyway by this
operation.

You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

i dont quite understand your last sentence, maybe a typo? but i do
understand your message, and let me point out that im only looking for
smart solutions here.

@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};

This is of course madness, but it would be a very simple solution if
thing worked different.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)
 
V

Victor Bazarov

typedef unsigned long long uint64 // just so we dont get confused ;)
ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all
times contain a value equal to the ORing of all the n variables. eg.
@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i
think). Oh, there is one unsignificant thing. It is know that that
the variable in y[n] will allways equal 0 if ANDed.
so, do i have to write a function which i have to call constantly,
or is there a smarter way?
NB. the y[n] variable must of course not be altered in anyway by
this operation.

You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul
tekst i anførselstegn -

- Vis tekst i anførselstegn -

i dont quite understand your last sentence, maybe a typo?

Nope, no typo AFAICS. What don't you understand?
but i do
understand your message, and let me point out that im only looking for
smart solutions here.

Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.
@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};

This is of course madness, but it would be a very simple solution if
thing worked different.

It would be, yes.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)

You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:

union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;

uint64 x;
};

Not sure how you progress from this to indexing within the 'y' member.

V
 
Z

zacariaz

(e-mail address removed) wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)
ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all
times contain a value equal to the ORing of all the n variables. eg.
@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i
think). Oh, there is one unsignificant thing. It is know that that
the variable in y[n] will allways equal 0 if ANDed.
so, do i have to write a function which i have to call constantly,
or is there a smarter way?
NB. the y[n] variable must of course not be altered in anyway by
this operation.
You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.
The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul
tekst i anførselstegn -
- Vis tekst i anførselstegn -
i dont quite understand your last sentence, maybe a typo?

Nope, no typo AFAICS. What don't you understand?
but i do
understand your message, and let me point out that im only looking for
smart solutions here.

Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.


@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};
This is of course madness, but it would be a very simple solution if
thing worked different.

It would be, yes.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)

You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:

union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;

uint64 x;
};

Not sure how you progress from this to indexing within the 'y' member.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

y[n] containt only either one or none set bits, but i need to know the
"position" of the set bit in the variable. You argue that bitsets
would be worth looking at and i have thought about it, but they are
just not suited for my needs.

I am playing around with bitboards, and at first it seems that bitsets
are the perfect candidate, but that is not the case, at least not for
this.
 
Z

zacariaz

(e-mail address removed) wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)
ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all
times contain a value equal to the ORing of all the n variables. eg.
@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i
think). Oh, there is one unsignificant thing. It is know that that
the variable in y[n] will allways equal 0 if ANDed.
so, do i have to write a function which i have to call constantly,
or is there a smarter way?
NB. the y[n] variable must of course not be altered in anyway by
this operation.
You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.
The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul
tekst i anførselstegn -
- Vis tekst i anførselstegn -
i dont quite understand your last sentence, maybe a typo?
Nope, no typo AFAICS. What don't you understand?
Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.
@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};
This is of course madness, but it would be a very simple solution if
thing worked different.
It would be, yes.
You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:
union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;
uint64 x;
};
Not sure how you progress from this to indexing within the 'y' member.
- Vis tekst i anførselstegn -

y[n] containt only either one or none set bits, but i need to know the
"position" of the set bit in the variable. You argue that bitsets
would be worth looking at and i have thought about it, but they are
just not suited for my needs.

I am playing around with bitboards, and at first it seems that bitsets
are the perfect candidate, but that is not the case, at least not for
this.- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

i read wront, you didnt say bitsets but bitfiels, sorry bout that.
anyway i cant use it.
 
G

Gianni Mariani

typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.

Option 1:
You may be able to get away with an O(log N) algorithm every time you
change an element in Y if you store intermediate values in an array.

Option 2:
Alternatively for O(1) complexity, you can keep counts on number of bits
set for each bit position. When a value is changed, you either
increment or decrement the count for a bit position, if any of them go
to zero then you clear the respective bit, it it is incremented you set
the respective bit.

Option 2 can also be improved to be O(number of bits changed) with a
little use of bit twiddling.
 
G

Gianni Mariani

y[n] containt only either one or none set bits,

You don't need a bitset.

All you need to know is bit position and y[] position.

If you only have 64 bit positions, then you don't need to actually store
y[], only bit index and a "not set" value (probably -1) and so you can
use an unsigned char for your bit index.

e.g.

struct BitThing
{
struct Data
{
uint64 x;
signed char ypos[ N ];
};

Data m_stuff;

Bithing()
: m_stuff()
{
}

...

class BithingProxy // proxy for operator []
{
...
};

BithingProxy operator[]( unsigned y_index )
{
return BithingProxy( this, y_index );
}

const uint64 operator[]( unsigned y_index ) const
{
assert( y_index < N );
return m_stuff.ypos[ y_index ] == -1 ? 0 :
uint64(1)<< m_stuff.ypos[y_index];
}

};

BithingProxy will allow you to write:
 
Z

zacariaz

(e-mail address removed) wrote:

...
y[n] containt only either one or none set bits,

You don't need a bitset.

All you need to know is bit position and y[] position.

If you only have 64 bit positions, then you don't need to actually store
y[], only bit index and a "not set" value (probably -1) and so you can
use an unsigned char for your bit index.

e.g.

struct BitThing
{
struct Data
{
uint64 x;
signed char ypos[ N ];
};

Data m_stuff;

Bithing()
: m_stuff()
{
}

...

class BithingProxy // proxy for operator []
{
...
};

BithingProxy operator[]( unsigned y_index )
{
return BithingProxy( this, y_index );
}

const uint64 operator[]( unsigned y_index ) const
{
assert( y_index < N );
return m_stuff.ypos[ y_index ] == -1 ? 0 :
uint64(1)<< m_stuff.ypos[y_index];
}

};

BithingProxy will allow you to write:


This way is possible i guess, but severen modifications is need.
1. i would still need to be able to treat y as an uint64 as i would
need the ability to perform various bitwise opperations on it.
I supose you could do something like this:

struct T {
unsigned char pos:6;
unsigned char y:1;
T():pos(0),t(0) {}
T(uint64 n) {
for(int i = 0; pos == 0; i++) {
if(n&(1ull<<i) == 1) {
pos = i;
y = 1;
}
}
}
};

allso there would need to be implemented alot of ostream, istream,
opprator, stuff that i know nothing about...
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top