hashlist

type-safe random access double linked lists
git clone https://noulin.net/git/hashlist.git
Log | Files | Refs | README | LICENSE

main.c (2218B)


      1 #! /usr/bin/env sheepy
      2 #include "libsheepyObject.h"
      3 #include "hashlist.h"
      4 #include "shpPackages/hashfunctions/hashfunctions.h"
      5 
      6 /* hashlist definition and prototypes */
      7 #define listMembers char *v;
      8 
      9 hashlistDef(/* scope */, lists /* listst hashtable */, Lists /* functions: initLists... */, u64 /* key */, list /* listt value type */, u64 /* hash */, listMembers);
     10 
     11 #define HASHFUNC u64Hash // hash function from the hashfunctions spm package
     12 #define CMPFUNC  cmpU64
     13 #define FREEFUNC freeList
     14 
     15 hashlistFunctions(/* scope */, lists /* listst hashtable */, Lists /* functions: initLists... */, u64 /* key */, list /* listt value type */, u64 /* hash */, listMembers,
     16 int cmpU64(u64 k1, u64 k2) {
     17   return k1 == k2;
     18 }
     19 void freeList(u64 *k, listt *v) {
     20 }
     21     );
     22 
     23 #undef HASHFUNC
     24 #undef CMPFUNC
     25 #undef FREEFUNC
     26 
     27 int main(int ARGC, char** ARGV) {
     28 
     29   initLibsheepy(ARGV[0]);
     30   setLogMode(LOG_FUNC);
     31 
     32   listst hlists;
     33 
     34   initLists(&hlists);
     35 
     36   listt g;
     37 
     38   g.key  = 0;
     39   g.v    = "sdf";
     40 
     41 
     42   listt *head = hashlistCreateLists(&hlists, g);
     43   //listt *head = hashlistNewLists(&hlists, 0);
     44   head->v     = "sdf";
     45 
     46   logVarG(getLists(&hlists, 0).v);
     47 
     48   listt *G = findLists(&hlists, 0);
     49 
     50   logVarG(G->v);
     51 
     52   g.key = 1;
     53   g.v   = "22";
     54 
     55   G = hashlistAddAfterLists(&hlists, G, g);
     56 
     57   G    = hashlistPushLists(&hlists, G, 2);
     58   G->v = "333";
     59 
     60   G    = hashlistPushLists(&hlists, G, 3);
     61   G->v = "4444";
     62 
     63   g.key = 30;
     64   g.v   = "3!!!!4";
     65 
     66   hashlistAddBeforeLists(&hlists, G, g);
     67 
     68   G    = hashlistPrependLists(&hlists, G, 31);
     69   G->v = "----4";
     70 
     71   lForEach(node, head) {
     72     logVarG(node->v);
     73   }
     74 
     75   // unlink key 1 and insert after key 3
     76   listt *N = hashlistUnlinkLists(head->next);
     77   hashlistInsertAfterLists(G, N);
     78 
     79   put
     80   lForEach(node, head) {
     81     logVarG(node->v);
     82   }
     83 
     84   // delete key 1
     85   hashlistDelLists(&hlists, 1);
     86 
     87   put
     88   lForEach(node, head) {
     89     logVarG(node->v);
     90   }
     91 
     92   hashlistDelLists(&hlists, 3);
     93 
     94   put
     95   lForEach(node, head) {
     96     logVarG(node->v);
     97   }
     98 
     99   //hashlistFreeLists(&hlists, head);
    100   hashlistReleaseLists(&hlists, 0);
    101 
    102   put
    103   if (findLists(&hlists, 0)) {logI("found head!");}
    104   else                       logI(BLD GRN"not found head!"RST);
    105 
    106   freeLists(&hlists);
    107 }
    108 // vim: set expandtab ts=2 sw=2: