qse/lib/cmn/sll.c

349 lines
7.4 KiB
C
Raw Permalink Normal View History

2008-08-06 07:30:10 +00:00
/*
2012-08-16 03:47:55 +00:00
* $Id$
2008-08-06 07:30:10 +00:00
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
2014-11-19 14:42:24 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
2014-11-19 14:42:24 +00:00
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2008-08-06 07:30:10 +00:00
*/
2008-12-21 21:35:07 +00:00
#include <qse/cmn/sll.h>
2016-04-29 03:55:42 +00:00
#include "mem-prv.h"
2008-12-21 21:35:07 +00:00
#define sll_t qse_sll_t
#define node_t qse_sll_node_t
#define copier_t qse_sll_copier_t
#define freeer_t qse_sll_freeer_t
#define comper_t qse_sll_comper_t
#define walker_t qse_sll_walker_t
2008-08-27 02:50:12 +00:00
2008-12-21 21:35:07 +00:00
#define HEAD(s) QSE_SLL_HEAD(s)
#define TAIL(s) QSE_SLL_TAIL(s)
#define SIZE(s) QSE_SLL_SIZE(s)
2008-08-27 02:50:12 +00:00
2008-12-21 21:35:07 +00:00
#define DPTR(n) QSE_SLL_DPTR(n)
#define DLEN(n) QSE_SLL_DLEN(n)
#define NEXT(n) QSE_SLL_NEXT(n)
2008-08-27 02:50:12 +00:00
#define TOB(sll,len) ((len)*(sll)->scale)
2008-12-21 21:35:07 +00:00
#define SIZEOF(x) QSE_SIZEOF(x)
#define size_t qse_size_t
#define mmgr_t qse_mmgr_t
2008-08-27 02:50:12 +00:00
2010-09-04 06:50:08 +00:00
static int default_comper (sll_t* sll,
2008-09-29 00:02:10 +00:00
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
2008-12-21 21:35:07 +00:00
if (dlen1 == dlen2) return QSE_MEMCMP (dptr1, dptr2, TOB(sll,dlen1));
2008-09-29 00:02:10 +00:00
/* it just returns 1 to indicate that they are different. */
return 1;
2010-09-04 06:50:08 +00:00
#if 0
size_t min = (dlen1 < dlen2)? dlen1: dlen2;
int n = QSE_MEMCMP (dptr1, dptr2, TOB(sll,min));
if (n == 0 && dlen1 != dlen2)
{
n = (dlen1 > dlen2)? 1: -1;
}
return n;
#endif
2008-09-29 00:02:10 +00:00
}
2008-10-08 05:30:16 +00:00
static node_t* alloc_node (sll_t* sll, void* dptr, size_t dlen)
{
node_t* n;
2008-12-21 21:35:07 +00:00
if (sll->copier == QSE_SLL_COPIER_SIMPLE)
2008-10-08 05:30:16 +00:00
{
2008-12-21 21:35:07 +00:00
n = QSE_MMGR_ALLOC (sll->mmgr, SIZEOF(node_t));
if (n == QSE_NULL) return QSE_NULL;
2008-10-08 05:30:16 +00:00
DPTR(n) = dptr;
}
2008-12-21 21:35:07 +00:00
else if (sll->copier == QSE_SLL_COPIER_INLINE)
2008-10-08 05:30:16 +00:00
{
2008-12-21 21:35:07 +00:00
n = QSE_MMGR_ALLOC (sll->mmgr,
2008-10-08 05:30:16 +00:00
SIZEOF(node_t) + TOB(sll,dlen));
2008-12-21 21:35:07 +00:00
if (n == QSE_NULL) return QSE_NULL;
2008-10-08 05:30:16 +00:00
2008-12-21 21:35:07 +00:00
QSE_MEMCPY (n + 1, dptr, TOB(sll,dlen));
2008-10-08 05:30:16 +00:00
DPTR(n) = n + 1;
}
else
{
2008-12-21 21:35:07 +00:00
n = QSE_MMGR_ALLOC (sll->mmgr, SIZEOF(node_t));
if (n == QSE_NULL) return QSE_NULL;
2008-10-08 05:30:16 +00:00
DPTR(n) = sll->copier (sll, dptr, dlen);
2008-12-21 21:35:07 +00:00
if (DPTR(n) == QSE_NULL)
2008-10-08 05:30:16 +00:00
{
2008-12-21 21:35:07 +00:00
QSE_MMGR_FREE (sll->mmgr, n);
return QSE_NULL;
2008-10-08 05:30:16 +00:00
}
}
DLEN(n) = dlen;
2008-12-21 21:35:07 +00:00
NEXT(n) = QSE_NULL;
2008-10-08 05:30:16 +00:00
return n;
}
2008-12-21 21:35:07 +00:00
sll_t* qse_sll_open (mmgr_t* mmgr, size_t ext)
2008-08-18 08:51:40 +00:00
{
2008-08-27 02:50:12 +00:00
sll_t* sll;
2008-08-06 07:30:10 +00:00
2008-12-21 21:35:07 +00:00
sll = QSE_MMGR_ALLOC (mmgr, SIZEOF(sll_t) + ext);
if (sll == QSE_NULL) return QSE_NULL;
2008-08-06 07:30:10 +00:00
2011-09-01 09:43:46 +00:00
if (qse_sll_init (sll, mmgr) <= -1)
2008-10-14 05:32:58 +00:00
{
2008-12-21 21:35:07 +00:00
QSE_MMGR_FREE (mmgr, sll);
return QSE_NULL;
2008-10-14 05:32:58 +00:00
}
return sll;
}
2008-12-21 21:35:07 +00:00
void qse_sll_close (sll_t* sll)
{
2008-12-21 21:35:07 +00:00
qse_sll_fini (sll);
QSE_MMGR_FREE (sll->mmgr, sll);
}
2011-09-01 09:43:46 +00:00
int qse_sll_init (sll_t* sll, mmgr_t* mmgr)
{
/* do not zero out the extension */
2008-12-21 21:35:07 +00:00
QSE_MEMSET (sll, 0, SIZEOF(*sll));
2008-09-30 01:11:08 +00:00
2008-08-06 07:30:10 +00:00
sll->mmgr = mmgr;
sll->size = 0;
sll->scale = 1;
2008-09-29 00:02:10 +00:00
2010-09-04 06:50:08 +00:00
sll->comper = default_comper;
2008-12-21 21:35:07 +00:00
sll->copier = QSE_SLL_COPIER_SIMPLE;
2011-09-01 09:43:46 +00:00
return 0;
2008-08-06 07:30:10 +00:00
}
2008-08-07 07:52:14 +00:00
2008-12-21 21:35:07 +00:00
void qse_sll_fini (sll_t* sll)
2008-08-07 07:52:14 +00:00
{
2008-12-21 21:35:07 +00:00
qse_sll_clear (sll);
2008-08-07 07:52:14 +00:00
}
qse_mmgr_t* qse_sll_getmmgr (qse_sll_t* sll)
{
return sll->mmgr;
}
void* qse_sll_getxtn (qse_sll_t* sll)
{
return QSE_XTN (sll);
}
2008-12-21 21:35:07 +00:00
int qse_sll_getscale (sll_t* sll)
{
return sll->scale;
}
2008-12-21 21:35:07 +00:00
void qse_sll_setscale (sll_t* sll, int scale)
{
2008-12-21 21:35:07 +00:00
QSE_ASSERTX (scale > 0 && scale <= QSE_TYPE_MAX(qse_byte_t),
"The scale should be larger than 0 and less than or equal to the maximum value that the qse_byte_t type can hold");
if (scale <= 0) scale = 1;
2008-12-21 21:35:07 +00:00
if (scale > QSE_TYPE_MAX(qse_byte_t)) scale = QSE_TYPE_MAX(qse_byte_t);
sll->scale = scale;
}
2008-12-21 21:35:07 +00:00
copier_t qse_sll_getcopier (sll_t* sll)
2008-08-11 08:22:31 +00:00
{
return sll->copier;
}
2008-12-21 21:35:07 +00:00
void qse_sll_setcopier (sll_t* sll, copier_t copier)
2008-08-07 07:52:14 +00:00
{
2008-12-21 21:35:07 +00:00
if (copier == QSE_NULL) copier = QSE_SLL_COPIER_SIMPLE;
sll->copier = copier;
2008-08-07 07:52:14 +00:00
}
2008-12-21 21:35:07 +00:00
freeer_t qse_sll_getfreeer (sll_t* sll)
2008-08-11 08:22:31 +00:00
{
return sll->freeer;
}
2008-12-21 21:35:07 +00:00
void qse_sll_setfreeer (sll_t* sll, freeer_t freeer)
2008-08-07 07:52:14 +00:00
{
sll->freeer = freeer;
2008-08-07 07:52:14 +00:00
}
2008-12-21 21:35:07 +00:00
comper_t qse_sll_getcomper (sll_t* sll)
2008-09-29 00:02:10 +00:00
{
return sll->comper;
}
2008-12-21 21:35:07 +00:00
void qse_sll_setcomper (sll_t* sll, comper_t comper)
2008-09-29 00:02:10 +00:00
{
2010-09-04 06:50:08 +00:00
if (comper == QSE_NULL) comper = default_comper;
2008-09-29 00:02:10 +00:00
sll->comper = comper;
}
2008-12-21 21:35:07 +00:00
size_t qse_sll_getsize (sll_t* sll)
2008-08-07 07:52:14 +00:00
{
2008-10-08 05:30:16 +00:00
return SIZE(sll);
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_gethead (sll_t* sll)
2008-10-08 05:30:16 +00:00
{
return HEAD(sll);
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_gettail (sll_t* sll)
2008-10-08 05:30:16 +00:00
{
return TAIL(sll);
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_search (sll_t* sll, node_t* pos, const void* dptr, size_t dlen)
2008-09-29 08:20:27 +00:00
{
2009-01-18 01:48:21 +00:00
pos = (pos == QSE_NULL)? sll->head: NEXT(pos);
2008-09-29 08:20:27 +00:00
2008-12-21 21:35:07 +00:00
while (pos != QSE_NULL)
2008-09-29 08:20:27 +00:00
{
if (sll->comper (sll, DPTR(pos), DLEN(pos), dptr, dlen) == 0)
{
return pos;
}
pos = NEXT(pos);
}
2008-12-21 21:35:07 +00:00
return QSE_NULL;
2008-09-29 08:20:27 +00:00
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_insert (
2008-08-27 02:50:12 +00:00
sll_t* sll, node_t* pos, void* dptr, size_t dlen)
2008-08-09 04:35:04 +00:00
{
2008-08-27 02:50:12 +00:00
node_t* n = alloc_node (sll, dptr, dlen);
2008-12-21 21:35:07 +00:00
if (n == QSE_NULL) return QSE_NULL;
2008-08-09 04:35:04 +00:00
2008-12-21 21:35:07 +00:00
if (pos == QSE_NULL)
2008-08-09 04:35:04 +00:00
{
/* insert at the end */
2008-12-21 21:35:07 +00:00
if (HEAD(sll) == QSE_NULL)
2008-08-11 02:27:21 +00:00
{
2008-12-21 21:35:07 +00:00
QSE_ASSERT (TAIL(sll) == QSE_NULL);
2008-08-27 02:50:12 +00:00
HEAD(sll) = n;
2008-08-11 02:27:21 +00:00
}
2008-08-27 02:50:12 +00:00
else NEXT(TAIL(sll)) = n;
2008-08-11 02:27:21 +00:00
2008-08-27 02:50:12 +00:00
TAIL(sll) = n;
2008-08-09 04:35:04 +00:00
}
else
{
/* insert in front of the positional node */
2008-08-27 02:50:12 +00:00
NEXT(n) = pos;
if (pos == HEAD(sll)) HEAD(sll) = n;
2008-08-11 02:27:21 +00:00
else
{
2008-08-11 08:22:31 +00:00
/* take note of performance penalty */
2008-08-27 02:50:12 +00:00
node_t* n2 = HEAD(sll);
while (NEXT(n2) != pos) n2 = NEXT(n2);
NEXT(n2) = n;
2008-08-11 02:27:21 +00:00
}
2008-08-09 04:35:04 +00:00
}
2008-08-11 02:27:21 +00:00
2008-08-27 02:50:12 +00:00
SIZE(sll)++;
2008-08-09 04:35:04 +00:00
return n;
}
2008-12-21 21:35:07 +00:00
void qse_sll_delete (sll_t* sll, node_t* pos)
2008-08-11 08:22:31 +00:00
{
2008-12-21 21:35:07 +00:00
if (pos == QSE_NULL) return; /* not a valid node */
2008-08-11 08:22:31 +00:00
2008-08-27 02:50:12 +00:00
if (pos == HEAD(sll))
2008-08-11 08:22:31 +00:00
{
2008-08-12 04:52:25 +00:00
/* it is simple to delete the head node */
2008-08-27 02:50:12 +00:00
HEAD(sll) = NEXT(pos);
2008-12-21 21:35:07 +00:00
if (HEAD(sll) == QSE_NULL) TAIL(sll) = QSE_NULL;
2008-08-11 08:22:31 +00:00
}
else
{
2008-08-12 04:52:25 +00:00
/* but deletion of other nodes has significant performance
* penalty as it has look for the predecessor of the
* target node */
2008-08-27 02:50:12 +00:00
node_t* n2 = HEAD(sll);
while (NEXT(n2) != pos) n2 = NEXT(n2);
2008-08-12 04:52:25 +00:00
2008-08-27 02:50:12 +00:00
NEXT(n2) = NEXT(pos);
2008-08-12 04:52:25 +00:00
/* update the tail node if necessary */
2008-08-27 02:50:12 +00:00
if (pos == TAIL(sll)) TAIL(sll) = n2;
2008-08-11 08:22:31 +00:00
}
2008-12-21 21:35:07 +00:00
if (sll->freeer != QSE_NULL)
2008-08-12 04:52:25 +00:00
{
/* free the actual data */
2008-08-27 02:50:12 +00:00
sll->freeer (sll, DPTR(pos), DLEN(pos));
2008-08-12 04:52:25 +00:00
}
/* free the node */
2008-12-21 21:35:07 +00:00
QSE_MMGR_FREE (sll->mmgr, pos);
2008-08-12 04:52:25 +00:00
/* decrement the number of elements */
2008-08-27 02:50:12 +00:00
SIZE(sll)--;
2008-08-11 08:22:31 +00:00
}
2008-12-21 21:35:07 +00:00
void qse_sll_clear (sll_t* sll)
{
2008-12-21 21:35:07 +00:00
while (HEAD(sll) != QSE_NULL) qse_sll_delete (sll, HEAD(sll));
QSE_ASSERT (TAIL(sll) == QSE_NULL);
}
2010-09-04 06:50:08 +00:00
void qse_sll_walk (sll_t* sll, walker_t walker, void* ctx)
{
node_t* n = HEAD(sll);
while (n != QSE_NULL)
{
qse_sll_node_t* nxt = NEXT(n);
if (walker(sll,n,ctx) == QSE_SLL_WALK_STOP) return;
n = nxt;
}
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_pushhead (sll_t* sll, void* data, size_t size)
{
2008-12-21 21:35:07 +00:00
return qse_sll_insert (sll, HEAD(sll), data, size);
}
2008-12-21 21:35:07 +00:00
node_t* qse_sll_pushtail (sll_t* sll, void* data, size_t size)
{
2008-12-21 21:35:07 +00:00
return qse_sll_insert (sll, QSE_NULL, data, size);
}
2008-12-21 21:35:07 +00:00
void qse_sll_pophead (sll_t* sll)
2008-08-11 08:22:31 +00:00
{
2008-12-21 21:35:07 +00:00
qse_sll_delete (sll, HEAD(sll));
2008-08-11 08:22:31 +00:00
}
2008-12-21 21:35:07 +00:00
void qse_sll_poptail (sll_t* sll)
2008-08-11 08:22:31 +00:00
{
2008-12-21 21:35:07 +00:00
qse_sll_delete (sll, TAIL(sll));
2008-08-11 08:22:31 +00:00
}
2008-08-23 09:23:09 +00:00