/* ------------------------------------------------------------------------- */
/*   "text" : Text translation, the abbreviations optimiser, the dictionary  */
/*                                                                           */
/*   Part of Inform 6    copyright (c) Graham Nelson 1993, 1994, 1995, 1996  */
/*                                                                           */
/* ------------------------------------------------------------------------- */

#include "header.h"

uchar *low_strings, *low_strings_top;  /* Start and next free byte in the low
                                          strings pool */

int32 static_strings_extent;           /* Number of bytes of static strings
                                          made so far */
memory_block static_strings_area;      /* Used if (!temporary_files_switch) to
                                          hold the static strings area so far */

static uchar *strings_holding_area;    /* Area holding translated strings
                                          until they are moved into either
                                          a temporary file, or the
                                          static_strings_area below */

char *all_text, *all_text_top;         /* Start and next byte free in (large)
                                          text buffer holding the entire text
                                          of the game, when it is being
                                          recorded                           */
int put_strings_in_low_memory,         /* When TRUE, put static strings in
                                          the low strings pool at 0x100 rather
                                          than in the static strings area    */
    is_abbreviation,                   /* When TRUE, the string being trans
                                          is itself an abbreviation string
                                          so can't make use of abbreviations */
    abbrevs_lookup_table_made,         /* The abbreviations lookup table is
                                          constructed when the first non-
                                          abbreviation string is translated:
                                          this flag is TRUE after that       */
    abbrevs_lookup[256];               /* Once this has been constructed,
                                          abbrevs_lookup[n] = the smallest
                                          number of any abbreviation beginning
                                          with ASCII character n, or -1
                                          if none of the abbreviations do    */
int no_abbreviations;                  /* No of abbreviations defined so far */
uchar *abbreviations_at;                 /* Memory to hold the text of any
                                          abbreviation strings declared      */
/* ------------------------------------------------------------------------- */
/*   Abbreviation arrays                                                     */
/* ------------------------------------------------------------------------- */

int *abbrev_values;
int *abbrev_quality;
int *abbrev_freqs;

/* ------------------------------------------------------------------------- */

int32 total_chars_trans,               /* Number of ASCII chars of text in   */
      total_bytes_trans,               /* Number of bytes of Z-code text out */
      zchars_trans_in_last_string;     /* Number of Z-chars in last string:
                                          needed only for abbrev efficiency
                                          calculation in "directs.c"         */
static int32 total_zchars_trans,       /* Number of Z-chars of text out
                                          (only used to calculate the above) */
      no_chars_transcribed;            /* Number of ASCII chars written to
                                          the text transcription area (used
                                          for the -r and -u switches)        */

const char *alphabet[3] = {            /* The standard Z-machine alphabets   */
    "abcdefghijklmnopqrstuvwxyz",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    " ^0123456789.,!?_#'~/\\-:()"      /* Note the C notation \\ meaning '\' */
};

       char *accents =                 /* Standard 0.2 stock of accented...  */

   ":a:o:u:A:O:Uss<<>>:e:i:y:E:I'a'e'i'o'u'y'A'E'I'O'U'Y`a`e`i`o`u\
`A`E`I`O`U^a^e^i^o^u^A^E^I^O^UoaoA/o/O~a~n~o~A~N~OaeAEcccCthetThEtLLoeOE!!??";

                                       /* ...characters, numbered upwards    */
                                       /* from 155                           */

int chars_lookup[256];                 /* An index (for speed) to "alphabet":
                                          chars_lookup[n] =
                                          i    if ASCII char n occurs in ith
                                               place in alphabet[] (treating
                                               that as a 96-char string), or
                                          127  if ASCII char not in alphabet */

static int zchars_out_buffer[3],       /* During text translation, a buffer of
                                          3 Z-chars at a time: when it's full
                                          these are written as a 2-byte word */
           zob_index;                  /* Index (0 to 2) into it             */

static unsigned char *text_out_pc;     /* The "program counter" during text
                                          translation: the next address to
                                          write Z-coded text output to       */

/* ------------------------------------------------------------------------- */
/*   For variables/arrays used by the dictionary manager, see below          */
/* ------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
/*   This routine should be altered for ports to an OS whose character set   */
/*   is not ASCII. (It is called seldom and there is little speed overhead.) */
/* ------------------------------------------------------------------------- */

extern int translate_to_ascii(char c) { return((int) c); }

/* ------------------------------------------------------------------------- */
/*   Prepare the "alphabet" index table (see above), and while we're at it   */
/*   also fill the abbreviations lookup table with null values.              */
/* ------------------------------------------------------------------------- */

static void make_chars_lookup(void)
{   int i, j, k;
    for (j=0; j<256; j++)
    {   chars_lookup[j]=127; abbrevs_lookup[j]= -1; }
    for (j=0; j<3; j++)
        for (k=0; k<26; k++)
        {   i=(int) ((alphabet[j])[k]);
            chars_lookup[i]=k+j*26;
        }
}

/* ------------------------------------------------------------------------- */
/*   Prepare the abbreviations lookup table (used to speed up abbreviation   */
/*   detection in text translation).  We first bubble-sort the abbrevs into  */
/*   alphabetical order (this is necessary for the detection algorithm to    */
/*   to work).  Since the table is only prepared once, and for a table       */
/*   of size at most 96, there's no point using an efficient sort algorithm. */
/* ------------------------------------------------------------------------- */

static void make_abbrevs_lookup(void)
{   int bubble_sort, j, k, l; char p[MAX_ABBREV_LENGTH]; char *p1, *p2;
    do
    {   bubble_sort = FALSE;
        for (j=0; j<no_abbreviations; j++)
            for (k=j+1; k<no_abbreviations; k++)
            {   p1=(char *)abbreviations_at+j*MAX_ABBREV_LENGTH;
                p2=(char *)abbreviations_at+k*MAX_ABBREV_LENGTH;
                if (strcmp(p1,p2)<0)
                {   strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p);
                    l=abbrev_values[j]; abbrev_values[j]=abbrev_values[k];
                    abbrev_values[k]=l;
                    l=abbrev_quality[j]; abbrev_quality[j]=abbrev_quality[k];
                    abbrev_quality[k]=l;
                    bubble_sort = TRUE;
                }
            }
    } while (bubble_sort);

    for (j=no_abbreviations-1; j>=0; j--)
    {   p1=(char *)abbreviations_at+j*MAX_ABBREV_LENGTH;
        abbrevs_lookup[p1[0]]=j;
        abbrev_freqs[j]=0;
    }
    abbrevs_lookup_table_made = TRUE;
}

/* ------------------------------------------------------------------------- */
/*   Search the abbreviations lookup table (a routine which must be fast).   */
/*   The source text to compare is text[i], text[i+1], ... and this routine  */
/*   is only called if text[i] is indeed the first character of at least one */
/*   abbreviation, "from" begin the least index into the abbreviations table */
/*   of an abbreviation for which text[i] is the first character.  Recall    */
/*   that the abbrevs table is in alphabetical order.                        */
/*                                                                           */
/*   The return value is -1 if there is no match.  If there is a match, the  */
/*   text to be abbreviated out is over-written by a string of null chars    */
/*   with "ASCII" value 1, and the abbreviation number is returned.          */
/* ------------------------------------------------------------------------- */

static int try_abbreviations_from(unsigned char *text, int i, int from)
{   int j, k; char *p, c;
    c=text[i];
    for (j=from, p=(char *)abbreviations_at+from*MAX_ABBREV_LENGTH;
         (j<no_abbreviations)&&(c==p[0]); j++, p+=MAX_ABBREV_LENGTH)
    {   if (text[i+1]==p[1])
        {   for (k=2; p[k]!=0; k++)
                if (text[i+k]!=p[k]) goto NotMatched;
            for (k=0; p[k]!=0; k++) text[i+k]=1;
            abbrev_freqs[j]++;
            return(j);
            NotMatched: ;
        }
    }
    return(-1);
}

extern void make_abbreviation(char *text)
{
    strcpy((char *)abbreviations_at
            + no_abbreviations*MAX_ABBREV_LENGTH, text);

    is_abbreviation = TRUE;
    abbrev_values[no_abbreviations] = compile_string(text, TRUE, TRUE);
    is_abbreviation = FALSE;

    /*   The quality is the number of Z-chars saved by using this            */
    /*   abbreviation: note that it takes 2 Z-chars to print it.             */

    abbrev_quality[no_abbreviations++] = zchars_trans_in_last_string - 2;
}

/* ------------------------------------------------------------------------- */
/*   The front end routine for text translation                              */
/* ------------------------------------------------------------------------- */

extern int32 compile_string(char *b, int in_low_memory, int is_abbrev)
{   int i, j; uchar *c;

    is_abbreviation = is_abbrev;

    /* Put into the low memory pool (at 0x100 in the Z-machine) of strings   */
    /* which may be wanted as possible entries in the abbreviations table    */

    if (in_low_memory)
    {   j=subtract_pointers(low_strings_top,low_strings);
        low_strings_top=translate_text(low_strings_top,b);
        i= subtract_pointers(low_strings_top,low_strings);
        is_abbreviation = FALSE;
        if (i>MAX_LOW_STRINGS)
            memoryerror("MAX_LOW_STRINGS", MAX_LOW_STRINGS);
        return(0x21+(j/2));
    }

    c = translate_text(strings_holding_area, b);
    i = subtract_pointers(c, strings_holding_area);

    if (i>MAX_STATIC_STRINGS)
        memoryerror("MAX_STATIC_STRINGS",MAX_STATIC_STRINGS);

    /* Insert null bytes as needed to ensure that the next static string */
    /* also occurs at an address expressible as a packed address         */

    while ((i%scale_factor)!=0)
    {   i+=2; *c++ = 0; *c++ = 0;
    }

    j = static_strings_extent;

    if (temporary_files_switch)
        for (c=strings_holding_area; c<strings_holding_area+i;
             c++, static_strings_extent++)
            fputc(*c,Temp1_fp);
    else
        for (c=strings_holding_area; c<strings_holding_area+i;
             c++, static_strings_extent++)
            write_byte_to_memory_block(&static_strings_area,
                static_strings_extent, *c);

    is_abbreviation = FALSE;

    return(j/scale_factor);
}

/* ------------------------------------------------------------------------- */
/*   Output a single Z-character into the buffer, and flush it if full       */
/* ------------------------------------------------------------------------- */

static void write_z_char(int i)
{   uint32 j;
    total_zchars_trans++;
    zchars_out_buffer[zob_index++]=(i%32);
    if (zob_index!=3) return;
    zob_index=0;
    j= zchars_out_buffer[0]*0x0400 + zchars_out_buffer[1]*0x0020
       + zchars_out_buffer[2];
    text_out_pc[0] = j/256; text_out_pc[1] = j%256; text_out_pc+=2;
    total_bytes_trans+=2;
}

/* ------------------------------------------------------------------------- */
/*   Finish a Z-coded string, padding out with Z-char 5s if necessary and    */
/*   setting the "end" bit on the final 2-byte word                          */
/* ------------------------------------------------------------------------- */

static void end_z_chars(void)
{   unsigned char *p;
    zchars_trans_in_last_string=total_zchars_trans-zchars_trans_in_last_string;
    while (zob_index!=0) write_z_char(5);
    p=(unsigned char *) text_out_pc;
    *(p-2)= *(p-2)+128;
}

/* ------------------------------------------------------------------------- */
/*   The main routine "text.c" provides to the rest of Inform: the text      */
/*   translator. p is the address to write output to, s_text the source text */
/*   and the return value is the next free address to write output to.       */
/*   Note that the source text may be corrupted by this routine.             */
/* ------------------------------------------------------------------------- */

extern uchar *translate_text(uchar *p, char *s_text)
{   int i, j, k, in_alphabet, lookup_value, value, value2;
    unsigned char *text_in;

    /*  Cast the input and output streams to unsigned char: text_out_pc will
        advance as bytes of Z-coded text are written, but text_in doesn't    */

    text_in     = (unsigned char *) s_text;
    text_out_pc = (unsigned char *) p;

    /*  Remember the Z-chars total so that later we can subtract to find the
        number of Z-chars translated on this string                          */

    zchars_trans_in_last_string = total_zchars_trans;

    /*  Start with the Z-characters output buffer empty                      */

    zob_index=0;

    /*  If this is the first text translated since the abbreviations were
        declared, and if some were declared, then it's time to make the
        lookup table for abbreviations

        (Except: we don't if the text being translated is itself
        the text of an abbreviation currently being defined)                 */

    if ((!abbrevs_lookup_table_made) && (no_abbreviations > 0)
        && (!is_abbreviation))
        make_abbrevs_lookup();

    /*  If we're storing the whole game text to memory, then add this text   */

    if ((!is_abbreviation) && (store_the_text))
    {   no_chars_transcribed += strlen(s_text)+2;
        if (no_chars_transcribed >= MAX_TRANSCRIPT_SIZE)
            memoryerror("MAX_TRANSCRIPT_SIZE", MAX_TRANSCRIPT_SIZE);
        sprintf(all_text_top, "%s\n\n", s_text);
        all_text_top += strlen(all_text_top);
    }

    if (transcript_switch && (!veneer_mode))
        write_to_transcript_file(s_text);

    /*  The empty string of Z-text is illegal, since it can't carry an end
        bit: so we translate an empty string of ASCII text to just the
        pad character 5.  Printing this causes nothing to appear on screen.  */

    if (text_in[0]==0) write_z_char(5);

    /*  Loop through the characters of the null-terminated input text: note
        that if 1 is written over a character in the input text, it is
        afterwards ignored                                                   */

    for (i=0; text_in[i]!=0; i++)
    {   total_chars_trans++;

        /*  Contract ".  " into ". " if double-space-removing switch set:
            likewise "?  " and "!  " if the setting is high enough           */

        if ((double_space_setting >= 1)
            && (text_in[i+1]==' ') && (text_in[i+2]==' '))
        {   if (text_in[i]=='.') text_in[i+2]=1;
            if (double_space_setting >= 2)
            {   if (text_in[i]=='?') text_in[i+2]=1;
                if (text_in[i]=='!') text_in[i+2]=1;
            }
        }

        /*  Try abbreviations if the economy switch set                      */

        if ((economy_switch) && (!is_abbreviation)
            && ((k=abbrevs_lookup[text_in[i]])!=-1))
        {   if ((j=try_abbreviations_from(text_in, i, k))!=-1)
            {   if (j<32) { write_z_char(2); write_z_char(j); }
                else { write_z_char(3); write_z_char(j-32); }
            }
        }

        /*  '@' is an escape character in Inform string notation: the various
            possibilities are:

                @@decimalnumber  :  write an extended ASCII (0 to 1023) char
                @twodigits       :  write the abbreviation string with this
                                    decimal number
                @accentcode      :  write this accented character: e.g.,
                                    for @'e write an E-acute                 */

        if (text_in[i]=='@')
        {   if (text_in[i+1]=='@')
            {
                /*   @@decimalnumber   */

                i+=2;
                value=atoi((char *) (text_in+i));
                write_z_char(5); write_z_char(6);
                write_z_char(value/32); write_z_char(value%32);
                while (isdigit(text_in[i])) i++; i--;
            }
            else if (isdigit(text_in[i+1])!=0)
            {
                /*   @twodigits   */

                value= -1;
                switch(text_in[i+1])
                {   case '0': value=0; break;
                    case '1': value=1; break;
                    case '2': value=2; break;
                    case '3': value=3; break;
                    case '4': value=4; break;
                    case '5': value=5; break;
                    case '6': value=6; break;
                    case '7': value=7; break;
                    case '8': value=8; break;
                    case '9': value=9; break;
                }
                value2= -1;
                switch(text_in[i+2])
                {   case '0': value2=0; break;
                    case '1': value2=1; break;
                    case '2': value2=2; break;
                    case '3': value2=3; break;
                    case '4': value2=4; break;
                    case '5': value2=5; break;
                    case '6': value2=6; break;
                    case '7': value2=7; break;
                    case '8': value2=8; break;
                    case '9': value2=9; break;
                }
                if ((value!=-1)&&(value2!=-1))
                {   i+=2;
                    write_z_char(1); write_z_char(value*10+value2);
                }
            }
            else
            {
                /*   @accentcode   */

                value=0;
                for (j=0;accents[j]!=0;j=j+2)
                {   if ((accents[j]==text_in[i+1])
                        && (accents[j+1]==text_in[i+2])) value = j/2 + 155;
                }
                if (value==0)
                {   char uac[4];
                    uac[0]='@'; uac[1]=text_in[i+1];
                    uac[2]=text_in[i+2]; uac[3]=0;
                    error_named("Unknown accented character", uac);
                }
                i+=2;
                write_z_char(5); write_z_char(6);
                write_z_char(value/32); write_z_char(value%32);
            }
        }
        else
        {   /*  Skip a character which has been over-written with the null
                value 1 earlier on                                           */

            if (text_in[i]!=1)
            {   if (text_in[i]==' ') write_z_char(0);
                else
                {   lookup_value = chars_lookup[(int) (text_in[i])];
                    if (lookup_value == 127)
                    {   /*  The character isn't in the standard alphabets, so
                            we have to use the ASCII value 4-Z-char sequence */

                        write_z_char(5); write_z_char(6);
                        j=translate_to_ascii(text_in[i]);
                        write_z_char(j/32); write_z_char(j%32);
                    }
                    else
                    {   /*  The character is in one of the standard alphabets:
                            write a SHIFT to temporarily change alphabet if
                            it isn't in alphabet 0, then write the Z-char    */

                        in_alphabet = lookup_value/26; value = lookup_value%26;
                        if (in_alphabet==1) write_z_char(4);  /* SHIFT to A1 */
                        if (in_alphabet==2) write_z_char(5);  /* SHIFT to A2 */
                        write_z_char(value+6);
                    }
                }
            }
        }
    }

    /*  Flush the Z-characters output buffer and set the "end" bit           */

    end_z_chars();

    return((uchar *) text_out_pc);
}

/* ------------------------------------------------------------------------- */
/*   The abbreviations optimiser                                             */
/*                                                                           */
/*   This is a very complex, memory and time expensive algorithm to          */
/*   approximately solve the problem of which abbreviation strings would     */
/*   minimise the total number of Z-chars to which the game text translates. */
/*   It is in some ways a quite separate program but remains inside Inform   */
/*   for compatibility with previous releases.                               */
/* ------------------------------------------------------------------------- */

typedef struct tlb_s
{   char text[4];
    int32 intab, occurrences;
} tlb;
static tlb *tlbtab;
static int32 no_occs;

static int32 *grandtable;
static int32 *grandflags;
typedef struct optab_s
{   int32  length;
    int32  popularity;
    int32  score;
    int32  location;
    char text[64];
} optab;
static optab *bestyet, *bestyet2;

static int pass_no;

static char *sub_buffer;

static void optimise_pass(void)
{   int32 i; int t1, t2;
    int32 j, j2, k, nl, matches, noflags, score, min, minat, x, scrabble, c;
    for (i=0; i<256; i++) bestyet[i].length=0;
    for (i=0; i<no_occs; i++)
    {   if ((i!=(int) '\n')&&(tlbtab[i].occurrences!=0))
        {   printf("Pass %d, %4ld/%ld '%s' (%ld occurrences) ",
                pass_no, (long int) i, (long int) no_occs, tlbtab[i].text,
                (long int) tlbtab[i].occurrences);
            t1=(int) (time(0));
            for (j=0; j<tlbtab[i].occurrences; j++)
            {   for (j2=0; j2<tlbtab[i].occurrences; j2++) grandflags[j2]=1;
                nl=2; noflags=tlbtab[i].occurrences;
                while ((noflags>=2)&&(nl<=62))
                {   nl++;
                    for (j2=0; j2<nl; j2++)
                        if (all_text[grandtable[tlbtab[i].intab+j]+j2]=='\n')
                            goto FinishEarly;
                    matches=0;
                    for (j2=j; j2<tlbtab[i].occurrences; j2++)
                    {   if (grandflags[j2]==1)
                        {   x=grandtable[tlbtab[i].intab+j2]
                              - grandtable[tlbtab[i].intab+j];
                         if (((x>-nl)&&(x<nl))
                            || (memcmp(all_text+grandtable[tlbtab[i].intab+j],
                                       all_text+grandtable[tlbtab[i].intab+j2],
                                       nl)!=0))
                            {   grandflags[j2]=0; noflags--; }
                            else matches++;
                        }
                    }
                    scrabble=0;
                    for (k=0; k<nl; k++)
                    {   scrabble++;
                        c=all_text[grandtable[tlbtab[i].intab+j+k]];
                        if (c!=(int) ' ')
                        {   if (chars_lookup[c]==127)
                                scrabble+=2;
                            else
                                if (chars_lookup[c]>=26)
                                    scrabble++;
                        }
                    }
                    score=(matches-1)*(scrabble-2);
                    min=score;
                    for (j2=0; j2<256; j2++)
                    {   if ((nl==bestyet[j2].length)
                                && (memcmp(all_text+bestyet[j2].location,
                                       all_text+grandtable[tlbtab[i].intab+j],
                                       nl)==0))
                        {   j2=256; min=score; }
                        else
                        {   if (bestyet[j2].score<min)
                            {   min=bestyet[j2].score; minat=j2;
                            }
                        }
                    }
                    if (min!=score)
                    {   bestyet[minat].score=score;
                        bestyet[minat].length=nl;
                        bestyet[minat].location=grandtable[tlbtab[i].intab+j];
                        bestyet[minat].popularity=matches;
                        for (j2=0; j2<nl; j2++) sub_buffer[j2]=
                            all_text[bestyet[minat].location+j2];
                        sub_buffer[nl]=0;
                    }
                }
                FinishEarly: ;
            }
            t2=((int) time(0)) - t1;
            printf(" (%d seconds)\n",t2);
        }
    }
}

static int any_overlap(char *s1, char *s2)
{   int a, b, i, j, flag;
    a=strlen(s1); b=strlen(s2);
    for (i=1-b; i<a; i++)
    {   flag=0;
        for (j=0; j<b; j++)
            if ((0<=i+j)&&(i+j<=a-1))
                if (s1[i+j]!=s2[j]) flag=1;
        if (flag==0) return(1);
    }
    return(0);
}

#define MAX_TLBS 8000

extern void optimise_abbreviations(void)
{   int32 i, j, t, max=0, MAX_GTABLE;
    int32 j2, selected, available, maxat, nl;
    tlb test;

    printf("Beginning calculation of optimal abbreviations...\n");

    pass_no = 0;
    tlbtab=my_calloc(sizeof(tlb), MAX_TLBS, "tlb table"); no_occs=0;
    sub_buffer=my_calloc(sizeof(char), 4000, "sub_buffer");
    for (i=0; i<MAX_TLBS; i++) tlbtab[i].occurrences=0;

    bestyet=my_calloc(sizeof(optab), 256, "bestyet");
    bestyet2=my_calloc(sizeof(optab), 64, "bestyet2");

            bestyet2[0].text[0]='.';
            bestyet2[0].text[1]=' ';
            bestyet2[0].text[2]=' ';
            bestyet2[0].text[3]=0;

            bestyet2[1].text[0]=',';
            bestyet2[1].text[1]=' ';
            bestyet2[1].text[2]=0;

    for (i=0; all_text+i<all_text_top; i++)
    {
        if ((all_text[i]=='.') && (all_text[i+1]==' ') && (all_text[i+2]==' '))
        {   all_text[i]='\n'; all_text[i+1]='\n'; all_text[i+2]='\n';
            bestyet2[0].popularity++;
        }

        if ((all_text[i]=='.') && (all_text[i+1]==' '))
        {   all_text[i]='\n'; all_text[i+1]='\n';
            bestyet2[0].popularity++;
        }

        if ((all_text[i]==',') && (all_text[i+1]==' '))
        {   all_text[i]='\n'; all_text[i+1]='\n';
            bestyet2[1].popularity++;
        }
    }

    MAX_GTABLE=subtract_pointers(all_text_top,all_text)+1;
    grandtable=my_calloc(4*sizeof(int32), MAX_GTABLE/4, "grandtable");

    for (i=0, t=0; all_text+i<all_text_top; i++)
    {   test.text[0]=all_text[i];
        test.text[1]=all_text[i+1];
        test.text[2]=all_text[i+2];
        test.text[3]=0;
        if ((test.text[0]=='\n')||(test.text[1]=='\n')||(test.text[2]=='\n'))
            goto DontKeep;
        for (j=0; j<no_occs; j++)
            if (strcmp(test.text,tlbtab[j].text)==0)
                goto DontKeep;
        test.occurrences=0;
        for (j=i+3; all_text+j<all_text_top; j++)
        {   if ((all_text[i]==all_text[j])
                 && (all_text[i+1]==all_text[j+1])
                 && (all_text[i+2]==all_text[j+2]))
                 {   grandtable[t+test.occurrences]=j;
                     test.occurrences++;
                     if (t+test.occurrences==MAX_GTABLE)
                     {   printf("All %ld cross-references used\n",
                             (long int) MAX_GTABLE);
                         goto Built;
                     }
                 }
        }
        if (test.occurrences>=2)
        {   tlbtab[no_occs]=test;
            tlbtab[no_occs].intab=t; t+=tlbtab[no_occs].occurrences;
            if (max<tlbtab[no_occs].occurrences)
                max=tlbtab[no_occs].occurrences;
            no_occs++;
            if (no_occs==MAX_TLBS)
            {   printf("All %d three-letter-blocks used\n",
                    MAX_TLBS);
                goto Built;
            }
        }
        DontKeep: ;
    }

    Built:
    grandflags=my_calloc(sizeof(int), max, "grandflags");


    printf("Cross-reference table (%ld entries) built...\n",
        (long int) no_occs);
    /*  for (i=0; i<no_occs; i++)
            printf("%4d %4d '%s' %d\n",i,tlbtab[i].intab,tlbtab[i].text,
                tlbtab[i].occurrences);
    */

    for (i=0; i<64; i++) bestyet2[i].length=0; selected=2;
    available=256;
    while ((available>0)&&(selected<64))
    {   printf("Pass %d\n", ++pass_no);

        optimise_pass();
        available=0;
        for (i=0; i<256; i++)
            if (bestyet[i].score!=0)
            {   available++;
                nl=bestyet[i].length;
                for (j2=0; j2<nl; j2++) bestyet[i].text[j2]=
                    all_text[bestyet[i].location+j2];
                bestyet[i].text[nl]=0;
            }

    /*  printf("End of pass results:\n");
        printf("\nno   score  freq   string\n");
        for (i=0; i<256; i++)
            if (bestyet[i].score>0)
                printf("%02d:  %4d   %4d   '%s'\n", i, bestyet[i].score,
                    bestyet[i].popularity, bestyet[i].text);
    */

        do
        {   max=0;
            for (i=0; i<256; i++)
                if (max<bestyet[i].score)
                {   max=bestyet[i].score;
                    maxat=i;
                }

            if (max>0)
            {   bestyet2[selected++]=bestyet[maxat];

                printf(
                    "Selection %2ld: '%s' (repeated %ld times, scoring %ld)\n",
                    (long int) selected,bestyet[maxat].text,
                    (long int) bestyet[maxat].popularity,
                    (long int) bestyet[maxat].score);

                test.text[0]=bestyet[maxat].text[0];
                test.text[1]=bestyet[maxat].text[1];
                test.text[2]=bestyet[maxat].text[2];
                test.text[3]=0;

                for (i=0; i<no_occs; i++)
                    if (strcmp(test.text,tlbtab[i].text)==0)
                        break;

                for (j=0; j<tlbtab[i].occurrences; j++)
                {   if (memcmp(bestyet[maxat].text,
                               all_text+grandtable[tlbtab[i].intab+j],
                               bestyet[maxat].length)==0)
                    {   for (j2=0; j2<bestyet[maxat].length; j2++)
                            all_text[grandtable[tlbtab[i].intab+j]+j2]='\n';
                    }
                }

                for (i=0; i<256; i++)
                    if ((bestyet[i].score>0)&&
                        (any_overlap(bestyet[maxat].text,bestyet[i].text)==1))
                    {   bestyet[i].score=0;
                       /* printf("Discarding '%s' as overlapping\n",
                            bestyet[i].text); */
                    }
            }
        } while ((max>0)&&(available>0)&&(selected<64));
    }

    printf("\nChosen abbreviations (in Inform syntax):\n\n");
    for (i=0; i<selected; i++)
        printf("Abbreviate \"%s\";\n", bestyet2[i].text);

    text_free_arrays();
}

/* ------------------------------------------------------------------------- */
/*   The dictionary manager begins here.                                     */
/*                                                                           */
/*   Speed is extremely important in these algorithms: a simple O(n^2) hack  */
/*   roughly triples Inform's total compilation speed on medium-size games,  */
/*   where n is the number of dictionary words.   (In fact for reasons gone  */
/*   into below, Inform actually uses an O(n^2) hashing algorithm.)          */
/* ------------------------------------------------------------------------- */
/*   A dictionary table similar to the Z-machine format is kept: there is a */
/*   7-byte header (left blank here to be filled in at the                   */
/*   construct_storyfile() stage in "tables.c") and then a sequence of       */
/*   records, one per word, in the form                                      */
/*                                                                           */
/*        <Z-coded text>    <flags>  <adjectivenumber>  <verbnumber>         */
/*        4 or 6 bytes       byte          byte             byte             */
/*                                                                           */
/*   These records are stored in "accession order" (i.e. in order of their   */
/*   first being received by these routines) but an alphabetical index is    */
/*   incrementally constructed so that construct_storyfile() can rearrange   */
/*   it all into alphabetical order.                                         */
/* ------------------------------------------------------------------------- */

uchar *dictionary,                    /* (These two pointers are externally
                                         used only in "tables.c" when
                                         building the story-file)            */
    *dictionary_top;                  /* Pointer to next free record         */

int dict_entries;                     /* Total number of records entered     */

/* ------------------------------------------------------------------------- */
/*   dict_word is a typedef for a struct of 6 unsigned chars (defined in     */
/*   "header.h"): it holds the (4 or) 6 bytes of Z-coded text of a word.     */
/*   Usefully, because the PAD character 5 is < all alphabetic characters,   */
/*   alphabetic order corresponds to numeric order.  For this reason, the    */
/*   dict_word is called the "sort code" of the original text word.          */
/* ------------------------------------------------------------------------- */

extern int compare_sorts(dict_word d1, dict_word d2)
{   int i;
    for (i=0; i<6; i++) if (d1.b[i]!=d2.b[i]) return(d1.b[i]-d2.b[i]);
    /* (since memcmp(d1.b, d2.b, 6); runs into a bug on some Unix libraries) */
    return(0);
}

static dict_word prepared_sort;       /* Holds the sort code of current word */

static int initial_letter;            /* Its first letter (0 or 1=a, 26=z)   */

extern dict_word dictionary_prepare(char *dword)
{   int i, j, k, wd[9]; int32 tot;

    /* A rapid text translation algorithm using only the simplified rules
       applying to the text of dictionary entries: first produce a sequence
       of 6 (v3) or 9 (v4+) Z-characters                                     */

    for (i=0, j=0; (i<9)&&(dword[j]!=0); i++, j++)
    {   k=(int) dword[j];
        if (k==(int) '\'')
            warning_named("Obsolete usages: use the ^ character for the \
apostrophe in", dword);
        if (k==(int) '^') k=(int) '\'';
        k=chars_lookup[k];
        if (k==127)
        {   /* We'll need 4 more Z-chars to encode a full ASCII escape
               sequence, so we give up ("break") if there's no room          */

            if (i <= ((version_number==3)?2:5))
            {   wd[i++] = 5; wd[i++] = 6;
                k = translate_to_ascii(dword[j]);
                wd[i++] = k/32; wd[i] = k%32;
            }
            else break;
        }
        else
        {   if ((k>=26)&&(k<2*26)) k=k-26;    /* Upper down to lower case    */
            if ((k/26)!=0)
            {   if (i == ((version_number==3)?5:8))   /* If there's space... */
                    break;
                wd[i++]=3+(k/26);             /* Change alphabet for symbols */
            }
            wd[i]=6+(k%26);                   /* Write the Z character       */
        }
    }

    /* Fill up to the end of the dictionary block with PAD characters        */

    for (; i<9; i++) wd[i]=5;

    /* We also work out the initial letter group (1 to 26 for a to z, 0 for
       non-alphabetic), which will be needed later                           */

    initial_letter = wd[0]-5;
    if (initial_letter<0) initial_letter=0;

    /* Despite appearances, this doesn't depend on A to Z being contiguous
       in the machine's character set.  The array of Z-chars is converted to
       three 2-byte blocks...                                                */

    tot = wd[2] + wd[1]*(1<<5) + wd[0]*(1<<10);
    prepared_sort.b[1]=tot%0x100;
    prepared_sort.b[0]=(tot/0x100)%0x100;
    tot = wd[5] + wd[4]*(1<<5) + wd[3]*(1<<10);
    prepared_sort.b[3]=tot%0x100;
    prepared_sort.b[2]=(tot/0x100)%0x100;
    tot = wd[8] + wd[7]*(1<<5) + wd[6]*(1<<10);
    prepared_sort.b[5]=tot%0x100;
    prepared_sort.b[4]=(tot/0x100)%0x100;

    /* Set the "end bit" on the 2nd (in v3) or the 3rd (v4+) 2-byte block    */

    if (version_number==3) prepared_sort.b[2]+=0x80;
                      else prepared_sort.b[4]+=0x80;

    return(prepared_sort);
}

/* ------------------------------------------------------------------------- */
/*   The arrays below are all concerned with the problem of alphabetically   */
/*   sorting the dictionary during the compilation pass.                     */
/*                                                                           */
/*   Of course the best sorting algorithms would therefore construct a       */
/*   dictionary of n words in O(n log n) comparisons.  This algorithm could  */
/*   be made O(n log n) if it had a more "logarithmic" approach to the       */
/*   thumb-indexing (where the number of "letter groups" grow, and then are  */
/*   themselves indexed, and so on) but that would be expensive on memory:   */
/*   experiments with this have shown speed gains outweighed by the losses   */
/*   involved in bureaucracy.  In practice the hash-coding delivers about    */
/*   n^2/40 comparisons and for n typically about 1000 on largish games this */
/*   works quickly enough to contribute little to total compilation time.    */
/*   (Comparison is a cheap process: see below.)                             */
/*                                                                           */
/*   The question still remains: why not just apply qsort to the dictionary  */
/*   list at the end of the pass?                                            */
/*   The answer being: because we've got to ensure that no duplicates ever   */
/*   enter the list on pass.  New accession numbers are only given to new    */
/*   words.                                                                  */
/*                                                                           */
/*   dict_sort_codes[n]     the sort code of record n: i.e., of the nth      */
/*                          word to be entered into the dictionary, where    */
/*                          n counts upward from 0                           */
/*                                                                           */
/*   linked_list_next and linked_list_prev are forward and backward links:   */
/*   the records 0 to no_entries-1 form a double linked list in              */
/*   (ascending) alphabetical order (i.e. also (ascending) sort code order): */
/*                                                                           */
/*   start_from             is the initial record in the list                */
/*   linked_list_next[n]    is the record after record n, or LIST_TOP        */
/*                          if record n is the last                          */
/*   linked_list_prev[n]    is the record before record n, or LIST_BOTTOM    */
/*                          if record n is the first                         */
/*                                                                           */
/*   As, however, this ordering information is inexplicit, the following    */
/*   array is worked out at the end of the compilation pass:                 */
/*                                                                           */
/*   final_dict_order[n]    holds the alphabetical-order position of the     */
/*                          nth word to arrive                               */
/*                                                                           */
/*   On top of all that is a hash-coding.  To facilitate reasonably          */
/*   fast seeking of a particular alphabet position in the linked list, we   */
/*   keep a "thumb-index" of which positions the letters A to Z start at:    */
/*                                                                           */
/*   letter_starts[n]       holds the earliest record in the linked list of  */
/*                          any word beginning with the nth letter, where    */
/*                          0 = non-letter (e.g. 3 or $), 1 = a, 26 = z;     */
/*                          or holds LETTER_EMPTY if no words in the list do */
/*                                                                           */
/*   And for convenience, since it is memory-cheap, we also keep             */
/*                                                                           */
/*   letter_keys[n]         the sort code of the earliest word in this group */
/*                          (or a value guaranteed to be > than a sort code  */
/*                          of any word can be, if the group is empty)       */
/* ------------------------------------------------------------------------- */

#define LIST_BOTTOM  (-1)
#define LIST_TOP     (-2)

static int start_list;

static int     *linked_list_next,
               *linked_list_prev;
       int     *final_dict_order;
static dict_word *dict_sort_codes;

#define NUMBER_LETTER_GROUPS 27
#define LETTER_EMPTY (-1)

static dict_word letter_keys[NUMBER_LETTER_GROUPS];
static int letter_starts[NUMBER_LETTER_GROUPS];

static void dictionary_begin_pass(void)
{   int i;

    /*  Leave room for the 7-byte header (added in "tables.c" much later)    */

    dictionary_top=dictionary+7;

        /*  Strictly speaking the linked list is initially empty, but rather
            than bother implementing that, we create it containing just record
            0 (even though record 0 doesn't yet exist): we won't change the
            list again until record 1 arrives                                */

    start_list=0;
    linked_list_next[0] = LIST_TOP;
    linked_list_prev[0] = LIST_BOTTOM;

    for (i=0; i<NUMBER_LETTER_GROUPS; i++)
    {   /*  This is bound to be "alphabetically" after any sort code,
            as it has top bit set on the initial 2-byte block, which no
            dictionary Z-coded string can legally have                   */

        letter_keys[i].b[0] = 0xff;
        letter_starts[i] = LETTER_EMPTY;
    }

    dict_entries = 0;
}

extern void sort_dictionary(void)
{   int i, j;
    if (module_switch)
    {   for (i=0; i<dict_entries; i++)
            final_dict_order[i] = i;
        return;
    }

    for (j=start_list, i=0; i<dict_entries; i++)
    {   final_dict_order[j]=i; j=linked_list_next[j];
    }
}

/* ------------------------------------------------------------------------- */
/*   If "dword" is in the dictionary, return its accession number plus 1;    */
/*   If not, return 0.                                                       */
/* ------------------------------------------------------------------------- */

extern int dictionary_find(char *dword)
{   int32 next_nonempty_letter, stop_search_at, search_point, record_range;

    dictionary_prepare(dword);

    record_range = dict_entries;

    /*  If there are no records, the word can't be in the dictionary         */

    if (record_range==0) return 0;

    /*  Work out where in the list our search word's letter group starts     */

    search_point = letter_starts[initial_letter];

    /*  If there's nothing in its letter group, it can't possibly match, so
        give up immediately (alas, this seldom happens)                      */

    if (search_point == LETTER_EMPTY) return(0);

    /*  Set stop_search_at to the nearest point we can find in the list which
        we need not search beyond: either the start of the next letter group
        which has anything in, or the top of the list if the current letter
        group is the last one with anything in                               */

    next_nonempty_letter=initial_letter+1;
    while ((next_nonempty_letter<NUMBER_LETTER_GROUPS)
           && ((stop_search_at=letter_starts[next_nonempty_letter])
                == LETTER_EMPTY)) next_nonempty_letter++;

    if (next_nonempty_letter==NUMBER_LETTER_GROUPS) stop_search_at = LIST_TOP;

    /*  Now do brute force comparisons: in effect, we compare our search word
        with every word already in the dictionary which has the same initial
        letter, and which is inside the record range required                */

    while (search_point != stop_search_at)
    {
        if ((compare_sorts(prepared_sort,dict_sort_codes[search_point])==0)
            && (search_point<record_range))
            return(search_point+1);
        search_point = linked_list_next[search_point];
    }
    return(0);
}

/* ------------------------------------------------------------------------- */
/*  Add "dword" to the dictionary with (x,y,z) as its data bytes; unless     */
/*  it already exists, in which case OR the data with (x,y,z).  (E.g. if an  */
/*  existing noun is entered as a verb-word, the verb bit is added to x,     */
/*  y is left alone and z becomes the verb number.)                          */
/*                                                                           */
/*  Returns: the accession number.                                           */
/* ------------------------------------------------------------------------- */

extern int dictionary_add(char *dword, int x, int y, int z)
{   int off, i, search_point, previous; uchar *p; dict_word pcomp, qcomp;
    int res=((version_number==3)?4:6);

    if (dict_entries==MAX_DICT_ENTRIES)
        memoryerror("MAX_DICT_ENTRIES",MAX_DICT_ENTRIES);

    /*  If it exists already, OR the data in and return the address          */

    i=dictionary_find(dword);
    if (i!=0)
    {   p=dictionary+7+(i-1)*(3+res)+res;
        p[0]=(p[0])|x; p[1]=(p[1])|y; p[2]=(p[2])|z;
        return i-1;
    }

    /*  Set i to be the record number to write to (counting from 0)          */

    i=dict_entries;

    /*  Set offset from start of dictionary table of record to write to      */

    off=(3+res)*i+7;

    /*  Address in Inform's own dictionary table to write the record to      */

    p=dictionary+off;

    /*  So copy in the 4 (or 6) bytes of Z-coded text and the 3 data bytes   */

    p[0]=prepared_sort.b[0]; p[1]=prepared_sort.b[1];
    p[2]=prepared_sort.b[2]; p[3]=prepared_sort.b[3];
    if (version_number > 3)
    {   p[4]=prepared_sort.b[4]; p[5]=prepared_sort.b[5]; }

    p[res]=x; p[res+1]=y; p[res+2]=z;

    pcomp=prepared_sort;

    /*  If this is the first word, then the linked list is empty: do
        nothing, as the list is already initialised to a state holding
        just record 0    */

    if (dict_entries==0) goto AddedToList;

    /*  Set search_point equal to the first point it's worth searching
        from: the start of the section for the initial letter of our word,
        or failing that the start of any section earlier in the alphabet
        that's occupied, or failing that the actual start of the list    */

    i=initial_letter;
    do    search_point = letter_starts[i--];
    while ((i>=0) && (search_point == LETTER_EMPTY));
    if (search_point == LETTER_EMPTY) search_point = start_list;

    /*  Hunt through the linked list until we find a word which
        is further on in the alphabet than our present one               */

    for (; search_point != LIST_TOP;
           search_point = linked_list_next[search_point])
    {   qcomp = dict_sort_codes[search_point];
        if (compare_sorts(pcomp,qcomp)<0)
        {
            /*  search_point is now at a point just alphabetically after
                the word we are trying to enter.  What came before it?   */

            previous = linked_list_prev[search_point];
            if (previous == LIST_BOTTOM)
            {
                /*  There was no previous entry, so make our new word
                    the new start of the list and adjust the links       */

                linked_list_next[dict_entries] = start_list;
                linked_list_prev[dict_entries] = LIST_BOTTOM;
                linked_list_prev[search_point] = dict_entries;
                start_list = dict_entries;
                goto AddedToList;
            }

            /*  Adjust the chain from  previous -> search_point
                                   to  previous -> (new) -> search_point */

            linked_list_next[previous]     = dict_entries;
            linked_list_prev[search_point] = dict_entries;
            linked_list_next[dict_entries] = search_point;
            linked_list_prev[dict_entries] = previous;
            goto AddedToList;
        }
        i = search_point;
    }

    /*  Our new word must have been alphabetically after everything in the
        dictionary, so we add it to the end of the chain                 */

    linked_list_next[i]            = dict_entries;
    linked_list_prev[dict_entries] = i;
    linked_list_next[dict_entries] = LIST_TOP;

    AddedToList:

    dict_sort_codes[dict_entries]=pcomp;

    /*  Is this now the earliest word in its letter group?  If so, reset
        the record of the first word in the group to be this             */

    if (compare_sorts(pcomp,letter_keys[initial_letter])<0)
    {   letter_keys[initial_letter]=pcomp;
        letter_starts[initial_letter]=dict_entries;
    }

    dict_entries++;
    dictionary_top += res+3;
    return(dict_entries-1);
}

/* ------------------------------------------------------------------------- */
/*   Used in "tables.c" for "Extend ... only", to renumber a verb-word to a  */
/*   new verb syntax of its own.  (Otherwise existing verb-words never       */
/*   change their verb-numbers.)                                             */
/* ------------------------------------------------------------------------- */

extern void dictionary_set_verb_number(char *dword, int to)
{   int i; uchar *p;
    int res=((version_number==3)?4:6);
    i=dictionary_find(dword);
    if (i!=0)
    {   p=dictionary+7+(i-1)*(3+res)+res; p[1]=to;
    }
}

/* ------------------------------------------------------------------------- */
/*   Tracing code for the dictionary: used only by "trace"                   */
/* ------------------------------------------------------------------------- */

static void show_letter(int code)
{   if (code<6) { printf("."); return; }
    printf("%c",(alphabet[0])[code-6]);
}

static int get_letter(int code)
{   if (code<6) return ' ';
    return (alphabet[0])[code-6];
}

extern void show_dictionary(void)
{   int i, j; char *p;
    int res=((version_number==3)?4:6);
    printf("Dictionary contains %d entries:\n",dict_entries);
    for (i=0; i<dict_entries; i++)
    {   p=(char *)dictionary+7+(3+res)*i;
        show_letter( (((int) p[0])&0x7c)/4 );
        show_letter( 8*(((int) p[0])&0x3) + (((int) p[1])&0xe0)/32 );
        show_letter( ((int) p[1])&0x1f );
        show_letter( (((int) p[2])&0x7c)/4 );
        show_letter( 8*(((int) p[2])&0x3) + (((int) p[3])&0xe0)/32 );
        show_letter( ((int) p[3])&0x1f );
        if (version_number > 3)
        {   show_letter( (((int) p[4])&0x7c)/4 );
            show_letter( 8*(((int) p[4])&0x3) + (((int) p[5])&0xe0)/32 );
            show_letter( ((int) p[5])&0x1f );
        }
        printf("  ");
        for (j=0; j<3+res; j++) printf("%02x ",p[j]);
        printf("\n");
    }
}

extern void write_dictionary_to_transcript(void)
{   int i; char *p;
    int res=((version_number==3)?4:6);
    char d_buffer[80];

    sprintf(d_buffer, "\n[Dictionary contains %d entries:]\n", dict_entries);
    write_to_transcript_file(d_buffer);

    for (i=0; i<dict_entries; i++)
    {   p=(char *)dictionary+7+(3+res)*i;
        if ((i%5) == 0) d_buffer[0] = 0;
        sprintf(d_buffer + strlen(d_buffer),
            "  %c%c%c%c%c%c%c%c%c     ",
        get_letter( (((int) p[0])&0x7c)/4 ),
        get_letter( 8*(((int) p[0])&0x3) + (((int) p[1])&0xe0)/32 ),
        get_letter( ((int) p[1])&0x1f ),
        get_letter( (((int) p[2])&0x7c)/4 ),
        get_letter( 8*(((int) p[2])&0x3) + (((int) p[3])&0xe0)/32 ),
        get_letter( ((int) p[3])&0x1f ),
        (version_number>3)?get_letter( (((int) p[4])&0x7c)/4 ):' ',
        (version_number>3)?
            get_letter( 8*(((int) p[4])&0x3) + (((int) p[5])&0xe0)/32 ):' ',
        (version_number>3)?get_letter( ((int) p[5])&0x1f ):' ');
        if ((i%5) == 4) write_to_transcript_file(d_buffer);
    }
    if ((i%5) != 0) write_to_transcript_file(d_buffer);
}

/* ========================================================================= */
/*   Data structure management routines                                      */
/* ------------------------------------------------------------------------- */

extern void init_text_vars(void)
{   bestyet = NULL;
    bestyet2 = NULL;
    tlbtab = NULL;
    grandtable = NULL;
    grandflags = NULL;
    no_chars_transcribed = 0;
    is_abbreviation = FALSE;
    put_strings_in_low_memory = FALSE;

    make_chars_lookup();

    total_zchars_trans = 0;

    linked_list_next = NULL;
    linked_list_prev = NULL;
    final_dict_order = NULL;
    dict_sort_codes = NULL;
    dict_entries=0;

    initialise_memory_block(&static_strings_area);
}

extern void text_begin_pass(void)
{   abbrevs_lookup_table_made = FALSE;
    no_abbreviations=0;
    total_chars_trans=0; total_bytes_trans=0;
    if (store_the_text) all_text_top=all_text;
    dictionary_begin_pass();
    low_strings_top = low_strings;

    static_strings_extent = 0;
}

/*  Note: for allocation and deallocation of all_the_text, see inform.c      */

extern void text_allocate_arrays(void)
{   abbreviations_at = my_malloc(MAX_ABBREVS*MAX_ABBREV_LENGTH,
        "abbreviations");
    abbrev_values    = my_calloc(sizeof(int), MAX_ABBREVS, "abbrev values");
    abbrev_quality   = my_calloc(sizeof(int), MAX_ABBREVS, "abbrev quality");
    abbrev_freqs     = my_calloc(sizeof(int),   MAX_ABBREVS, "abbrev freqs");

    linked_list_next = my_calloc(sizeof(int),  MAX_DICT_ENTRIES,
                                 "dictionary places linked list next-markers");
    linked_list_prev = my_calloc(sizeof(int),  MAX_DICT_ENTRIES,
                                 "dictionary places linked list prev-markers");
    final_dict_order = my_calloc(sizeof(int),  MAX_DICT_ENTRIES,
                                 "final dictionary ordering table");
    dict_sort_codes = my_calloc(sizeof(dict_word),   MAX_DICT_ENTRIES,
                                 "dictionary sort codes");

    dictionary = my_malloc(9*MAX_DICT_ENTRIES+7,"dictionary");
    strings_holding_area
         = my_malloc(MAX_STATIC_STRINGS,"static strings holding area");
    low_strings = my_malloc(MAX_LOW_STRINGS,"low (abbreviation) strings");
}

extern void text_free_arrays(void)
{
    my_free(&strings_holding_area, "static strings holding area");
    my_free(&low_strings, "low (abbreviation) strings");
    my_free(&abbreviations_at, "abbreviations");
    my_free(&abbrev_values,    "abbrev values");
    my_free(&abbrev_quality,   "abbrev quality");
    my_free(&abbrev_freqs,     "abbrev freqs");

    my_free(&linked_list_next, "dictionary places linked list next-markers");
    my_free(&linked_list_prev, "dictionary places linked list prev-markers");
    my_free(&final_dict_order, "final dictionary ordering table");
    my_free(&dict_sort_codes,  "dictionary sort codes");

    my_free(&dictionary,"dictionary");

    deallocate_memory_block(&static_strings_area);
}

extern void ao_free_arrays(void)
{   my_free (&tlbtab,"tlb table");
    my_free (&sub_buffer,"sub_buffer");
    my_free (&bestyet,"bestyet");
    my_free (&bestyet2,"bestyet2");
    my_free (&grandtable,"grandtable");
    my_free (&grandflags,"grandflags");
}

/* ========================================================================= */
