this repo has no description
at trunk 50 lines 1.3 kB view raw
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */ 2#pragma once 3 4namespace py { 5 6struct ListEntry { 7 ListEntry* prev; 8 ListEntry* next; 9}; 10 11// Inserts an entry into the linked list as the new root 12bool listEntryInsert(ListEntry* entry, ListEntry** root); 13 14// Removes an entry from the linked list 15bool listEntryRemove(ListEntry* entry, ListEntry** root); 16 17inline bool listEntryInsert(ListEntry* entry, ListEntry** root) { 18 // If already tracked, do nothing. 19 if (entry->prev != nullptr || entry->next != nullptr || entry == *root) { 20 return false; 21 } 22 entry->prev = nullptr; 23 entry->next = *root; 24 if (*root != nullptr) { 25 (*root)->prev = entry; 26 } 27 *root = entry; 28 return true; 29} 30 31inline bool listEntryRemove(ListEntry* entry, ListEntry** root) { 32 // The node is the first node of the list. 33 if (*root == entry) { 34 *root = entry->next; 35 } else if (entry->prev == nullptr && entry->next == nullptr) { 36 // This is an already untracked object. 37 return false; 38 } 39 if (entry->prev != nullptr) { 40 entry->prev->next = entry->next; 41 } 42 if (entry->next != nullptr) { 43 entry->next->prev = entry->prev; 44 } 45 entry->prev = nullptr; 46 entry->next = nullptr; 47 return true; 48} 49 50} // namespace py