summaryrefslogtreecommitdiff
path: root/source/luametatex/source/utilities/auxsparsearray.h
blob: 91e3100747e899d0eb6023f28afc7d8b27e1fa1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    See license.txt in the root of this project.
*/

# ifndef LMT_UTILITIES_SPARSEARRAY_H
# define LMT_UTILITIES_SPARSEARRAY_H

/*tex

    This file originally was called |managed-sa| but becauss it kind of a library and also used in
    \LUATEX\ it's better to use a different name. In this variant dumping is more sparse (resulting
    in somewhat smaller format files). This might be backported but only after testing it here for a
    long time. Of course the principles are the same, it's just extended.

*/

/*tex

    The next two sets of three had better match up exactly, but using bare numbers is easier on the
    \CCODE\ compiler. Here are some format sizes (for ConTeXt) with different values:

     64 : 17562942
    128 : 17548150 <= best value
    256 : 17681398

*/

# define LMT_SA_HIGHPART 128
# define LMT_SA_MIDPART  128
# define LMT_SA_LOWPART  128

# define LMT_SA_H_PART(a) (((a)>>14)&127)
# define LMT_SA_M_PART(a) (((a)>> 7)&127)
# define LMT_SA_L_PART(a) ( (a)     &127)

/*tex

    In the early days of \LUATEX\ we had just simple items, all 32 bit values. Then we put the
    delcodes in trees too which saved memory and format size but it introduced 32 bit slack in all
    the other code arrays. We then also had to dump selectively, but it was no big deal. Eventually,
    once it became clear that the concepts would not change a variant was made for \LUAMETATEX: we
    just use a two times larger lower array when we have delimiters. This saves some memory. The
    price we pay is that a stack entry now has two values but that is not really an issue.

    By packing the math code values we loose the option to store an active state but that's no big
    deal.

    todo: consider simple char array for catcodes.

    The code here is somewhat messy because we generalized it a bit. Maybe I'll redo it some day.

 */

typedef struct sparse_state_info {
    memory_data sparse_data;
} sparse_state_info;

extern sparse_state_info lmt_sparse_state;

typedef struct sa_mathblob {
    unsigned int class_value:math_class_bits;
    unsigned int family_value:math_family_bits;
    unsigned int character_value:math_character_bits;
} sa_mathblob;

typedef struct sa_mathspec {
    unsigned short properties;
    unsigned short group;
    unsigned int   index;
} sa_mathspec;

typedef struct packed_math_character {
    union {
        sa_mathblob sa_value;
        unsigned    ui_value;
    };
} packed_math_character;

typedef union sa_tree_item {
    unsigned int   uint_value;
    int            int_value;
    sa_mathblob    math_code_value;
    sa_mathspec    math_spec_value;
    unsigned short ushort_value[2];
    unsigned char  uchar_value[4];
} sa_tree_item;

typedef struct sa_stack_item {
    int          code;
    int          level;
    sa_tree_item value_1;
    sa_tree_item value_2;
} sa_stack_item;

typedef struct sa_tree_head {
    int              sa_stack_size; /*tex initial stack size   */
    int              sa_stack_step; /*tex increment stack step */
    int              sa_stack_ptr;  /*tex current stack point  */
    sa_tree_item     dflt;          /*tex default item value   */
    sa_tree_item  ***tree;          /*tex item tree head       */
    sa_stack_item   *stack;         /*tex stack tree head      */
    int              bytes;         /*tex the number of items per entry */
    int              padding;
} sa_tree_head;

typedef sa_tree_head *sa_tree;

extern int     sa_get_item_1    (const sa_tree head, int n);                                     /* these return the value or dflt */
extern int     sa_get_item_2    (const sa_tree head, int n);                                     /* these return the value or dflt */
extern int     sa_get_item_4    (const sa_tree head, int n, sa_tree_item *v);                    /* these return success */
extern int     sa_get_item_8    (const sa_tree head, int n, sa_tree_item *v1, sa_tree_item *v2); /* these return success */
extern void    sa_set_item_1    (sa_tree head, int n, int v, int gl);
extern void    sa_set_item_2    (sa_tree head, int n, int v, int gl);
extern void    sa_set_item_4    (sa_tree head, int n, sa_tree_item v, int gl);
extern void    sa_set_item_8    (sa_tree head, int n, sa_tree_item v1, sa_tree_item v2, int gl);
extern sa_tree sa_new_tree      (int size, int bytes, sa_tree_item dflt);
extern sa_tree sa_copy_tree     (sa_tree head);
extern void    sa_destroy_tree  (sa_tree head);
extern void    sa_dump_tree     (dumpstream f, sa_tree a);
extern sa_tree sa_undump_tree   (dumpstream f);
extern void    sa_restore_stack (sa_tree a, int gl);
extern void    sa_clear_stack   (sa_tree a);

extern void    sa_set_item_n    (const sa_tree head, int n, int v, int gl);
extern int     sa_get_item_n    (const sa_tree head, int n);

inline static halfword sa_return_item_1(sa_tree head, halfword n)
{
    if (head->tree) {
        int hp = LMT_SA_H_PART(n);
        if (head->tree[hp]) {
            int mp = LMT_SA_M_PART(n);
            if (head->tree[hp][mp]) {
                return (halfword) head->tree[hp][mp][LMT_SA_L_PART(n)/4].uchar_value[n%4];
            }
        }
    }
    return (halfword) head->dflt.uchar_value[0];
}

inline static halfword sa_return_item_2(sa_tree head, halfword n)
{
    if (head->tree) {
        int hp = LMT_SA_H_PART(n);
        if (head->tree[hp]) {
            int mp = LMT_SA_M_PART(n);
            if (head->tree[hp][mp]) {
                return (halfword) head->tree[hp][mp][LMT_SA_L_PART(n)/2].ushort_value[n%2];
            }
        }
    }
    return (halfword) head->dflt.ushort_value[0];
}

inline static halfword sa_return_item_4(sa_tree head, halfword n)
{
    if (head->tree) {
        int hp = LMT_SA_H_PART(n);
        if (head->tree[hp]) {
            int mp = LMT_SA_M_PART(n);
            if (head->tree[hp][mp]) {
                return (halfword) head->tree[hp][mp][LMT_SA_L_PART(n)].int_value;
            }
        }
    }
    return (halfword) head->dflt.int_value;
}

inline static void sa_rawset_item_1(sa_tree head, halfword n, unsigned char v)
{
    head->tree[LMT_SA_H_PART(n)][LMT_SA_M_PART(n)][LMT_SA_L_PART(n)/4].uchar_value[n%4] = v;
}

inline static void sa_rawset_item_2(sa_tree head, halfword n, unsigned short v)
{
    head->tree[LMT_SA_H_PART(n)][LMT_SA_M_PART(n)][LMT_SA_L_PART(n)/2].ushort_value[n%2] = v;
}

inline static void sa_rawset_item_4(sa_tree head, halfword n, sa_tree_item v)
{
    head->tree[LMT_SA_H_PART(n)][LMT_SA_M_PART(n)][LMT_SA_L_PART(n)] = v;
}

inline static void sa_rawset_item_8(sa_tree head, halfword n, sa_tree_item v1, sa_tree_item v2)
{
    sa_tree_item *low = head->tree[LMT_SA_H_PART(n)][LMT_SA_M_PART(n)];
    int l = 2*LMT_SA_L_PART(n);
    low[l] = v1;
    low[l+1] = v2;
}

// inline them

extern void *sa_malloc_array  (int recordsize, int size);
extern void *sa_realloc_array (void *p, int recordsize, int size, int step);
extern void *sa_calloc_array  (int recordsize, int size);
extern void *sa_free_array    (void *p);
extern void  sa_wipe_array    (void *head, int recordsize, int size);

# endif