Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2003-10-06 16:57:22


cppljevans_at_[hidden] wrote:

[...]

>> Group: group of pointees on the heap refering to themselves.
> Given pointee1 and pointee2, how can
> bool same_group(pointee1,pointee2);
> be calculated?

Remember that:

template <T>
    class shifted_ptr<T, root_collector>
    {
        ...

        T * m_ptr;
        long * m_ind;
    };

So if we have some sort of node as the pointee object, it will have another
smart pointer member inside it. Let's say pointee1 & pointee2 are 2 nodes:

struct node
{
    int i;
    shifted_ptr<node> p;
};

If we want to implement same_group() as it is:

bool same_group(shifted_ptr<node> & a, shifted_ptr<node> & b)
{
    return a.m_ind == b.m_ind;
}

int main()
{
    shifted_ptr<node> p = make_shifted_ptr<node>(); // Creates an
"indicator" since the shifted_ptr is on the stack

    p->p = make_shifted_ptr<node>();

    p->p->p = p; // Cyclic

    cout << (same_group(p, p->p) ? "true" : "false") << endl;
    cout << (same_group(p->p, p->p->p) ? "true" : "false") << endl;
    cout << (same_group(p->p->p, p) ? "true" : "false") << endl;
}

It will return:
false
true
false

Philippe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk