1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* tclist.c -- a list for transcode / implementation
* (C) 2008-2010 - Francesco Romani <fromani -at- gmail -dot- com>
*
* This file is part of transcode, a video stream processing tool.
*
* transcode 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.
*
* transcode 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, see <http://www.gnu.org/licenses/>.
*/
#include "libtc.h"
#include "tclist.h"
/*************************************************************************/
enum {
DIR_BACKWARD = -1,
DIR_FORWARD = +1
};
static int free_item(TCListItem *item, void *unused)
{
tc_free(item);
return 0;
}
static TCListItem* next_item(TCListItem *cur, int direction)
{
TCListItem *ret = NULL;
if (cur) {
if (direction == DIR_BACKWARD) {
ret = cur->prev;
} else {
ret = cur->next;
}
}
return ret;
}
static TCListItem *start_item(TCList *L, int direction)
{
TCListItem *ret = NULL;
if (L) {
if (direction == DIR_BACKWARD) {
ret = L->tail;
} else {
ret = L->head;
}
}
return ret;
}
static void del_item(TCList *L, TCListItem *IT)
{
if (L->use_cache) {
IT->prev = NULL;
IT->data = NULL;
IT->next = L->cache;
L->cache = IT;
} else {
tc_free(IT);
}
}
static TCListItem *new_item(TCList *L)
{
TCListItem *IT = NULL;
if (L->use_cache && L->cache) {
IT = L->cache;
L->cache = L->cache->next;
} else {
IT = tc_zalloc(sizeof(TCListItem));
}
return IT;
}
static int foreach_item(TCListItem *start, int direction,
TCListVisitor vis, void *userdata)
{
int ret = 0;
TCListItem *cur = NULL, *inc = NULL;
for (cur = start; cur; cur = inc) {
inc = next_item(cur, direction);
ret = vis(cur, userdata);
if (ret != 0) {
break;
}
}
return ret;
}
struct find_data {
int dir;
int cur_idx;
int stop_idx;
TCListItem *ptr;
};
static int elem_finder(TCListItem *item, void *userdata)
{
struct find_data *FD = userdata;
int ret = 0;
FD->ptr = item;
if (FD->cur_idx != FD->stop_idx) {
FD->cur_idx += FD->dir;
} else {
ret = 1;
}
return ret;
}
static TCListItem *find_position(TCList *L, int pos)
{
TCListItem *ret = NULL;
if (L) {
/* common cases first */
if (pos == 0) {
ret = L->head;
} else if (pos == -1) {
ret = L->tail;
} else {
/* if we're here we can't avoid a full scan */
struct find_data FD = { DIR_FORWARD, 0, 0, NULL };
if (pos >= 0) {
FD.dir = DIR_FORWARD; /* enforce */
FD.cur_idx = 0;
FD.stop_idx = pos;
} else {
/*
* we're perfectly fine with negative indexes;
* we'll just starting from the end going backwards
* with -1 being the last element.
*/
FD.dir = DIR_BACKWARD;
FD.cur_idx = L->nelems - 1;
FD.stop_idx = L->nelems + pos;
}
/* we can now catch some over/under-run common cases */
if (FD.stop_idx > 0 || FD.stop_idx < L->nelems) {
/* we want something in the middle, so let's run */
FD.ptr = NULL; /* for safeness */
foreach_item(start_item(L, FD.dir), FD.dir, elem_finder, &FD);
ret = FD.ptr; /* cannot fail */
}
}
}
return ret;
}
static int item_insert_before(TCList *L, int pos, void *data)
{
int ret = TC_ERROR;
TCListItem *ref = find_position(L, pos);
if (ref) {
TCListItem *ext = new_item(L);
if (ext) {
ext->data = data;
ref->prev->next = ext;
ext->prev = ref->prev;
ext->next = ref;
ref->prev = ext;
L->nelems++;
ret = TC_OK;
}
}
return ret;
}
static int item_insert_after(TCList *L, int pos, void *data)
{
int ret = TC_ERROR;
TCListItem *ref = find_position(L, pos);
if (ref) {
TCListItem *ext = new_item(L);
if (ext) {
ext->data = data;
ref->next->prev = ext;
ext->next = ref->next;
ext->prev = ref;
ref->next = ext;
L->nelems++;
ret = TC_OK;
}
}
return ret;
}
/*************************************************************************/
int tc_list_init(TCList *L, int elemcache)
{
if (L) {
L->head = NULL;
L->tail = NULL;
L->nelems = 0;
L->cache = NULL;
L->use_cache = elemcache;
return 0;
}
return -1;
}
int tc_list_fini(TCList *L)
{
/* if !use_cache, this will not hurt anyone */
foreach_item(L->head, DIR_FORWARD, free_item, NULL);
foreach_item(L->cache, DIR_FORWARD, free_item, NULL);
/* now reset to clean status */
return tc_list_init(L, 0);
}
int tc_list_size(TCList *L)
{
return (L) ?L->nelems :0;
}
int tc_list_foreach(TCList *L, TCListVisitor vis, void *userdata)
{
return foreach_item(L->head, DIR_FORWARD, vis, userdata);
}
int tc_list_append(TCList *L, void *data)
{
int ret = TC_ERROR;
TCListItem *IT = new_item(L);
if (IT) {
IT->data = data;
IT->prev = L->tail;
if (!L->head) {
L->head = IT;
} else {
/* at least one element */
L->tail->next = IT;
}
L->tail = IT;
L->nelems++;
ret = TC_OK;
}
return ret;
}
int tc_list_prepend(TCList *L, void *data)
{
int ret = TC_ERROR;
TCListItem *IT = new_item(L);
if (IT) {
IT->data = data;
IT->next = L->head;
if (!L->tail) {
L->tail = IT;
} else {
/* at least one element */
L->head->prev = IT;
}
L->head = IT;
L->nelems++;
ret = TC_OK;
}
return ret;
}
int tc_list_insert(TCList *L, int pos, void *data)
{
int ret = TC_ERROR;
if (L && data) {
if (pos == 0) {
ret = tc_list_prepend(L, data);
} else if (pos == -1) {
ret = tc_list_append(L, data);
} else if (pos > 0) {
ret = item_insert_before(L, pos, data);
} else {
ret = item_insert_after(L, pos, data);
}
}
return ret;
}
void *tc_list_get(TCList *L, int pos)
{
TCListItem *IT = find_position(L, pos);
return (IT) ?IT->data :NULL;
}
void *tc_list_pop(TCList *L, int pos)
{
TCListItem *IT = find_position(L, pos);
void *data = NULL;
if (IT) {
data = IT->data;
if (L->head == IT) {
if (IT->next) {
IT->next->prev = NULL;
}
L->head = IT->next;
} else if (L->tail == IT) {
if (IT->prev) {
IT->prev->next = NULL;
}
L->tail = IT->prev;
} else {
IT->next->prev = IT->prev;
IT->prev->next = IT->next;
}
del_item(L, IT);
L->nelems--;
}
return data;
}
/*************************************************************************/
int tc_list_insert_dup(TCList *L, int pos, void *data, size_t size)
{
int ret = TC_ERROR;
void *mem = tc_malloc(size);
if (mem) {
memcpy(mem, data, size);
ret = tc_list_insert(L, pos, mem);
if (ret == TC_ERROR) {
tc_free(mem);
}
}
return ret;
}
static int free_item_all(TCListItem *item, void *unused)
{
if (item->data != NULL) {
free(item->data);
}
free(item);
return 0;
}
void tc_list_del(TCList *L, int deepclean)
{
if (deepclean) {
foreach_item(L->head, DIR_FORWARD, free_item_all, NULL);
/* if !use_cache, this will not hurt anyone */
foreach_item(L->cache, DIR_FORWARD, free_item_all, NULL);
}
tc_free(L);
}
TCList *tc_list_new(int usecache)
{
TCList *L = tc_malloc(sizeof(TCList));
if (L) {
tc_list_init(L, usecache);
}
return L;
}
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|