Skip to content

Instantly share code, notes, and snippets.

@endavid
Created February 22, 2024 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endavid/12ef5ed38a3296d2e83e593c0d851d10 to your computer and use it in GitHub Desktop.
Save endavid/12ef5ed38a3296d2e83e593c0d851d10 to your computer and use it in GitHub Desktop.
Intrusive double-linked list
// Copyright (C) 2012 David Gavilan Ruiz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* @file List.h
* @author David Gavilan
* @date 6/28/10
*/
#ifndef CORE_LIST_H_
#define CORE_LIST_H_
#include <stddef.h>
#include "core_def.h"
#include "core/Matom.h"
CORE_NS_BEGIN
// -----------------------------------------------------------
/** Node for sorted double linked list
* Notice that a node can't belong to 2 lists at the same time!!
*/
template <class T>
class IntrusiveNode : public Matom {
public:
IntrusiveNode( const int priority = 0 ) :
m_priority ( priority ),
m_next ( NULL ),
m_prev ( NULL )
{}
/// @return top of the list
T* Link( T* node ) {
T* selfT = static_cast<T*> ( this ) ;
if ( node->m_priority <= m_priority ) {
node->m_next = selfT ;
this->m_prev = node ;
return node ;
}
T* prev = selfT ;
T* next = this->m_next ;
while ( next ) {
if ( node->m_priority <= next->m_priority ) {
node->m_prev = prev ;
node->m_next = next ;
next->m_prev = node ;
prev->m_next = node ;
return selfT ;
}
prev = next ;
next = next->m_next ;
}
prev->m_next = node ;
node->m_prev = prev ;
return selfT ;
}
/// @return top of the list
T* Unlink( T* node ) {
T* selfT = static_cast<T*>( this ) ;
if ( node == this ) {
T* next = this->m_next ;
if (next) next->m_prev = NULL ;
this->m_prev = NULL ;
this->m_next = NULL ;
return next ;
}
T* prev = selfT ;
T* next = this->m_next ;
while ( next ) {
if ( next == node ) {
prev->m_next = next->m_next;
node->m_prev = NULL ;
node->m_next = NULL ;
return selfT ;
}
prev = next ;
next = next->m_next ;
}
// not found!
return selfT ;
}
T* GetNext() { return m_next ; }
T* GetPrev() { return m_prev ; }
const T* GetNext() const { return m_next ; }
const T* GetPrev() const { return m_prev ; }
int GetPriority() const { return m_priority; }
private:
int m_priority ;
T* m_next ;
T* m_prev ;
};
// -----------------------------------------------------------
/// Double linked list
template <class T>
class IntrusiveList {
public:
IntrusiveList() :
m_top ( NULL )
{}
IntrusiveList& Add(T* node) {
if ( m_top == NULL ) {
m_top = node ;
return *this ;
}
if ( node ) {
if (!Contains(node)) {
m_top = m_top->Link( node ) ;
}
}
return *this ;
}
IntrusiveList& Remove(T* node) {
if ( m_top ) {
m_top = m_top->Unlink( node ) ;
}
return *this ;
}
T* GetTop() const { return m_top; }
T* GetTail() const {
T* node = m_top;
if (node == NULL) return NULL;
for(;node->GetNext();node=node->GetNext()) {}
return node;
}
bool Contains(T* targetNode) const {
T* node = m_top;
if (node == NULL) return false;
for(;node->GetNext();node=node->GetNext()) {
if (node == targetNode) return true;
}
return false;
}
/// Finds the first node with the specified priority
T* FindFirst(const int priority) {
T* node = m_top;
for(;node;node=node->GetNext()) {
if (node->GetPriority()==priority) {
return node;
}
}
return NULL; // not found
}
private:
T* m_top ;
};
CORE_NS_END
#endif // CORE_LIST_H_
@endavid
Copy link
Author

endavid commented Feb 22, 2024

@endavid
Copy link
Author

endavid commented Feb 22, 2024

Matom is not very relevant, but it's just a simple class I defined to make sure all my objects have at least a destructor and an update function. It can be removed from the definition above, but it's this:

/** A Matom is a Memory Atom, a raw Entity, or Object, the Super class of all our game objects
 *  It defines a virtual destructor, so every pointer to Matoms can be destroyed.
 */
class Matom {
public:
    virtual ~Matom();
    virtual void Update(float time_ms);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment