Fix List
swap behavior on front, back and adjacent elements
- immediately return if A == B;
- first and last elements (front, back) are updated upon relinking;
- handles a special case of forward and backward adjacent elements.
(cherry picked from commit ac69f092fc
)
This commit is contained in:
parent
564d7101b4
commit
1f42a8a073
1 changed files with 29 additions and 15 deletions
44
core/list.h
44
core/list.h
|
@ -417,24 +417,38 @@ public:
|
||||||
ERR_FAIL_COND(p_A->data != _data);
|
ERR_FAIL_COND(p_A->data != _data);
|
||||||
ERR_FAIL_COND(p_B->data != _data);
|
ERR_FAIL_COND(p_B->data != _data);
|
||||||
|
|
||||||
|
if (p_A == p_B) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Element *A_prev = p_A->prev_ptr;
|
Element *A_prev = p_A->prev_ptr;
|
||||||
Element *A_next = p_A->next_ptr;
|
Element *A_next = p_A->next_ptr;
|
||||||
|
Element *B_prev = p_B->prev_ptr;
|
||||||
|
Element *B_next = p_B->next_ptr;
|
||||||
|
|
||||||
p_A->next_ptr = p_B->next_ptr;
|
if (A_prev) {
|
||||||
p_A->prev_ptr = p_B->prev_ptr;
|
A_prev->next_ptr = p_B;
|
||||||
|
} else {
|
||||||
p_B->next_ptr = A_next;
|
_data->first = p_B;
|
||||||
p_B->prev_ptr = A_prev;
|
}
|
||||||
|
if (B_prev) {
|
||||||
if (p_A->prev_ptr)
|
B_prev->next_ptr = p_A;
|
||||||
p_A->prev_ptr->next_ptr = p_A;
|
} else {
|
||||||
if (p_A->next_ptr)
|
_data->first = p_A;
|
||||||
p_A->next_ptr->prev_ptr = p_A;
|
}
|
||||||
|
if (A_next) {
|
||||||
if (p_B->prev_ptr)
|
A_next->prev_ptr = p_B;
|
||||||
p_B->prev_ptr->next_ptr = p_B;
|
} else {
|
||||||
if (p_B->next_ptr)
|
_data->last = p_B;
|
||||||
p_B->next_ptr->prev_ptr = p_B;
|
}
|
||||||
|
if (B_next) {
|
||||||
|
B_next->prev_ptr = p_A;
|
||||||
|
} else {
|
||||||
|
_data->last = p_A;
|
||||||
|
}
|
||||||
|
p_A->prev_ptr = A_next == p_B ? p_B : B_prev;
|
||||||
|
p_A->next_ptr = B_next == p_A ? p_B : B_next;
|
||||||
|
p_B->prev_ptr = B_next == p_A ? p_A : A_prev;
|
||||||
|
p_B->next_ptr = A_next == p_B ? p_A : A_next;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* copy the list
|
* copy the list
|
||||||
|
|
Loading…
Reference in a new issue