From ce26b4aaa58839a9781c4e250d605ba220ef9f07 Mon Sep 17 00:00:00 2001 From: xchaos Date: Tue, 5 Feb 2008 16:24:41 +0000 Subject: [PATCH] order_by_int, order_by_str, order_by_ascii + some examples git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@59 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 32 +++++++++++++++---------- demos/eq.c | 46 ++++++++++++++++++++++-------------- demos/lists.c | 48 ++++++++++++++++++++----------------- demos/seznamy.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 53 deletions(-) create mode 100644 demos/seznamy.c diff --git a/cll1.h b/cll1.h index 7192150..711b951 100644 --- a/cll1.h +++ b/cll1.h @@ -61,7 +61,7 @@ typedef FILE * file; typedef int bool; extern str NIL,EOL,OFS,IFS; extern unsigned RANDOM_SEED; -#define _EXPORT_GLOBALS str NIL="NULL", EOL="\n", OFS=" ", IFS=" \t\n"; unsigned RANDOM_SEED=0; +#define _EXPORT_GLOBALS str NIL="(null)", EOL="\n", OFS=" ", IFS=" \t\n"; unsigned RANDOM_SEED=0; /* .----------------------------------------------------------------------. / 1. C<<1 then buddhist section, updated 2008-02-04 xCh. '----------------------------------------------------------------------- */ @@ -109,11 +109,13 @@ extern unsigned RANDOM_SEED; #define get_str(...) _cll1_get_str(__VA_ARGS__,NIL) #define eq(A,B) (!_cll1_str_cmp(A,B)) #define eqi(A,B) (!_cll1_str_cmpi(A,B)) +//#define str_eq(A,B) eq(A,B) ? +//#define str_eqi(A,B) eqi(A,B) #define str_order(A,B) _cll1_str_coll(A,B) #define str_ascii(A,B) _cll1_str_cmp(A,B) #define str_len(...) _cll1__strlen(__VA_ARGS__,NIL) #define str_in(A,B) _cll1_str_str(A,B) -#define word_in_str(A,B) _cll1_wordinstr(A,B,0) +#define word_in(A,B) _cll1_wordinstr(A,B,0) #define print(...) _cll1_print(__VA_ARGS__,NIL) #define fprint(...) _cll1_fprint(__VA_ARGS__,NIL) #define file_print(...) _cll1_fprint(_F,__VA_ARGS__,NIL) @@ -134,14 +136,14 @@ extern unsigned RANDOM_SEED; //to do: seek //to do: store #define count(NODE,HEAD,VAR) { _NEW(I,0) for(NODE=HEAD; NODE || ((VAR=_I),0) ; (NODE=NODE->__next)?(_I++):0 ); } -#define order_by(K1,K2) (K1>K2) -#define desc_order_by(K1,K2) (K10) -#define desc_sort_by(K1,K2) (str_order(K1,K2)<0) -#define ascii_by(K1,K2) (str_ascii(K1,K2)>0) -#define desc_ascii_by(K1,K2) (str_ascii(K1,K2)<0) -#define insert(NODE,HEAD,EXP,K) { if(HEAD) { void **_L=NULL, *H=HEAD; /* <-' */ for_search(HEAD,H,EXP(HEAD->K,NODE->K)) { if(_L) {*_L=NODE; NODE->__next=HEAD; } else push(NODE,H); break; } else _L=(void *)&(HEAD->__next); if(!HEAD)*_L=NODE; HEAD=H; } else if (NODE) { push(NODE,HEAD); HEAD->__next=NULL; } } -#define sort(NODE,HEAD,EXP,K) { void *_NEXT; /* <-' */ NODE=HEAD; HEAD=NULL; do { _NEXT=NODE->__next; NODE->__next=NULL; insert(NODE,HEAD,EXP,K); NODE=_NEXT; } while(_NEXT); } +#define order_by_int(K1,K2) (K1>K2) +#define order_by_int_desc(K1,K2) (K10) +#define order_by_str_desc(K1,K2) (str_order(K1,K2)<0) +#define order_by_ascii(K1,K2) (str_ascii(K1,K2)>0) +#define order_by_ascii_desc(K1,K2) (str_ascii(K1,K2)<0) +#define insert(NODE,HEAD,ORDERBY,KEY) { if(HEAD) { void **_L=NULL, *H=HEAD; /* <-' */ for_search(HEAD,H,ORDERBY(HEAD->KEY,NODE->KEY)) { if(_L) {*_L=NODE; NODE->__next=HEAD; } else push(NODE,H); break; } else _L=(void *)&(HEAD->__next); if(!HEAD)*_L=NODE; HEAD=H; } else if (NODE) { push(NODE,HEAD); HEAD->__next=NULL; } } +#define sort(NODE,HEAD,ORDERBY,KEY) { void *_NEXT; /* <-' */ NODE=HEAD; HEAD=NULL; do { _NEXT=NODE->__next; NODE->__next=NULL; insert(NODE,HEAD,ORDERBY,KEY); NODE=_NEXT; } while(_NEXT); } /* .----------------------------------------------------------------------. / 7. C<<1 builtin iterators and conditionals, updated 2008-01-26 xCh. '----------------------------------------------------------------------- */ @@ -347,15 +349,19 @@ obsolete //#define _ASSERT(EXPR) assert(EXPR) /* .----------------------------------------------------------------------. - / 99. We have have done our homework - now let's have some fun: + / 98. We have have done our homework - now let's have some fun: '----------------------------------------------------------------------- */ #define _INIT_VARIABLES _EXPORT_GLOBALS _EXPORT_YES _EXPORT_STACKS _SQL_GLOBALS _CGI_GLOBALS #define _IMPL_FUNCTIONS _MALLOC _EXPORT_OUTS _EXPORT_ARGS _EXPORT_STRS #define _IMPL_INTERFACE //_SQL_IFC _CAKE_IFC _STD_IFC _X_IFC #define _IMPLEMENT_CLL1_LIBRARY _INIT_VARIABLES _IMPL_FUNCTIONS _IMPL_INTERFACE +#define _PROGRAM _cll1_program +#define _DECLARE_PROGRAM void _PROGRAM(int argc, char **argv) +#define program _IMPLEMENT_CLL1_LIBRARY _DECLARE_PROGRAM; int main(int argc, char **argv) { setlocale(LC_ALL, ""); _PROGRAM(argc,argv); return 0; } _DECLARE_PROGRAM #ifdef USE_PREPROCESSOR #include "_include.c" #endif - -#define program _IMPLEMENT_CLL1_LIBRARY void _program(int argc, char **argv); int main(int argc, char **argv) { setlocale(LC_ALL, ""); _program(argc,argv); return 0; } void _program(int argc, char **argv) +/* .----------------------------------------------------------------------. + / 99. C<<1 is up and running. It's your turn now to show something new. + '----------------------------------------------------------------------- */ #endif diff --git a/demos/eq.c b/demos/eq.c index 3466b74..10b6104 100644 --- a/demos/eq.c +++ b/demos/eq.c @@ -6,32 +6,42 @@ program a="¾luklý"; b="®LUKLÝ"; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order(\"%s\",\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq(\"%s\",\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi(\"%s\",\"%s\") = %s\n",a,b,btoa(eqi(a,b))); a="aabc"; b="axyz"; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order(\"%s\",\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq(\"%s\",\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi(\"%s\",\"%s\") = %s\n",a,b,btoa(eqi(a,b))); a="ABC"; b="abc"; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order(\"%s\",\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq(\"%s\",\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi(\"%s\",\"%s\") = %s\n",a,b,btoa(eqi(a,b))); a="abc"; b="abc"; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order(\"%s\",\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq(\"%s\",\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi(\"%s\",\"%s\") = %s\n",a,b,btoa(eqi(a,b))); + a=""; + b=""; + printf("str_order(\"%s\",\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq(\"%s\",\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi(\"%s\",\"%s\") = %s\n",a,b,btoa(eqi(a,b))); a=NULL; b="abc"; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order( %s ,\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq( %s ,\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi( %s ,\"%s\") = %s\n",a,b,btoa(eqi(a,b))); + a=NULL; + b=""; + printf("str_order( %s ,\"%s\") = %d\n",a,b,str_order(a,b)); + printf("eq( %s ,\"%s\") = %s\n",a,b,btoa(eq(a,b))); + printf("eqi( %s ,\"%s\") = %s\n",a,b,btoa(eqi(a,b))); a=NULL; b=NULL; - printf("str_order(\"%s\",\"%s\")=%d\n",a,b,str_order(a,b)); - printf("eq(\"%s\",\"%s\")=%s\n",a,b,btoa(eq(a,b))); - printf("eqi(\"%s\",\"%s\")=%s\n",a,b,btoa(eqi(a,b))); + printf("str_order( %s , %s ) = %d\n",a,b,str_order(a,b)); + printf("eq( %s ,%s ) = %s\n",a,b,btoa(eq(a,b))); + printf("eqi( %s , %s ) = %s\n",a,b,btoa(eqi(a,b))); } diff --git a/demos/lists.c b/demos/lists.c index 0f36e64..c92117c 100644 --- a/demos/lists.c +++ b/demos/lists.c @@ -11,50 +11,54 @@ program { Record record,records=NULL; - print("4x insert(record,records ,order_by,i);"); + print("4x insert(record,records,order_by,i);"); record=get_mem(Record); record->i=1; record->s="ddd"; - insert(record,records ,order_by,i); + insert(record,records,order_by_int,i); record=get_mem(Record); record->i=3; - record->s="bbbbb"; - insert(record,records ,order_by,i); + record->s="BBBB"; + insert(record,records,order_by_int,i); record=get_mem(Record); record->i=4; record->s="e"; - insert(record,records ,order_by,i); + insert(record,records,order_by_int,i); record=get_mem(Record); record->i=2; record->s="aaaa"; - insert(record,records ,order_by,i); + insert(record,records,order_by_int,i); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); - print("1x append(record,records );"); + print("1x append(record,records);"); record=get_mem(Record); record->i=0; record->s="ccc"; - append(record,records ); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + append(record,records); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); - print("sort(record,records ,desc_order_by,i);"); - sort(record,records ,desc_order_by,i); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + print("sort(record,records,order_by_int_desc,i);"); + sort(record,records,order_by_int_desc,i); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); - print("sort(record,records ,sort_by,s);"); - sort(record,records ,sort_by,s); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + print("sort(record,records,order_by_str,s);"); + sort(record,records,order_by_str,s); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); - print("remove(record,records ,record->i==1);"); - remove(record,records ,record->i==1); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + print("sort(record,records,order_by_ascii_desc,s);"); + sort(record,records,order_by_ascii_desc,s); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); - print("drop(record,records );"); - drop(record,records ); - for_each(record,records ) printf("i=%d, s=%s\n",record->i,record->s); + print("remove(record,records,record->i==1);"); + remove(record,records,record->i==1); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); + + print("drop(record,records);"); + drop(record,records); + for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s); } diff --git a/demos/seznamy.c b/demos/seznamy.c new file mode 100644 index 0000000..3fed54f --- /dev/null +++ b/demos/seznamy.c @@ -0,0 +1,63 @@ +#include "cll1.h" + +def_mem(Zaznam) +{ + int i; + str s; + list(Zaznam); +}; + +program +{ + Zaznam zaznam,zaznamy=NULL; + + print("5x insert(zaznam,zaznamy,order_by_int,i);"); + zaznam=get_mem(Zaznam); + zaznam->i=1; + zaznam->s="¾lu»ouèký kùò"; + insert(zaznam,zaznamy,order_by_int,i); + zaznam=get_mem(Zaznam); + zaznam->i=3; + zaznam->s="Úpìl Ïábelské"; + insert(zaznam,zaznamy,order_by_int,i); + zaznam=get_mem(Zaznam); + zaznam->i=4; + zaznam->s="ódy"; + insert(zaznam,zaznamy,order_by_int,i); + zaznam=get_mem(Zaznam); + zaznam->i=2; + zaznam->s="Hlavono¾ec - H je pøed Ch"; + insert(zaznam,zaznamy,order_by_int,i); + zaznam=get_mem(Zaznam); + zaznam->i=5; + zaznam->s="hurá - malé h"; + insert(zaznam,zaznamy,order_by_int,i); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("1x append(zaznam,zaznamy);"); + zaznam=get_mem(Zaznam); + zaznam->i=0; + zaznam->s="Chleba - písmeno Ch"; + append(zaznam,zaznamy); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("sort(zaznam,zaznamy,order_by_int_desc,i);"); + sort(zaznam,zaznamy,order_by_int_desc,i); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("sort(zaznam,zaznamy,order_by_str,s);"); + sort(zaznam,zaznamy,order_by_str,s); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("sort(zaznam,zaznamy,order_by_ascii,s);"); + sort(zaznam,zaznamy,order_by_ascii,s); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("remove(zaznam,zaznamy,zaznam->i==0);"); + remove(zaznam,zaznamy,zaznam->i==0); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); + + print("drop(zaznam,zaznamy);"); + drop(zaznam,zaznamy); + for_each(zaznam,zaznamy) printf("i=%d, s=%s\n",zaznam->i,zaznam->s); +} -- 2.30.2