Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 68 additions & 50 deletions LifeAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
//Written by Michael Simkin 2014

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define N 64
#define CAPTURE_COUNT 10
#define MAX_ITERATIONS 200
Expand All @@ -24,6 +21,9 @@
# define __builtin_popcount __popcnt64
#endif

enum CopyType { COPY, OR, XOR, AND };
enum EvolveType { EVOLVE, LEAVE };

typedef struct
{
int min;
Expand Down Expand Up @@ -154,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];
Expand All @@ -184,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)
Expand Down Expand Up @@ -262,33 +262,33 @@ int AreEqual(LifeState* pat1, LifeState* pat2)
}


int ContainsInverse(LifeState* main, LifeState* inverseSpark)
int AreDisjoint(LifeState* pat1, LifeState* pat2)
{
ClearData(Temp);
Copy(Temp, main);
Inverse(Temp);
Copy(Temp, inverseSpark, "and");
return AreEqual(Temp, inverseSpark);
for(int i = 0; i < N; i++)
if(pat1->state[i] & pat2->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)
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()
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -558,15 +558,15 @@ 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);

return result;
}

LifeState* NewState(char* rle)
LifeState* NewState(const char* rle)
{
return NewState(rle, 0, 0);
}
Expand Down Expand Up @@ -722,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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -819,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)));

Expand All @@ -844,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);
}

Expand All @@ -854,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++)
{
Expand All @@ -867,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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -1098,8 +1098,26 @@ int ContainsTarget(LifeTarget* target)

void FreeTarget(LifeTarget* iter)
{
free(iter -> inverse);
free(iter -> target);
free(iter -> wanted);
free(iter -> unwanted);

free(iter);
}

void GetBoundary(LifeState* state, LifeState* boundary)
{
for(int i = 0; i < N; i++) {
uint64_t col = state->state[i];
Temp->state[i] = col | 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];
}