Skip to content
Browse files

demo

  • Loading branch information...
1 parent 586e772 commit 1aec1df9fb2248ee4de66f6b8f17915859dbe943 @pi8027 committed
Showing with 562 additions and 35 deletions.
  1. +296 −0 demo/curses_frontend.c
  2. +256 −16 demo/nqueens.c
  3. +2 −5 demo/nqueens.hs
  4. +4 −8 demo/simple_frontend.c
  5. +3 −5 demo/yadorigi.c
  6. +1 −1 demo/yadorigi.h
View
296 demo/curses_frontend.c
@@ -0,0 +1,296 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ncurses.h>
+#include <unistd.h>
+#include "yadorigi.h"
+
+#define DEFAULT_BOARD_SIZE 5
+#define MAX_BOARD_SIZE 32
+
+typedef struct
+{
+ int x,y;
+} point_t;
+
+int abs(int n)
+{
+ return 0 < n?n:-n;
+}
+
+unsigned int calc_pages(unsigned int all_items,unsigned page_items)
+{
+ return !all_items?0:(all_items-1)/page_items+1;
+}
+
+int input_nqueens_data(WINDOW *mainwin,size_t *size_,size_t *points_size,point_t **points_)
+{
+ size_t size = DEFAULT_BOARD_SIZE,iter = 0,iter_,x = 0,y = 0;
+ int *points = malloc(sizeof(int)*MAX_BOARD_SIZE);
+ WINDOW *subwindow;
+ if(!points)return 0;
+ while(iter != DEFAULT_BOARD_SIZE)points[iter++] = -1;
+ wclear(mainwin);
+ wrefresh(mainwin);
+ subwindow = subwin(mainwin,size+2,size+2,0,0);
+ goto REWRITE_BOARD;
+ while(1){
+ int getf = wgetch(subwindow);
+ switch(getf){
+ case '\n':
+ delwin(subwindow);
+ *size_ = size;
+ iter = 0,iter_ = 0;
+ *points_ = malloc(sizeof(point_t)*(size+1));
+ if(!*points_){
+ free(points);
+ return 0;
+ }
+ while(iter != size){
+ if(0 <= points[iter]){
+ (*points_)[iter_].x = iter;
+ (*points_)[iter_].y = points[iter];
+ iter_++;
+ }
+ iter++;
+ }
+ *points_size = iter_;
+ free(points);
+ return 1;
+ case 'q':
+ delwin(subwindow);
+ free(points);
+ return 2;
+ case 'h':
+ if(0 < x)x--;
+ goto MOVE_CURSOR;
+ case 'j':
+ if(y < size-1)y++;
+ goto MOVE_CURSOR;
+ case 'k':
+ if(0 < y)y--;
+ goto MOVE_CURSOR;
+ case 'l':
+ if(x < size-1)x++;
+ goto MOVE_CURSOR;
+ case 'a':
+ if(1 < size){
+ size--;
+ if(x == size)x--;
+ if(y == size)y--;
+ iter = 0;
+ while(iter != size){
+ if(points[iter] == size)points[iter] = -1;
+ iter++;
+ }
+ goto REWRITE_BOARD;
+ }
+ break;
+ case 's':
+ if(size < MAX_BOARD_SIZE){
+ points[size] = -1;
+ size++;
+ goto REWRITE_BOARD;
+ }
+ break;
+ case ' ':
+ if(points[x] == y)points[x] = -1;
+ else{
+ iter = 0;
+ while(iter != size){
+ if(points[iter] != -1 && (points[iter] == y ||
+ abs(x-iter) == abs(y-points[iter])))points[iter] = -1;
+ iter++;
+ }
+ points[x] = y;
+ }
+ REWRITE_BOARD:
+ wclear(subwindow);
+ wrefresh(subwindow);
+ wresize(subwindow,size+2,size+2);
+ wborder(subwindow,'|','|','-','-','+','+','+','+');
+ iter = 0;
+ while(iter != size){
+ if(0 <= points[iter])mvwaddch(subwindow,points[iter]+1,iter+1,'*');
+ iter++;
+ }
+ MOVE_CURSOR:
+ wmove(subwindow,y+1,x+1);
+ wrefresh(subwindow);
+ }
+ }
+}
+
+int get_result(size_t size,size_t points_size,point_t *points,int ***result)
+{
+ size_t iter = 0,iter_,result_size;
+ int hs_size = size,***hs_points = malloc(sizeof(int **)*(points_size+1))
+ ,***hs_result,errcode;
+ FILE *pin,*pout;
+ if(!hs_points)return 0;
+ while(iter != points_size){
+ hs_points[iter] = malloc(sizeof(int *)*2);
+ if(!hs_points[iter]){
+ free_iituplelist(hs_points);
+ return 0;
+ }
+ hs_points[iter][0] = malloc(sizeof(int));
+ hs_points[iter][1] = malloc(sizeof(int));
+ if(!hs_points[iter][0] || !hs_points[iter][1]){
+ free(hs_points[iter][0]);
+ free(hs_points[iter][1]);
+ free(hs_points[iter]);
+ hs_points[iter] = NULL;
+ free_iituplelist(hs_points);
+ return 0;
+ }
+ *hs_points[iter][0] = points[iter].x;
+ *hs_points[iter][1] = points[iter].y;
+ iter++;
+ }
+ hs_points[points_size] = NULL;
+ errcode = popen2("runyadorigi ./nqueens.hs",&pout,&pin);
+ if(errcode < 0){
+ free_iituplelist(hs_points);
+ return 0;
+ }
+ output_int(pin,&hs_size);
+ fputc('\n',pin);
+ output_iituplelist(pin,hs_points);
+ fputc('\n',pin);
+ fclose(pin);
+ hs_result = parse_intlistlist(pout);
+ fclose(pout);
+ if(!hs_result)return 0;
+ result_size = parray_length((void **)hs_result);
+ *result = malloc(sizeof(int *)*(result_size+1));
+ if(!*result){
+ free_intlistlist(hs_result);
+ return 0;
+ }
+ iter = 0;
+ while(iter != result_size){
+ (*result)[iter] = malloc(sizeof(int)*size);
+ if(!(*result)[iter]){
+ while(iter--)free((*result)[iter]);
+ free(*result);
+ return 0;
+ }
+ iter_ = 0;
+ while(iter_ != size){
+ (*result)[iter][iter_] = *hs_result[iter][iter_];
+ iter_++;
+ }
+ iter++;
+ }
+ (*result)[result_size] = NULL;
+ return 1;
+}
+
+int output_result(WINDOW *mainwin,size_t size,int **result)
+{
+ size_t iter,iter_x,iter_y,page = 0,pages,page_items;
+ unsigned int mainwin_size_x,mainwin_size_y;
+ WINDOW **subwins;
+ getmaxyx(mainwin,mainwin_size_y,mainwin_size_x);
+ page_items = (mainwin_size_x/(size+3))*(mainwin_size_y/(size+3));
+ pages = calc_pages(parray_length((void **)result),page_items);
+ if(!pages){
+ wclear(mainwin);
+ mvwinsstr(mainwin,1,1,"empty result");
+ wrefresh(mainwin);
+ return 1;
+ }
+ subwins = malloc(sizeof(WINDOW *)*page_items);
+ if(!subwins)return 0;
+ iter = 0;
+ iter_y = 0;
+ while(iter_y != mainwin_size_y/(size+3)){
+ iter_x = 0;
+ while(iter_x != mainwin_size_x/(size+3)){
+ subwins[iter] = subwin(mainwin,size+2,size+2,iter_y*(size+3),iter_x*(size+3));
+ if(!subwins[iter]){
+ while(iter--)delwin(subwins[iter]);
+ free(subwins);
+ return 0;
+ }
+ iter_x++;
+ iter++;
+ }
+ iter_y++;
+ }
+ goto REWRITE_BOARD;
+ while(1){
+ int getf = wgetch(mainwin);
+ switch(getf){
+ case '\n':
+ case 'q':
+ iter = 0;
+ while(iter != page_items)delwin(subwins[iter++]);
+ free(subwins);
+ wclear(mainwin);
+ wrefresh(mainwin);
+ return (getf == '\n'?1:2);
+ case 'j':
+ if(page < pages-1){
+ page++;
+ goto REWRITE_BOARD;
+ }
+ break;
+ case 'k':
+ if(page){
+ page--;
+ goto REWRITE_BOARD;
+ }
+ break;
+ REWRITE_BOARD:
+ wclear(mainwin);
+ iter = 0;
+ while(iter != page_items && result[page_items*page+iter]){
+ iter_x = 0;
+ while(iter_x != size){
+ mvwaddch(subwins[iter],result[page_items*page+iter][iter_x]+1,iter_x+1,'*');
+ iter_x++;
+ }
+ wborder(subwins[iter],'|','|','-','-','+','+','+','+');
+ iter++;
+ }
+ wrefresh(mainwin);
+ break;
+ }
+ }
+}
+
+void free_result(int **result)
+{
+ size_t iter = 0;
+ while(result[iter])free(result[iter++]);
+ free(result);
+}
+
+int main(void)
+{
+ int errcode;
+ initscr();
+ cbreak();
+ noecho();
+ while(1){
+ int **result;
+ size_t size,points_size;
+ point_t *points;
+ errcode = input_nqueens_data(stdscr,&size,&points_size,&points);
+ if(errcode == 0)return -1;
+ if(errcode == 2)break;
+ errcode = get_result(size,points_size,points,&result);
+ free(points);
+ if(errcode == 0)return -1;
+ errcode = output_result(stdscr,size,result);
+ free_result(result);
+ if(errcode == 0)return -1;
+ if(errcode == 2)break;
+ }
+ endwin();
+ return 0;
+}
+
View
272 demo/nqueens.c
@@ -2,6 +2,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ncurses.h>
+#include <unistd.h>
+#include "yadorigi.h"
+
+#define DEFAULT_BOARD_SIZE 5
+#define MAX_BOARD_SIZE 32
#define SWAP(a,b,size) \
do{ \
@@ -14,14 +20,21 @@
} \
}while(0)
+typedef struct
+{
+ int x,y;
+} point_t;
+
int abs(int n)
{
- if(0 <= n){
- return n;
- }
- else{
- return -n;
- }
+ return 0 < n?n:-n;
+}
+
+size_t parray_length(void **array)
+{
+ size_t iter = 0;
+ while(array[iter])iter++;
+ return iter;
}
unsigned int fact(unsigned int n)
@@ -122,18 +135,245 @@ unsigned int **nqueens(size_t size)
return NULL;
}
-int main(void)
+unsigned int calc_pages(unsigned int all_items,unsigned page_items)
{
- unsigned int size = 10,**result = nqueens(size);
- size_t iter1 = 0;
- while(result[iter1]){
- size_t iter2 = 0;
- while(iter2 != size){
- printf("%d ",result[iter1][iter2]);
- iter2++;
+ return !all_items?0:(all_items-1)/page_items+1;
+}
+
+int input_nqueens_data(WINDOW *mainwin,size_t *size_,size_t *points_size,point_t **points_)
+{
+ size_t size = DEFAULT_BOARD_SIZE,iter = 0,iter_,x = 0,y = 0;
+ int *points = malloc(sizeof(int)*MAX_BOARD_SIZE);
+ WINDOW *subwindow;
+ if(!points)return 0;
+ while(iter != DEFAULT_BOARD_SIZE)points[iter++] = -1;
+ wclear(mainwin);
+ wrefresh(mainwin);
+ subwindow = subwin(mainwin,size+2,size+2,0,0);
+ goto REWRITE_BOARD;
+ while(1){
+ int getf = wgetch(subwindow);
+ switch(getf){
+ case '\n':
+ delwin(subwindow);
+ *size_ = size;
+ iter = 0,iter_ = 0;
+ *points_ = malloc(sizeof(point_t)*(size+1));
+ if(!*points_){
+ free(points);
+ return 0;
+ }
+ while(iter != size){
+ if(0 <= points[iter]){
+ (*points_)[iter_].x = iter;
+ (*points_)[iter_].y = points[iter];
+ iter_++;
+ }
+ iter++;
+ }
+ *points_size = iter_;
+ free(points);
+ return 1;
+ case 'q':
+ delwin(subwindow);
+ free(points);
+ return 2;
+ case 'h':
+ if(0 < x)x--;
+ goto MOVE_CURSOR;
+ case 'j':
+ if(y < size-1)y++;
+ goto MOVE_CURSOR;
+ case 'k':
+ if(0 < y)y--;
+ goto MOVE_CURSOR;
+ case 'l':
+ if(x < size-1)x++;
+ goto MOVE_CURSOR;
+ case 'a':
+ if(1 < size){
+ size--;
+ if(x == size)x--;
+ if(y == size)y--;
+ iter = 0;
+ while(iter != size){
+ if(points[iter] == size)points[iter] = -1;
+ iter++;
+ }
+ goto REWRITE_BOARD;
+ }
+ break;
+ case 's':
+ if(size < MAX_BOARD_SIZE){
+ points[size] = -1;
+ size++;
+ goto REWRITE_BOARD;
+ }
+ break;
+ case ' ':
+ if(points[x] == y)points[x] = -1;
+ else{
+ iter = 0;
+ while(iter != size){
+ if(points[iter] != -1 && (points[iter] == y ||
+ abs(x-iter) == abs(y-points[iter])))points[iter] = -1;
+ iter++;
+ }
+ points[x] = y;
+ }
+ REWRITE_BOARD:
+ wclear(subwindow);
+ wrefresh(subwindow);
+ wresize(subwindow,size+2,size+2);
+ wborder(subwindow,'|','|','-','-','+','+','+','+');
+ iter = 0;
+ while(iter != size){
+ if(0 <= points[iter])mvwaddch(subwindow,points[iter]+1,iter+1,'*');
+ iter++;
+ }
+ MOVE_CURSOR:
+ wmove(subwindow,y+1,x+1);
+ wrefresh(subwindow);
}
- fputc('\n',stdout);
- iter1++;
}
}
+int result_filter(size_t size,size_t points_size,point_t *points,unsigned int *result)
+{
+ size_t iter = 0;
+ while(iter != points_size){
+ if(size <= points[iter].x || result[points[iter].x] != points[iter].y){
+ return 0;
+ }
+ iter++;
+ }
+ return 1;
+}
+
+int get_result(size_t size,size_t points_size,point_t *points,unsigned int ***result)
+{
+ size_t iter = 0,iter_ = 0;
+ *result = nqueens(size);
+ if(!*result){
+ return 0;
+ }
+ while((*result)[iter]){
+ if(result_filter(size,points_size,points,(*result)[iter])){
+ (*result)[iter_] = (*result)[iter];
+ iter_++;
+ }
+ else{
+ free((*result)[iter]);
+ }
+ iter++;
+ }
+ (*result)[iter_] = NULL;
+ return 1;
+}
+
+int output_result(WINDOW *mainwin,size_t size,unsigned int **result)
+{
+ size_t iter,iter_x,iter_y,page = 0,pages,page_items;
+ unsigned int mainwin_size_x,mainwin_size_y;
+ WINDOW **subwins;
+ getmaxyx(mainwin,mainwin_size_y,mainwin_size_x);
+ page_items = (mainwin_size_x/(size+3))*(mainwin_size_y/(size+3));
+ pages = calc_pages(parray_length((void **)result),page_items);
+ if(!pages){
+ wclear(mainwin);
+ mvwinsstr(mainwin,1,1,"empty result");
+ wrefresh(mainwin);
+ return 1;
+ }
+ subwins = malloc(sizeof(WINDOW *)*page_items);
+ if(!subwins)return 0;
+ iter = 0;
+ iter_y = 0;
+ while(iter_y != mainwin_size_y/(size+3)){
+ iter_x = 0;
+ while(iter_x != mainwin_size_x/(size+3)){
+ subwins[iter] = subwin(mainwin,size+2,size+2,iter_y*(size+3),iter_x*(size+3));
+ if(!subwins[iter]){
+ while(iter--)delwin(subwins[iter]);
+ free(subwins);
+ return 0;
+ }
+ iter_x++;
+ iter++;
+ }
+ iter_y++;
+ }
+ goto REWRITE_BOARD;
+ while(1){
+ int getf = wgetch(mainwin);
+ switch(getf){
+ case '\n':
+ case 'q':
+ iter = 0;
+ while(iter != page_items)delwin(subwins[iter++]);
+ free(subwins);
+ wclear(mainwin);
+ wrefresh(mainwin);
+ return (getf == '\n'?1:2);
+ case 'j':
+ if(page < pages-1){
+ page++;
+ goto REWRITE_BOARD;
+ }
+ break;
+ case 'k':
+ if(page){
+ page--;
+ goto REWRITE_BOARD;
+ }
+ break;
+ REWRITE_BOARD:
+ wclear(mainwin);
+ iter = 0;
+ while(iter != page_items && result[page_items*page+iter]){
+ iter_x = 0;
+ while(iter_x != size){
+ mvwaddch(subwins[iter],result[page_items*page+iter][iter_x]+1,iter_x+1,'*');
+ iter_x++;
+ }
+ wborder(subwins[iter],'|','|','-','-','+','+','+','+');
+ iter++;
+ }
+ wrefresh(mainwin);
+ break;
+ }
+ }
+}
+
+void free_result(unsigned int **result)
+{
+ size_t iter = 0;
+ while(result[iter])free(result[iter++]);
+ free(result);
+}
+
+int main(void)
+{
+ int errcode;
+ initscr();
+ cbreak();
+ noecho();
+ while(1){
+ unsigned int **result;
+ size_t size,points_size;
+ point_t *points;
+ errcode = input_nqueens_data(stdscr,&size,&points_size,&points);
+ if(errcode == 0)return -1;
+ if(errcode == 2)break;
+ errcode = get_result(size,points_size,points,&result);
+ free(points);
+ if(errcode == 0)return -1;
+ errcode = output_result(stdscr,size,result);
+ free_result(result);
+ if(errcode == 0)return -1;
+ if(errcode == 2)break;
+ }
+ endwin();
+ return 0;
+}
+
View
7 demo/nqueens.hs
@@ -5,14 +5,11 @@ import Data.List
import Control.Applicative
import Control.Monad
-nqueensFilter :: [Int] -> Bool
-nqueensFilter = all f.tails where
+nqueens :: Int -> [[Int]]
+nqueens n = filter (all f.tails) $ permutations [0..n-1] where
f [] = True
f (x:xs) = and $ zipWith ((/=).abs.subtract x) xs [1..]
-nqueens :: Int -> [[Int]]
-nqueens n = filter nqueensFilter $ permutations [0..n-1]
-
nqueensInterface :: Int -> [(Int,Int)] -> [[Int]]
nqueensInterface n cond = foldr (\(x,y) -> filter $ (y==).(!!x)) (nqueens n) cond
View
12 demo/simple_frontend.c
@@ -4,11 +4,6 @@
#include <string.h>
#include "yadorigi.h"
-typedef struct
-{
- unsigned int x,y;
-} point_t;
-
void *copy_with_alloc(const void *p,size_t size)
{
void *temp = malloc(size);
@@ -65,7 +60,7 @@ void stream_redirect(FILE *in,FILE *out)
int main(void)
{
- int *size,***points,getf;
+ int *size,***points,***result,getf;
size_t iter = 0;
FILE *pin,*pout;
size = fgetli(stdin);
@@ -94,13 +89,14 @@ int main(void)
}
iter++;
}
- popen2("runhaskell ./nqueens.hs",&pout,&pin);
+ popen2("runghc ./nqueens.hs",&pout,&pin);
output_int(pin,size);
fputc('\n',pin);
output_iituplelist(pin,points);
fputc('\n',pin);
fclose(pin);
- stream_redirect(pout,stdout);
+ result = parse_intlistlist(pout);
+ output_intlistlist(stdout,result);
fclose(pout);
return 0;
}
View
8 demo/yadorigi.c
@@ -56,12 +56,10 @@ int popen2(char *command,FILE **fr,FILE **fw)
Parser
*/
-unsigned int parray_length(void **array)
+size_t parray_length(void **array)
{
size_t iter = 0;
- while(array[iter]){
- iter++;
- }
+ while(array[iter])iter++;
return iter;
}
@@ -134,7 +132,7 @@ void **parse_list(parser_t parse_child,free_t free_child,FILE *input)
}
while(1){
if(iter%8 == 0){
- void **temp = realloc(result,sizeof(void *)*(iter+8));
+ void **temp = realloc(result,sizeof(void *)*(iter+9));
if(!temp){
goto ERROR;
}
View
2 demo/yadorigi.h
@@ -7,7 +7,7 @@ typedef void (*free_t)(void *);
int popen2(char *,FILE **,FILE **);
-unsigned int parray_length(void **);
+size_t parray_length(void **);
/* parser */

0 comments on commit 1aec1df

Please sign in to comment.
Something went wrong with that request. Please try again.