/* * Copyright (c) 2006 Teodor Sigaev * All rights reserved. * * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``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 CONTRIBUTORS 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. */ #include #include #include #include #include "tmalloc.h" #include "glist.h" static void dumpGList(GList *l, char *str) { int i=0; GListCell *c; printf("%s: GLIST_LENGTH(%d)\n", str, GLIST_LENGTH(l)); GListForeach(c,l) printf("\t%d\t%s\n", i++, (char*)GLCELL_DATA(c)); } static GList* fillF() { int i; char buf[1024]; GList *l = NULL; for(i=0;i<16;i++) { sprintf(buf,":%d:",i); l = GListPush(l, tstrdup(buf)); } l->cmpFunc = (GListCellComparator)strcmp; l->freeFunc = (GListDataFreeFunc)tfree; return l; } static GList* fillB() { int i; char buf[1024]; GList *l = NULL; for(i=0;i<20;i+=2) { sprintf(buf,":%d:",i); l = GListUnshift(l, tstrdup(buf)); } l->cmpFunc = (GListCellComparator)strcmp; l->freeFunc = (GListDataFreeFunc)tfree; return l; } int main(int argn, char *argv[]) { GList *l; GListCell *c; dumpGList(fillF(), "Forward"); dumpGList(fillB(), "Backward"); dumpGList(GListTruncate(fillF(), 3), "Truncate"); dumpGList(GListConcat(fillF(), fillB()), "Concat"); dumpGList(GListDifference(fillF(), fillB()), "Difference"); dumpGList(GListUnion(fillF(), fillB()), "Union"); dumpGList(GListIntersect(fillF(), fillB()), "Intersect"); dumpGList(GListSort(fillB()), "Sort"); dumpGList(GListUniq(GListSort(GListConcat(fillF(), fillB()))), "Sort + Uniq + Concat"); l = fillB(); c = GListFind(l,":10:"); printf("Find: %s\n", (char*)GLCELL_DATA(c)); dumpGList(GListInsert(l,c,"|11|"),"Insert"); dumpGList(GListDelete(l,c),"Delete :10:"); printf("Get #5: %s\n", (char*)GLCELL_DATA(GListGet(l,5))); printf("Backward scan:\n"); GListForeachBack(c,l) printf("\t%s\n", (char*)GLCELL_DATA(c)); printf("Shift:\n"); while( (c=GListShift(l))!=NULL ) printf("\t%s\n", (char*)GLCELL_DATA(c)); l = fillB(); printf("Pop:\n"); while( (c=GListPop(l))!=NULL ) printf("\t%s\n", (char*)GLCELL_DATA(c)); GListFree(fillF()); return 0; }