]> git.mdlowis.com Git - projs/tide.git/commitdiff
Implemented log coalescing for inserts
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 28 Oct 2016 16:36:41 +0000 (12:36 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 28 Oct 2016 16:36:41 +0000 (12:36 -0400)
buf.c

diff --git a/buf.c b/buf.c
index 72020e10a32521982edb19870ca4a61f2157a336..f145b6010a2ea690c4a5ecdfee31cb376a4bf523 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -94,12 +94,24 @@ void buf_init(Buf* buf) {
 }
 
 static void log_insert(Log** list, unsigned beg, unsigned end) {
-    Log* newlog  = (Log*)calloc(sizeof(Log), 1);
-    newlog->insert = true;
-    newlog->data.ins.beg = beg;
-    newlog->data.ins.end = end;
-    newlog->next = *list;
-    *list = newlog;
+    Log* log = *list;
+    if (!log || log->locked || !log->insert || (log->data.ins.beg && beg < log->data.ins.beg-1) || end > log->data.ins.end+1) {
+        //if (log) printf("%d %d %d %d\n", log->locked, !log->insert, beg < log->data.ins.beg-1, end > log->data.ins.end+1);
+        //if (log) printf("%u < %u\n", beg, log->data.ins.beg-1);
+        //printf("new %u-%u\n", beg, end);
+        Log* newlog  = (Log*)calloc(sizeof(Log), 1);
+        newlog->insert = true;
+        newlog->data.ins.beg = beg;
+        newlog->data.ins.end = end;
+        newlog->next = *list;
+        *list = newlog;
+    } else if (beg <= log->data.ins.beg) {
+        //puts("coalesce 1");
+        log->data.ins.beg--;
+    } else {
+        //puts("coalesce 2");
+        log->data.ins.end++;
+    }
 }
 
 static void log_delete(Log** list, unsigned off, Rune* r, size_t len) {