singleList

type-safe single linked list with less memory allocations
git clone https://noulin.net/git/singleList.git
Log | Files | Refs | README | LICENSE

main.c (9853B)


      1 #! /usr/bin/env sheepy
      2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */
      3 
      4 /* Libsheepy documentation: http://spartatek.se/libsheepy/ */
      5 #include "libsheepyObject.h"
      6 #include "isingleList.h"
      7 #include "singleList.h"
      8 #include "singleListe.h"
      9 
     10 int argc; char **argv;
     11 
     12 /* enable/disable logging */
     13 /* #undef pLog */
     14 /* #define pLog(...) */
     15 
     16 
     17 int main(int ARGC, char** ARGV) {
     18 
     19   argc = ARGC; argv = ARGV;
     20 
     21   initLibsheepy(ARGV[0]);
     22   setLogMode(LOG_FUNC);
     23 
     24   // define list:
     25   isingleListT(slt, u8, char*);
     26 
     27   // declare list:
     28   slt sl;
     29 
     30   logVarG(sizeof sl);
     31 
     32   // initialize:
     33   isingleListInit(&sl, -1);
     34 
     35   logVarG(isingleListHeadIdx(&sl));
     36   logVarG(isingleListLastIdx(&sl));
     37 
     38   isingleListNodeType(&sl) sE;
     39 
     40   logI("isEmpty %b", isingleListIsEmpty(&sl));
     41   logI("count %zu", isingleListCount(&sl));
     42   logI("push %b", isingleListPush(&sl));
     43   logI("count %zu", isingleListCount(&sl));
     44   logVarG(sl.last);
     45   isingleListFirst(&sl) = "qwe";
     46   logVarG(isingleListLast(&sl));
     47   isingleListLastNode(&sl).elem = "QWER";
     48   logVarG(isingleListElem(&sl, 0));
     49   logVarG(isingleListNode(&sl, 0).elem);
     50   logVarG(isingleListPrevIdx(&sl, 0));
     51   logI("push %b", isingleListPush(&sl));
     52   logI("count %zu", isingleListCount(&sl));
     53   logVarG(sl.last);
     54   isingleListLast(&sl) = "22 qwe";
     55   logVarG(isingleListLastPrevIdx(&sl));
     56   logVarG(isingleListPrevAt(&sl, isingleListLastIdx(&sl)).elem);
     57   logVarG(isingleListPrev(&sl, isingleListLastNode(&sl)).elem);
     58   logI("push %b", isingleListPush(&sl));
     59   logI("count %zu", isingleListCount(&sl));
     60   logVarG(sl.last);
     61   isingleListLast(&sl) = "qwe33333";
     62   isingleListForEachDown(&sl, i) {
     63     logI("elem: %s", isingleListElem(&sl, i));
     64   }
     65   isingleListForEachNodeDown(&sl, node) {
     66     logI("elem: %s", node->elem);
     67   }
     68   isingleListForEachElemDown(&sl, i) {
     69     logI("elem: %s", i);
     70   }
     71   isingleListForEachElemPDown(&sl, i) {
     72     logI("elem: %s", *i);
     73   }
     74   logI("count %zu", isingleListCount(&sl));
     75   var u = isingleListUnlinkPrev(&sl, sl.last);
     76   //var u = isingleListUnlinkLast(&sl);
     77   logI("count %zu", isingleListCount(&sl));
     78   AT;
     79   //isingleListInsertBefore(&sl, sl.head, u);
     80   isingleListInsertLast(&sl, u);
     81   var add = isingleListAddBefore(&sl, sl.head);
     82   isingleListFirst(&sl) = "4444 add qwe";
     83   isingleListForEachElemPDown(&sl, i) {
     84     logI("elem: %s", *i);
     85   }
     86   isingleListPrevPtr(&sl, isingleListLastPtr(&sl))->elem = "zxcxc";
     87   logI("count %zu", isingleListCount(&sl));
     88   var idx = isingleListDelPrev(&sl, 2);
     89   logVarG(idx);
     90   logI("count %zu", isingleListCount(&sl));
     91   logVarG(sl.last);
     92   logI("pop %b", isingleListPop(&sl));
     93   logI("count %zu", isingleListCount(&sl));
     94   logVarG(sl.last);
     95   logI("pop %b", isingleListPop(&sl));
     96   logI("count %zu", isingleListCount(&sl));
     97   logVarG(sl.last);
     98   logI("pop %b", isingleListPop(&sl));
     99   logI("count %zu", isingleListCount(&sl));
    100   logVarG(sl.last);
    101   isingleListFree(&sl);
    102 
    103   // read/write list
    104   // define list:
    105   isingleListT(sllt, u8, u8);
    106 
    107   // declare list:
    108   sllt sll;
    109 
    110   // initialize:
    111   isingleListInit(&sll, -1);
    112 
    113   isingleListPush(&sll);
    114   //isingleListFirst(&sll) = 1;
    115   isingleListLast(&sll) = 1;
    116 
    117   //isingleListAddBefore(&sll, sll.head);
    118   isingleListPush(&sll);
    119   //isingleListFirst(&sll) = 2;
    120   isingleListLast(&sll) = 2;
    121   //isingleListAddBefore(&sll, sll.head);
    122   isingleListPush(&sll);
    123   //isingleListFirst(&sll) = 3;
    124   isingleListLast(&sll) = 3;
    125   isingleListForEachDown(&sll, i) {
    126     logI("uint elem: %d", isingleListElem(&sll, i));
    127   }
    128   put;
    129   isingleListWriteFilename(&sll, "slist.dat");
    130 
    131   isingleListPush(&sll);
    132   isingleListLast(&sll) = 222;
    133   isingleListPush(&sll);
    134   isingleListLast(&sll) = 33;
    135   isingleListFirst(&sll) = 0;
    136 
    137   isingleListReadFilename(&sll, "slist.dat");
    138 
    139   isingleListForEachDown(&sll, i) {
    140     logI("uint elem: %d", isingleListElem(&sll, i));
    141   }
    142 
    143   isingleListFree(&sll);
    144 
    145 
    146   void s(void) {
    147 
    148   // define list:
    149   singleListT(slt, char*);
    150 
    151   // declare list:
    152   slt sl;
    153 
    154   logVarG(sizeof sl);
    155 
    156   // initialize:
    157   singleListInit(&sl);
    158 
    159   singleListNodeType(&sl) sE;
    160 
    161   logI("isEmpty %b", singleListIsEmpty(&sl));
    162   logI("count %zu", singleListCount(&sl));
    163   logI("push %b", singleListPush(&sl));
    164   logI("count %zu", singleListCount(&sl));
    165   singleListFirst(&sl) = "qwe";
    166   logVarG(singleListLast(&sl));
    167   singleListLastNode(&sl)->elem = "QWER";
    168   logVarG(singleListElem(sl.head));
    169   logVarG(sl.head->prev);
    170   logI("push %b", singleListPush(&sl));
    171   logI("count %zu", singleListCount(&sl));
    172   singleListLast(&sl) = "22 qwe";
    173   logVarG(sl.last->elem);
    174   logVarG(sl.last->prev->elem);
    175   logI("push %b", singleListPush(&sl));
    176   logI("count %zu", singleListCount(&sl));
    177   singleListLast(&sl) = "qwe33333";
    178   singleListForEachDown(&sl, node) {
    179     logI("elem: %s", node->elem);
    180   }
    181   singleListForEachElemDown(&sl, i) {
    182     logI("elem: %s", i);
    183   }
    184   singleListForEachElemPDown(&sl, i) {
    185     logI("elem: %s", *i);
    186   }
    187   logI("count %zu", singleListCount(&sl));
    188   var u = singleListUnlinkPrev(&sl, sl.last);
    189   //var u = singleListUnlinkLast(&sl);
    190   logI("count %zu", singleListCount(&sl));
    191   AT;
    192   //singleListInsertBefore(&sl, sl.head, u);
    193   singleListInsertLast(&sl, u);
    194   var add = singleListAddBefore(&sl, sl.head);
    195   singleListFirst(&sl) = "4444 add qwe";
    196   singleListForEachElemPDown(&sl, i) {
    197     logI("elem: %s", *i);
    198   }
    199   logI("count %zu", singleListCount(&sl));
    200   var idx = singleListDelPrev(&sl, sl.last);
    201   logI("count %zu", singleListCount(&sl));
    202   logI("pop %b", singleListPop(&sl));
    203   logI("count %zu", singleListCount(&sl));
    204   logI("pop %b", singleListPop(&sl));
    205   logI("count %zu", singleListCount(&sl));
    206   logI("pop %b", singleListPop(&sl));
    207   logI("count %zu", singleListCount(&sl));
    208   singleListFree(&sl);
    209 
    210   // read/write list
    211   // define list:
    212   singleListT(sllt, u8);
    213 
    214   // declare list:
    215   sllt sll;
    216 
    217   // initialize:
    218   singleListInit(&sll);
    219 
    220   singleListPush(&sll);
    221   //singleListFirst(&sll) = 1;
    222   singleListLast(&sll) = 1;
    223 
    224   //singleListAddBefore(&sll, sll.head);
    225   singleListPush(&sll);
    226   //singleListFirst(&sll) = 2;
    227   singleListLast(&sll) = 2;
    228   //singleListAddBefore(&sll, sll.head);
    229   singleListPush(&sll);
    230   //singleListFirst(&sll) = 3;
    231   singleListLast(&sll) = 3;
    232   singleListForEachDown(&sll, i) {
    233     logI("uint elem: %d", singleListElem(i));
    234   }
    235   put;
    236   singleListWriteFilename(&sll, "slist.dat");
    237 
    238   singleListPush(&sll);
    239   singleListLast(&sll) = 222;
    240   singleListPush(&sll);
    241   singleListLast(&sll) = 33;
    242   singleListFirst(&sll) = 0;
    243 
    244   singleListReadFilename(&sll, "slist.dat");
    245 
    246   singleListForEachDown(&sll, i) {
    247     logI("uint elem: %d", singleListElem(i));
    248   }
    249 
    250   singleListFree(&sll);
    251   }
    252 
    253   s();
    254 
    255 
    256   void se(void) {
    257 
    258   // define list:
    259   singleListeT(slt, char*);
    260 
    261   // declare list:
    262   slt sl;
    263 
    264   singleListeNodesT(&sl) da;
    265   singleListeFreeNodesT(&sl) fre;
    266 
    267   singleListeStoreInit(&da, &fre);
    268 
    269   logVarG(sizeof sl);
    270 
    271   // initialize:
    272   singleListeInit(&sl, &da, &fre);
    273 
    274   singleListeNodeType(&sl) sE;
    275 
    276   logI("isEmpty %b", singleListeIsEmpty(&sl));
    277   logI("count %zu", singleListeCount(&sl));
    278   logI("push %b", singleListePush(&sl));
    279   logI("count %zu", singleListeCount(&sl));
    280   singleListeFirst(&sl) = "qwe";
    281   logVarG(singleListeLast(&sl));
    282   singleListeLastNode(&sl)->elem = "QWER";
    283   logVarG(singleListeElem(sl.head));
    284   logVarG(sl.head->prev);
    285   logI("push %b", singleListePush(&sl));
    286   logI("count %zu", singleListeCount(&sl));
    287   singleListeLast(&sl) = "22 qwe";
    288   logVarG(sl.last->elem);
    289   logVarG(sl.last->prev->elem);
    290   logI("push %b", singleListePush(&sl));
    291   logI("count %zu", singleListeCount(&sl));
    292   singleListeLast(&sl) = "qwe33333";
    293   singleListeForEachDown(&sl, node) {
    294     logI("elem: %s", node->elem);
    295   }
    296   singleListeForEachElemDown(&sl, i) {
    297     logI("elem: %s", i);
    298   }
    299   singleListeForEachElemPDown(&sl, i) {
    300     logI("elem: %s", *i);
    301   }
    302   logI("count %zu", singleListeCount(&sl));
    303   var u = singleListeUnlinkPrev(&sl, sl.last);
    304   //var u = singleListeUnlinkLast(&sl);
    305   logI("count %zu", singleListeCount(&sl));
    306   AT;
    307   //singleListeInsertBefore(&sl, sl.head, u);
    308   singleListeInsertLast(&sl, u);
    309   var add = singleListeAddBefore(&sl, sl.head);
    310   singleListeFirst(&sl) = "4444 add qwe";
    311   singleListeForEachElemPDown(&sl, i) {
    312     logI("elem: %s", *i);
    313   }
    314   logI("count %zu", singleListeCount(&sl));
    315   var idx = singleListeDelPrev(&sl, sl.last);
    316   logI("count %zu", singleListeCount(&sl));
    317   logI("pop %b", singleListePop(&sl));
    318   logI("count %zu", singleListeCount(&sl));
    319   logI("pop %b", singleListePop(&sl));
    320   logI("count %zu", singleListeCount(&sl));
    321   logI("pop %b", singleListePop(&sl));
    322   logI("count %zu", singleListeCount(&sl));
    323   singleListeFree(&sl);
    324 
    325   singleListeStoreFree(&da, &fre);
    326 
    327   // read/write list
    328   // define list:
    329   singleListeT(sllt, u8);
    330 
    331   // declare list:
    332   sllt sll;
    333 
    334   singleListeNodesPtrT(&sll) da8;
    335   singleListeFreeNodesPtrT(&sll) fre8;
    336 
    337   da8  = malloc(sizeof(singleListeNodesT(&sll)));
    338   fre8 = malloc(sizeof(singleListeFreeNodesT(&sll)));
    339 
    340   singleListeStoreInit(da8, fre8);
    341 
    342   // initialize:
    343   singleListeInit(&sll, da8, fre8);
    344 
    345   singleListePush(&sll);
    346   //singleListeFirst(&sll) = 1;
    347   singleListeLast(&sll) = 1;
    348 
    349   //singleListeAddBefore(&sll, sll.head);
    350   singleListePush(&sll);
    351   //singleListeFirst(&sll) = 2;
    352   singleListeLast(&sll) = 2;
    353   //singleListeAddBefore(&sll, sll.head);
    354   singleListePush(&sll);
    355   //singleListeFirst(&sll) = 3;
    356   singleListeLast(&sll) = 3;
    357   singleListeForEachDown(&sll, i) {
    358     logI("uint elem: %d", singleListeElem(i));
    359   }
    360   put;
    361   singleListeWriteFilename(&sll, "slist.dat");
    362 
    363   singleListePush(&sll);
    364   singleListeLast(&sll) = 222;
    365   singleListePush(&sll);
    366   singleListeLast(&sll) = 33;
    367   singleListeFirst(&sll) = 0;
    368 
    369   singleListeReadFilename(&sll, "slist.dat");
    370 
    371   singleListeForEachDown(&sll, i) {
    372     logI("uint elem: %d", singleListeElem(i));
    373   }
    374 
    375   singleListeFree(&sll);
    376 
    377   singleListeStoreFree(da8, fre8);
    378   }
    379 
    380   se();
    381 }
    382 // vim: set expandtab ts=2 sw=2: