// Main function
//
void MAppMain()
{
  MString str;                  // Program's output
  MVector<UInt32> L0, L1, L2;   // Database lists
// Encode List 1 (2-3-letter combinations from "exclude.txt")
  MStrings exc;
  MTextFile(dir+"/exclude.txt").readAll().parseTo(exc,"\r\n");
  for (MStrings::CIter it(exc); it(); ++it)
  {
    const MString& s = (*it).convToLower();
    if (s.cnt() == 2)
      L1.append(ch2int1(s[0]) + ch2int1(s[1])*28);
    else if (s.cnt() == 3)
      L1.append(ch2int1(s[0]) + ch2int1(s[1])*28 + ch2int1(s[2])*784);
  }
// Encode List 0 and List 2 (converted to lowercase in the process)
  MStrings words;
  MTextFile(dir+"/words.txt").readAll().parseTo(words,"\r\n");  // Load dictionary
  for (MStrings::CIter it(words); it(); ++it)
  {
    const MString s = (*it).convToLower();
  // Encode 1-4-length words to List 2 (as is, no signatures)
    if (s.cnt() >= 1 && s.cnt() <= 4)
    {
      if (s.cnt() == 1)
        L2.append(ch2int2(s[0]));
      else if (s.cnt() == 2)
        L2.append(ch2int2(s[0]) + ch2int2(s[1])*28);
      else if (s.cnt() == 3)
        L2.append(ch2int2(s[0]) + ch2int2(s[1])*28 + ch2int2(s[2])*784);
      else if (s.cnt() == 4)
        L2.append(ch2int2(s[0])     + ch2int2(s[1])*28 +
                  ch2int2(s[2])*784 + ch2int2(s[3])*21952);
    }
  // Skip words with "wrong" lengths (<5 - handled above, >15 - too few to care)
    if (s.cnt() < 5 || s.cnt() > 15) continue;
  // Encode 5-15-length words to List 0 as signatures
    UInt32 sig = (1-s.cnt()%2)<<ch2int0('*');           // Encode length bit (Note: produces a slightly smaller database than "s.cnt()%2")
    for (MNum i = 0; i < s.cnt(); ++i)  // Encode 19 letter bits
      sig |= (1 << ch2int0(s[i]));
    L0.append(sig);
  }
// Sort and merge the lists
  unique(L0);
  unique(L1);
  unique(L2); L2.setCnt(c2,0);
  MVector<UInt32> L = L0;                  // Merged list
  L.append(L1);
  L.append(L2);
// Calculate element differences (at lists' edges add initial element)
  MVector<UInt32> diffs;
  for (MNum i = 1; i < L.cnt(); ++i)
    if (L.get(i) > L.get(i-1))
      diffs.append(L.get(i) - L.get(i-1));
// Encode 'diffs' as optimized bitmaps (in a string form for convenience)
// Each diff value is encoded as "0..01n..n": 'z' zeroes, followed by 1,
// followed by bitmap of the 'x-a' number, where 'x' is the diff and 'a' is
// the nearest (smaller or equal) to 'x' power of 2
  MString bits;
  for (MNum i = 0; i < diffs.cnt(); ++i)
  {
    UInt32 d = diffs.get(i);
    for (UInt32 z = 0; ; ++z)
    {
      UInt32 a = (1<<z), b = a*2-1;   // [a,b] - suitable range of 'd' to process
      if (d<a || d>b) continue;       // Outside of proper range - keep looking
      for (UInt32 k = 0; k < z; ++k)
        bits << '0';
      bits << '1';
      for (UInt32 k = 0; k < z; ++k)
        bits << (GetBits(d-a,UInt8(k),UInt8(k))?'1':'0');
      break;
    }
  }
  while (bits.cnt()%8) bits << '0';   // Pad with zeroes to complete the last byte
// Encode the intermediary 'bits' string to actual bit vector
  MVector<UInt8> bytes;
  for (MNum i = 0; i < bits.cnt(); i+=8)
  {
    UInt8 b = 0;
    for (MNum j = 0; j < 8; ++j) if (bits[i+j]=='1') b |= (1<<j);
    bytes.append(b);
  }
// Save the data to the file and compress it, if 7z is available at the sprcified location
  MFile(dir+"/data",fmOverwrite) << bytes;
  if (MSys::getFileInfo(GZipExe).name.cnt())
  {
    MSys::deleteFile(dir+"/data.gz");
    MSys::open(GZipExe, (MString)"a -tgzip -mx=9 -sdel \""+dir+"/data.gz\" \""+dir+"/data\"");
  }
// Compile some useful information (needed for hardcoding in the Node.js file)
 // Prepare sizes
  int size1 = 0;
  while (!size1)
  {
    MApp::sleep(500);
    size1 = (UInt32)MSys::getFileInfo(dir+"/data.gz").size;
  }
  int size2 = (int)MSys::getFileInfo(dir+"/solution.js").size;
 // Initial elements and sizes
  str <<"w=["<<L0.get(0)<<","<<L1.get(0)<<","<<L2.get(0)<<","
      <<L0.cnt()<<","<<L1.cnt()<<","<<L2.cnt()<<"]\n";
 // Letter->index conversion string for L0
  str<<"C0: \" ";
  for (int j='a'; j<='z'; ++j) str<<MChar(int('a'+ch2int0(j)-1));
  str<<MChar(int('a'+ch2int0('\'')-1))<<"\"\n";
 // Letter->index conversion string for L1
  MString c1 = " ...........................";
  for (int j='a'; j<='z'; ++j) c1.setChar(ch2int1(j),j);
  c1.setChar(ch2int1('\''),'\'');
  str<<"C1: \""<<c1<<"\"\n";
 // Letter->index conversion string for L2
  str<<"C2: \" ";
  for (int j='a'; j<='z'; ++j) str<<MChar(int('a'+ch2int2(j)-1));
  str<<MChar(int('a'+ch2int2('\'')-1))<<"\"\n";
// Size report
  str <<"\n"<<words.cnt()<<" words => "<<L.cnt()<<" signatures => "
      <<bytes.cnt()<<" bytes =>\n"<<size1<<"+"<<size2<<" final bytes ("
      <<(65536-size1-size2)<<" left)\n\n";
// Display the information
  MMainFrame wnd;                                       // Create app's mainframe window
  wnd.plugBar(MEditBoxPtr(wnd,AutoRect,None,true,str)); // Create a text box
  wnd.setCaption("Data file written");
  wnd.runMessageLoop();                                 // Run the app
}