71 if (this->list && this->list->_nodeObjectCleanUpCallback) {
72 this->list->_nodeObjectCleanUpCallback(this->obj);
92 this->_nodeObjectCleanUpCallback = 0;
93 this->_compareCallback = 0;
96 List(
const std::initializer_list<L> & list) :
List() {
104 S
count()
const {
return this->_count; }
115 if (!this->_head && !this->_tail) {
120 }
else if (this->_head && this->_tail) {
121 n->left = this->_tail;
122 this->_tail->right = n;
143 if (!this->runCompare(obj, n->object())) {
157 if ((index >= 0) && (index < this->_count))
168 if ((index >= 0) && (index < this->_count)) {
177 Node * node = this->_head;
179 Node * tmp = node->right;
192 Node * n = this->nodeAtIndex(index, this->_head, 0);
193 if (n)
return n->
object();
200 [[deprecated(
"please use the setReleaseCallback")]]
202 this->_nodeObjectCleanUpCallback = callback;
209 this->_nodeObjectCleanUpCallback = callback;
216 this->_compareCallback = callback;
237 return this->getNodeForObject(obj, this->_head) != NULL;
244 std::cout << std::endl;
247 this->
print(this->_head, NULL);
250 std::cout << std::endl;
261 return this->sortNodeToNode(this->_head, this->_tail, this->_count, option);
269 Node * n = this->_tail;
270 int i = this->
count() - 1;
271 for (; n; n = n->
prev()) {
273 int r = rand() % (i + 1);
274 Node * tmp = this->nodeAtIndex(r, this->_head, 0);
275 int err = this->swap(n, tmp);
291 if (this->_tail == node) this->_tail = node->left;
292 if (this->_head == node) this->_head = node->right;
296 node->left->right = node->right;
299 node->right->left = node->left;
317 static int swap(Node * a, Node * b) {
319 if (!a || !b)
return -1;
334 Node * mid = 0, * t0 = 0, * t1 = 0;
335 const S halfDistance = distance / 2;
344 if (this->runCompare(
first->obj,
last->obj) < 0) {
348 if (this->runCompare(
first->obj,
last->obj) > 0) {
356 for (S i = 0; i < (halfDistance - 1); i++)
363 result = this->sortNodeToNode(
first, mid, halfDistance, option);
367 result = this->sortNodeToNode(mid->next(),
last, distance - halfDistance, option);
371 while (t0 && (t0 != mid->next()) && t1 && (t1 !=
last->
next())) {
372 int res = this->runCompare(t0->obj, t1->obj);
378 }
else if (res > 0) {
394 }
else if (res > 0) {
410 while (t0 && (t0 != mid->next())) {
415 while (t1 && (t1 !=
last->
next())) {
421 for (Node * n0 = tmp.first(), * n1 =
first;
423 n0 = n0->next(), n1 = n1->next()) {
438 void set(
const std::initializer_list<L> & list) {
440 typename std::initializer_list<L>::iterator itr;
441 for (itr = list.begin(); itr != list.end(); ++itr) {
449 void set(L * rawArray, S size) {
450 if (!rawArray)
return;
452 for (S i = 0; i < size; i++) {
453 this->
add(rawArray[i]);
460 Node * nodeAtIndex(S reqIndex, Node * node, S currIndex)
const {
462 if (currIndex == reqIndex) {
465 return this->nodeAtIndex(reqIndex, node->right, ++currIndex);
482 if (currIndex < reqIndex) {
490 if (currNode->left) currNode->left->right = n;
491 n->left = currNode->left;
508 if (currNode == 0)
return 2;
510 if (currIndex < reqIndex) {
513 L
object = currNode->object();
514 if (this->_nodeObjectCleanUpCallback) this->_nodeObjectCleanUpCallback(
object);
525 Node * getNodeForObject(L obj, Node * node) {
526 if (!node)
return NULL;
528 if (node->obj == obj)
return node;
530 return this->getNodeForObject(obj, node->right);
535 void print(Node * node, Node *
end) {
537 std::cout <<
" " << node->object() <<
" ";
549 int runCompare(L a, L b) {
550 if (this->_compareCallback)
return this->_compareCallback(a, b);
552 if (a < b)
return -1;
553 else if (a > b)
return 1;
571 void (* _nodeObjectCleanUpCallback)(L obj);
579 int (* _compareCallback) (L a, L b);
599 return this->_curr->
object();
602 this->_curr = this->_curr->
next();
611 return !(*
this == i);
614 return this->_curr == i._curr;