Logo Search packages:      
Sourcecode: inkscape version File versions

int sp_repr_compare_position ( SPRepr *  first,
SPRepr *  second 
)

Works for different-parent objects, so long as they have a common ancestor. Return value: 0 positions are equivalent 1 first object's position is greater than the second -1 first object's position is less than the second

Definition at line 314 of file repr-util.cpp.

{
    int p1, p2;
    if (sp_repr_parent(first) == sp_repr_parent(second)) {
        /* Basic case - first and second have same parent */
        p1 = sp_repr_position(first);
        p2 = sp_repr_position(second);
    } else {
        /* Special case - the two objects have different parents.  They
           could be in different groups or on different layers for
           instance. */

        // Find the lowest common ancestor (LCA)
        SPRepr *ancestor = LCA(first, second);
        g_assert(ancestor != NULL);

        if (ancestor == first) {
            return 1;
        } else if (ancestor == second) {
            return -1;
        } else {
            SPRepr const *to_first = AncetreFils(first, ancestor);
            SPRepr const *to_second = AncetreFils(second, ancestor);
            g_assert(sp_repr_parent(to_second) == sp_repr_parent(to_first));
            p1 = sp_repr_position(to_first);
            p2 = sp_repr_position(to_second);
        }
    }

    if (p1 > p2) return 1;
    if (p1 < p2) return -1;
    return 0;

    /* effic: Assuming that the parent--child relationship is consistent
       (i.e. that the parent really does contain first and second among
       its list of children), it should be equivalent to walk along the
       children and see which we encounter first (returning 0 iff first
       == second).
       
       Given that this function is used solely for sorting, we can use a
       similar approach to do the sort: gather the things to be sorted,
       into an STL vector (to allow random access and faster
       traversals).  Do a single pass of the parent's children; for each
       child, do a pass on whatever items in the vector we haven't yet
       encountered.  If the child is found, then swap it to the
       beginning of the yet-unencountered elements of the vector.
       Continue until no more than one remains unencountered.  --
       pjrm */
}


Generated by  Doxygen 1.6.0   Back to index