From b13763d32765aaebcaa7ad0b2f5cd10282b1f413 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 16:15:57 +0000 Subject: [PATCH 1/7] Remove unnecessary includes --- LifeAPI.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/LifeAPI.h b/LifeAPI.h index 495c07f..1194256 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -3,11 +3,8 @@ //Written by Michael Simkin 2014 #include -#include -#include #include #include -#include #define N 64 #define CAPTURE_COUNT 10 #define MAX_ITERATIONS 200 From 78af29ab15ce075a7e83b5544916044789b7b610 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 16:18:13 +0000 Subject: [PATCH 2/7] Use enum instead of string for op --- LifeAPI.h | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/LifeAPI.h b/LifeAPI.h index 1194256..0014e17 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -21,6 +21,9 @@ # define __builtin_popcount __popcnt64 #endif +enum CopyType { COPY, OR, XOR, AND }; +enum EvolveType { EVOLVE, LEAVE }; + typedef struct { int min; @@ -151,25 +154,25 @@ void Print(LifeState *state) } -void Copy(LifeState* main, LifeState* delta, char* op) +void Copy(LifeState* main, LifeState* delta, CopyType op) { - if(strcmp(op,"copy") == 0) + if(op == COPY) { for(int i = 0; i < N; i++) main->state[i] = delta->state[i]; } - if(strcmp(op,"or") == 0) + if(op == OR) { for(int i = 0; i < N; i++) main->state[i] |= delta->state[i]; } - if(strcmp(op,"and") == 0) + if(op == AND) { for(int i = 0; i < N; i++) main->state[i] &= delta->state[i]; } - if(strcmp(op,"xor") == 0) + if(op == XOR) { for(int i = 0; i < N; i++) main->state[i] ^= delta->state[i]; @@ -181,7 +184,7 @@ void Copy(LifeState* main, LifeState* delta, char* op) void Copy(LifeState* main, LifeState* delta) { - Copy(main, delta, "copy"); + Copy(main, delta, COPY); } int GetPop(LifeState* state) @@ -264,7 +267,7 @@ int ContainsInverse(LifeState* main, LifeState* inverseSpark) ClearData(Temp); Copy(Temp, main); Inverse(Temp); - Copy(Temp, inverseSpark, "and"); + Copy(Temp, inverseSpark, AND); return AreEqual(Temp, inverseSpark); } @@ -273,7 +276,7 @@ int Contains(LifeState* main, LifeState* spark) { ClearData(Temp); Copy(Temp, main); - Copy(Temp, spark, "and"); + Copy(Temp, spark, AND); return AreEqual(Temp, spark); } @@ -719,7 +722,7 @@ void Run(int numIter) void Join(LifeState* main, LifeState* delta) { - Copy(main, delta , "or"); + Copy(main, delta , OR); } void Join(LifeState* main, LifeState* delta, int dx, int dy) @@ -816,7 +819,7 @@ typedef struct } LifeIterator; -LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h, int s, char* op) +LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h, int s, EvolveType op) { LifeIterator* result = (LifeIterator*)(malloc(sizeof(LifeIterator))); @@ -841,7 +844,7 @@ LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h, int s, c result->States[i] = NewState(); Copy(result->States[i], Temp); - if(strcmp("evolve", op) == 0) + if(op == EVOLVE) Evolve(Temp, 1); } @@ -851,7 +854,7 @@ LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h, int s, c LifeIterator* NewIterator(LifeState* states[], int x, int y, int w, int h, int s) { - LifeIterator* result = NewIterator(states[0], x, y, w, h, s, "leave"); + LifeIterator* result = NewIterator(states[0], x, y, w, h, s, LEAVE); for(int i = 0; i < s; i++) { @@ -864,12 +867,12 @@ LifeIterator* NewIterator(LifeState* states[], int x, int y, int w, int h, int s LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h, int s) { - return NewIterator(state, x, y, w, h, s, "evolve"); + return NewIterator(state, x, y, w, h, s, EVOLVE); } LifeIterator* NewIterator(LifeState* state, int x, int y, int w, int h) { - return NewIterator(state, x, y, w, h, 1, "leave"); + return NewIterator(state, x, y, w, h, 1, LEAVE); } LifeIterator* NewIterator(int x, int y, int w, int h) From acae7fd9d1340325a0761aa846786b6c107b69e6 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 16:29:27 +0000 Subject: [PATCH 3/7] Specify const for string arguments --- LifeAPI.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/LifeAPI.h b/LifeAPI.h index 0014e17..6fc0218 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -459,7 +459,7 @@ void Transform(LifeState* state, int dx, int dy) Move(state, dx, dy); } -int Parse(LifeState* lifeState, char* rle, int dx, int dy) +int Parse(LifeState* lifeState, const char* rle, int dx, int dy) { char ch; int cnt, i, j; @@ -533,12 +533,12 @@ int Parse(LifeState* lifeState, char* rle, int dx, int dy) return SUCCESS; } -int Parse(LifeState* lifeState, char* rle) +int Parse(LifeState* lifeState, const char* rle) { return Parse(lifeState, rle, 0, 0); } -int Parse(LifeState* lifeState, char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) +int Parse(LifeState* lifeState, const char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) { int result = Parse(lifeState, rle); @@ -549,7 +549,7 @@ int Parse(LifeState* lifeState, char* rle, int dx, int dy, int dxx, int dxy, int } -LifeState* NewState(char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) +LifeState* NewState(const char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) { LifeState* result = NewState(); Parse(result, rle); @@ -558,7 +558,7 @@ LifeState* NewState(char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dy return result; } -LifeState* NewState(char* rle, int dx, int dy) +LifeState* NewState(const char* rle, int dx, int dy) { LifeState* result = NewState(); Parse(result, rle, dx, dy); @@ -566,7 +566,7 @@ LifeState* NewState(char* rle, int dx, int dy) return result; } -LifeState* NewState(char* rle) +LifeState* NewState(const char* rle) { return NewState(rle, 0, 0); } @@ -761,12 +761,12 @@ void PutState(LifeState* state, int dx, int dy, int dxx, int dxy, int dyx, int d PutState(Temp); } -void PutState(LifeState* state, char* op) +void PutState(LifeState* state, CopyType op) { Copy(GlobalState, state, op); } -int PutState(char* rle) +int PutState(const char* rle) { ClearData(Temp); int result = Parse(Temp, rle); @@ -777,7 +777,7 @@ int PutState(char* rle) return result; } -int PutState(char* rle, int x, int y) +int PutState(const char* rle, int x, int y) { ClearData(Temp); int result = Parse(Temp, rle, x, y); @@ -788,7 +788,7 @@ int PutState(char* rle, int x, int y) return result; } -int PutState(char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) +int PutState(const char* rle, int dx, int dy, int dxx, int dxy, int dyx, int dyy) { ClearData(Temp); int result = Parse(Temp, rle); @@ -885,7 +885,7 @@ void Print(LifeIterator* iter) { printf("\n(%d, %d, %d)", iter->curx, iter->cury, iter->curs); } -void Print(LifeIterator* iter, char* name) +void Print(LifeIterator* iter, const char* name) { printf("\nSetCurrent(%s, %d, %d, %d);", name, iter->curx, iter->cury, iter->curs); } From 87e466b50b0e35ab01193bffce24b90e4f30f7a5 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 17:25:38 +0000 Subject: [PATCH 4/7] Direct implementation of Contains and ContainsInverse --- LifeAPI.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/LifeAPI.h b/LifeAPI.h index 6fc0218..51f9bb7 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -264,21 +264,21 @@ int AreEqual(LifeState* pat1, LifeState* pat2) int ContainsInverse(LifeState* main, LifeState* inverseSpark) { - ClearData(Temp); - Copy(Temp, main); - Inverse(Temp); - Copy(Temp, inverseSpark, AND); - return AreEqual(Temp, inverseSpark); + for(int i = 0; i < N; i++) + if(main->state[i] & inverseSpark->state[i]) + return NO; + + return YES; } int Contains(LifeState* main, LifeState* spark) { - ClearData(Temp); - Copy(Temp, main); - Copy(Temp, spark, AND); - - return AreEqual(Temp, spark); + for(int i = 0; i < N; i++) + if(spark->state[i] & ~main->state[i]) + return NO; + + return YES; } int Contains(LifeState* spark) From b76c6bc64e2ec4833f9220e60ed1c2ed715d3093 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 17:28:41 +0000 Subject: [PATCH 5/7] Some renamings --- LifeAPI.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/LifeAPI.h b/LifeAPI.h index 51f9bb7..0c320b3 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -262,10 +262,10 @@ int AreEqual(LifeState* pat1, LifeState* pat2) } -int ContainsInverse(LifeState* main, LifeState* inverseSpark) +int AreDisjoint(LifeState* pat1, LifeState* pat2) { for(int i = 0; i < N; i++) - if(main->state[i] & inverseSpark->state[i]) + if(pat1->state[i] & pat2->state[i]) return NO; return YES; @@ -281,14 +281,14 @@ int Contains(LifeState* main, LifeState* spark) return YES; } -int Contains(LifeState* spark) +int AllOn(LifeState* spark) { return Contains(GlobalState, spark); } -int ContainsInverse(LifeState* inverseSpark) +int AllOff(LifeState* spark) { - return ContainsInverse(GlobalState, inverseSpark); + return AreDisjoint(GlobalState, spark); } LifeState* NewState() { @@ -1065,27 +1065,27 @@ int Validate(LifeIterator *iter1, LifeIterator *iter2) typedef struct { - LifeState* target; - LifeState* inverse; + LifeState* wanted; + LifeState* unwanted; } LifeTarget; -LifeTarget* NewTarget(LifeState* target, LifeState* inverse) +LifeTarget* NewTarget(LifeState* wanted, LifeState* unwanted) { LifeTarget* result = (LifeTarget*)(malloc(sizeof(LifeTarget))); - result->target = NewState(); - result->inverse = NewState(); + result->wanted = NewState(); + result->unwanted = NewState(); - Copy(result->target, target); - Copy(result->inverse, inverse); + Copy(result->wanted, wanted); + Copy(result->unwanted, unwanted); return result; } int ContainsTarget(LifeState* state, LifeTarget* target) { - if(Contains(state, target->target) == YES && ContainsInverse(state, target->inverse) == YES) + if(Contains(state, target->wanted) == YES && AreDisjoint(state, target->unwanted) == YES) return YES; else return NO; @@ -1098,8 +1098,8 @@ int ContainsTarget(LifeTarget* target) void FreeTarget(LifeTarget* iter) { - free(iter -> inverse); - free(iter -> target); + free(iter -> wanted); + free(iter -> unwanted); free(iter); } From 7343db510800742b0c79d63ec60ad41bbddfc1d4 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 17:30:29 +0000 Subject: [PATCH 6/7] Add function GetBoundary --- LifeAPI.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/LifeAPI.h b/LifeAPI.h index 0c320b3..5aa6443 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -1103,3 +1103,21 @@ void FreeTarget(LifeTarget* iter) free(iter); } + +void GetBoundary(LifeState* state, LifeState* boundary) +{ + for(int i = 0; i < N; i++) { + uint64_t col = state->state[i]; + Temp->state[i] = CirculateLeft(col) | CirculateRight(col); + } + + boundary->state[0] = Temp->state[N-1] | Temp->state[0] | Temp->state[1]; + + for(int i = 1; i < N-1; i++) + boundary->state[i] = Temp->state[i-1] | Temp->state[i] | Temp->state[i+1]; + + boundary->state[N-1] = Temp->state[N-2] | Temp->state[N-1] | Temp->state[0]; + + for(int i = 0; i < N; i++) + boundary->state[i] &= ~state->state[i]; +} From 638a0cb526385171f8bceaabcd34f7d317b94914 Mon Sep 17 00:00:00 2001 From: ceebo Date: Sat, 13 Dec 2014 19:13:47 +0000 Subject: [PATCH 7/7] Fix bug in GetBoundary --- LifeAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LifeAPI.h b/LifeAPI.h index 5aa6443..3da854c 100644 --- a/LifeAPI.h +++ b/LifeAPI.h @@ -1108,7 +1108,7 @@ void GetBoundary(LifeState* state, LifeState* boundary) { for(int i = 0; i < N; i++) { uint64_t col = state->state[i]; - Temp->state[i] = CirculateLeft(col) | CirculateRight(col); + Temp->state[i] = col | CirculateLeft(col) | CirculateRight(col); } boundary->state[0] = Temp->state[N-1] | Temp->state[0] | Temp->state[1];