Skip to content

Commit

Permalink
Implemented a simple, reusable linked list structure. Supports single…
Browse files Browse the repository at this point in the history
… and double linked lists plus circular (ring) variants. List has an integral sort using a recursive mergesort algorithm.

TODO: Some level of thread-safety would be nice.
  • Loading branch information
danij committed Aug 23, 2007
1 parent f639448 commit 62f5d96
Show file tree
Hide file tree
Showing 5 changed files with 1,224 additions and 1 deletion.
1 change: 1 addition & 0 deletions doomsday/build/win32/doomsday_cl.rsp
Expand Up @@ -15,6 +15,7 @@
./../../engine/portable/src/m_filehash.c
./../../engine/portable/src/m_bams.c
./../../engine/portable/src/m_args.c
./../../engine/portable/src/m_linkedlist.c
./../../engine/portable/src/s_wav.c
./../../engine/portable/src/s_sfx.c
./../../engine/portable/src/s_mus.c
Expand Down
8 changes: 8 additions & 0 deletions doomsday/build/win32/vs8/doomsday.vcproj
Expand Up @@ -436,6 +436,10 @@
RelativePath="..\..\..\engine\portable\src\m_huffman.c"
>
</File>
<File
RelativePath="..\..\..\engine\portable\src\m_linkedlist.c"
>
</File>
<File
RelativePath="..\..\..\engine\portable\src\m_md5.c"
>
Expand Down Expand Up @@ -1383,6 +1387,10 @@
RelativePath="..\..\..\engine\portable\include\m_huffman.h"
>
</File>
<File
RelativePath="..\..\..\engine\portable\include\m_linkedlist.h"
>
</File>
<File
RelativePath="..\..\..\engine\portable\include\m_md5.h"
>
Expand Down
3 changes: 2 additions & 1 deletion doomsday/engine/portable/include/de_misc.h
@@ -1,4 +1,4 @@
/**\file
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
Expand Down Expand Up @@ -39,5 +39,6 @@
#include "m_vector.h"
#include "m_profiler.h"
#include "m_md5.h"
#include "m_linkedlist.h"

#endif
128 changes: 128 additions & 0 deletions doomsday/engine/portable/include/m_linkedlist.h
@@ -0,0 +1,128 @@
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2007 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

/*
* m_linkedlist.h: Linked lists.
*/

#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__

#ifdef __cplusplus
extern "C" {
#endif

// The C_DECL macro, used with compare functions.
#ifndef C_DECL
# if defined(WIN32)
# define C_DECL __cdecl
# elif defined(UNIX)
# define C_DECL
# endif
#endif

typedef int listindex_t;

// Helper macro for defining list compare functions.
#define COMPAREFUNC(name) int C_DECL name(const void *a, const void *b)

typedef int (C_DECL *comparefunc) (const void *a, const void *b);

// Premade compare functions:
COMPAREFUNC( compareInt );
COMPAREFUNC( compareUInt );
COMAPREFUNC( compareFloat );
COMPAREFUNC( compareDouble );
COMPAREFUNC( compareString );
COMPAREFUNC( compareAddress );

//
// Linked lists.
//
typedef void* linklist_t;

#define PTR_NOT_LIST -1

// Linked list errors:
enum {
LL_NO_ERROR,
LL_ERROR_UNKNOWN,
LL_ERROR_OUT_OF_MEMORY,
LL_ERROR_UNDERFLOW,
LL_ERROR_OVERFLOW,
LL_ERROR_LAST
};

// List_Create flags:
#define LCF_CIRCULAR 0x1 // The list forms a ring of items, as
// opposed to the default linear list.

// List_Iterate flags
#define LIF_REVERSE 0x1 // Iteration will be tail > head.

// Create/Destroy:
linklist_t *List_Create(void);
linklist_t *List_Create2(int createFlags, comparefunc func);
void List_Destroy(linklist_t *list);

// Inserts:
int List_InsertFront(linklist_t *list, const void *data);
int List_InsertBack(linklist_t *list, const void *data);

// Retrieves:
void* List_GetFront(const linklist_t *list);
void* List_GetBack(const linklist_t *list);
void* List_GetAt(const linklist_t *list, listindex_t position);

// Extracts:
void* List_ExtractFront(linklist_t *list);
void* List_ExtractBack(linklist_t *list);
void* List_ExtractAt(linklist_t *list, listindex_t position);

// Other:
void List_SetCompareFunc(linklist_t *list, comparefunc func);
void List_Clear(linklist_t *list);
listindex_t List_Find(const linklist_t *list, const void *data);
int List_Exchange(linklist_t *list, listindex_t positionA,
listindex_t positionB);
// Sorts:
void List_Sort(linklist_t *list);

// State retrieval:
listindex_t List_Count(const linklist_t *list);
int List_GetError(const linklist_t *list);

// Iteration:
int List_Iterate(const linklist_t *list, int iterateFlags,
void *data,
int (*callback) (void *element, void *data));
// Debugging:
#ifndef NDEBUG
void List_Test(void);
#endif

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit 62f5d96

Please sign in to comment.