Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes832downloads
F64FileSort.txt74 linesDownload Raw Back to SortFileDemo
1 2//This takes an input file of doubles and makes a sorted output file.3 4Cd(__DIR__);;5 6I64 Compare(F64 e1,F64 e2)7{8  return Sign(e1-e2);9}10 11U0 F64FileSortTXT(U8 *in_name,U8 *out_name)12{13  CCmpCtrl *cc;14  I64 i,n;15  F64 *a;16  CDoc *doc;17 18  //Pass1: Count the number of F64s.19  n=0;20  cc=CmpCtrlNew(MStrPrint("#include \"%s\"",in_name));21  while (Lex(cc))22    if (cc->token==TK_F64)23      n++;24  CmpCtrlDel(cc);25 26  a=MAlloc(n*sizeof(F64));27 28  //Pass2: Read F64s.29  i=0;30  cc=CmpCtrlNew(MStrPrint("#include \"%s\"",in_name));31  while (Lex(cc))32    if (cc->token==TK_F64)33      a[i++]=cc->cur_f64;34  CmpCtrlDel(cc);35 36  QSortI64(a,n,&Compare); //Sort 64-bit sized values37 38  //Save39  doc=DocNew(out_name);40  DocPrint(doc,"//This is sorted.\n");41  for (i=0;i<n;i++)42    DocPrint(doc,"%12.6f\n",a[i]);43  DocWrite(doc);44  DocDel(doc);45 46  Free(a);47}48F64FileSortTXT("~/DemoUnsortedF64.DD.Z","~/DemoSortedF64.DD.Z");49Type("~/DemoUnsortedF64.DD.Z");50Type("~/DemoSortedF64.DD.Z");51 52U0 F64FileSortDAT(U8 *in_name,U8 *out_name)53{//File/CFile, ::/Demo/Dsk/DataBase.HC54  I64 size;55  F64 *a=FileRead(in_name,&size);56  QSortI64(a,size/sizeof(F64),&Compare); //Sort 64-bit sized values57  FileWrite(out_name,a,size);58  Free(a);59}60F64FileSortDAT("~/DemoUnsortedF64.DATA.Z","~/DemoSortedF64.DATA.Z");61 62U0 F64FileDumpDAT(U8 *in_name)63{//File/CFile, ::/Demo/Dsk/DataBase.HC64  I64 i,n,size;65  F64 *a=FileRead(in_name,&size);66  n=size/sizeof(F64);67  "%s\n",in_name;68  for (i=0;i<n;i++)69    "%12.6f\n",a[i];70  Free(a);71}72F64FileDumpDAT("~/DemoUnsortedF64.DATA.Z");73F64FileDumpDAT("~/DemoSortedF64.DATA.Z");74