Sulaiman Mustafa avatar Sulaiman Mustafa committed 6126101

أول وديعة...

Comments (0)

Files changed (1)

+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <stdint.h>
+#include <inttypes.h>
+
+// Chain.. because using "array" is soo last year!
+// {size, length, [outside world ptr], ...}
+
+// Chain meta type
+#define CLT     uint32_t*
+#define CLTS    sizeof(CLT)*2 
+#define chain_len(c) (*( ( (CLT) (((void *)c)-CLTS) )+1 ))
+//#define m_set_length(c, length) {*(((CLT) c)+1) = length;}
+
+#define chain_new(cell_type) (cell_type*) chain_new_(sizeof(cell_type))
+void* chain_new_(uint8_t size){
+    void *c;
+    if (!size) abort();
+    if (!(c = malloc(size+CLTS))) abort();
+    *((CLT) c) = size;
+    *(((CLT) c)+1) = 0;
+    return c+CLTS;
+}
+void chain_free(void *c){ free(c-CLTS);}
+
+#define chain_insert(buf, what, at) {buf = chain_resize_(buf, (at), 1); buf[((at)>chain_len(buf))?(chain_len(buf)-1):(at)]=(what);}
+#define chain_push(buf, what) chain_insert(buf, what, ~((uint32_t) 0))
+#define chain_remove(buf, where) chain_resize_(buf, where, -1)
+#define chain_pop(buf) buf[chain_len(buf)-1] | !(chain_remove(buf, ~((uint32_t) 0))) 
+
+void* chain_resize_(void *c, uint32_t where, int8_t how){
+    uint32_t size, length;
+    c-=CLTS;
+    size = *((CLT)c);
+    length = *(((CLT) c)+1);
+    
+    //TODO if length == ~uint32_t check for fullness
+    if (how == 1) {
+        if (!(c=realloc(c, CLTS+size*length+size))) abort();
+        if (where< length) {
+            memmove(c+CLTS+where*size+size, c+CLTS+where*size, (CLTS+length*size)-(CLTS+where*size));
+        }
+        ++(*(((CLT) c)+1));
+    }
+    else if (how == -1) {
+        if (where< length-1) {
+            memmove(c+CLTS+where*size, c+CLTS+where*size+size, (CLTS+length*size)-(CLTS+where*size+size));
+        }        
+        if (!(c=realloc(c, CLTS+size*length-size))) abort();
+        --(*(((CLT) c)+1));
+    }
+    else abort();
+    
+    return c+CLTS;
+}
+
+
+
+void print_info(void *c){
+    unsigned int i;
+    long int *x = c;
+    uint32_t size, length;
+    length = chain_len(c);
+    c-=CLTS;
+    size = *((CLT)c);
+    
+    fprintf(stderr, "cell size: %d\ncell count: %d\nDATA:\n", size, length);
+    
+    for (i=0; i<length; ++i){
+        fprintf(stderr, "cell %u: %ld\n", i,  x[i]);
+    }
+}
+
+
+int main (){
+    long int* rofl = NULL;
+    rofl = chain_new(long int);
+    
+    chain_push(rofl, 15);
+    print_info(rofl);
+    chain_push(rofl, 30);
+    print_info(rofl);
+    chain_push(rofl, 45);
+    print_info(rofl);
+    chain_push(rofl, 60);
+    print_info(rofl);
+    chain_insert(rofl, 16, 1);
+    print_info(rofl);
+    chain_insert(rofl, 2, 1);
+    print_info(rofl);
+    chain_insert(rofl, 1, 1);
+    print_info(rofl);
+    chain_remove(rofl, 1);
+    print_info(rofl);
+    printf("--%ld--\n", chain_pop(rofl));
+    print_info(rofl);
+    chain_free(rofl);
+    return 0;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.