Skip to content

Commit 63d3b24

Browse files
committed
Move the condesc_t::more data to a new element condesc_more_t
1 parent 10b565a commit 63d3b24

File tree

4 files changed

+54
-40
lines changed

4 files changed

+54
-40
lines changed

link-grammar/connectors.c

+41-32
Original file line numberDiff line numberDiff line change
@@ -288,27 +288,27 @@ static void connector_encode_lc(const char *lc_string, condesc_t *desc)
288288
*
289289
* Note: check_connector() has already validated the connector string.
290290
*/
291-
void calculate_connector_info(hdesc_t *hdesc)
291+
void calculate_connector_info(condesc_t *condesc)
292292
{
293293
const char *s;
294+
condesc_more_t *m = condesc->more;
294295

295-
s = hdesc->string;
296+
s = m->string;
296297
if (islower((unsigned char)*s))
297298
{
298-
dassert((hdesc->string[0] == 'h') || (hdesc->string[0] == 'd'),
299-
"\'%hdesc\': Bad head/dependent character", hdesc->string[0]);
299+
dassert((s[0] == 'h') || (s[0] == 'd'), "'%s': Bad head/dependent", s);
300300

301-
if ((*s == 'h') || (*s == 'd')) hdesc->flags |= CD_HEAD_DEPENDENT;
302-
if (*s == 'h') hdesc->flags |= CD_HEAD;
301+
if ((s[0] == 'h') || (s[0] == 'd')) m->flags |= CD_HEAD_DEPENDENT;
302+
if (s[0] == 'h') m->flags |= CD_HEAD;
303303
s++; /* Ignore head-dependent indicator. */
304304
}
305305

306-
hdesc->uc_start = (uint8_t)(s - hdesc->string);
306+
m->uc_start = (uint8_t)(s - m->string);
307307
/* Skip the uppercase part. */
308308
do { s++; } while (is_connector_name_char(*s));
309-
hdesc->uc_length = (uint8_t)(s - hdesc->string - hdesc->uc_start);
309+
m->uc_length = (uint8_t)(s - m->string - m->uc_start);
310310

311-
connector_encode_lc(s, hdesc->desc);
311+
connector_encode_lc(s, condesc);
312312
}
313313

314314
/* ================= Connector descriptor table. ====================== */
@@ -407,35 +407,36 @@ int condesc_by_uc_constring(const void * a, const void * b)
407407
*/
408408
static bool sort_condesc_by_uc_constring(Dictionary dict)
409409
{
410-
if ((0 == dict->contable.num_con) && !IS_DYNAMIC_DICT(dict))
410+
ConTable *ct = &dict->contable;
411+
412+
if ((0 == ct->num_con) && !IS_DYNAMIC_DICT(dict))
411413
{
412414
prt_error("Error: Dictionary %s: No connectors found.\n", dict->name);
413415
return false;
414416
}
415417

416418
/* An SQL dict without <UNKNOWN-WORD> may have 0 connectors here. */
417-
if (0 == dict->contable.num_con)
419+
if (0 == ct->num_con)
418420
return true;
419421

420-
condesc_t **sdesc = malloc(dict->contable.num_con * sizeof(condesc_t *));
422+
condesc_t **sdesc = malloc(ct->num_con * sizeof(condesc_t *));
421423
size_t i = 0;
422-
for (size_t n = 0; n < dict->contable.size; n++)
424+
for (size_t n = 0; n < ct->size; n++)
423425
{
424-
condesc_t *condesc = dict->contable.hdesc[n].desc;
426+
condesc_t *condesc = ct->hdesc[n].desc;
425427

426428
if (NULL == condesc) continue;
427-
calculate_connector_info(&dict->contable.hdesc[n]);
428-
sdesc[i++] = dict->contable.hdesc[n].desc;
429+
calculate_connector_info(condesc);
430+
sdesc[i++] = condesc;
429431
}
430432

431-
qsort(sdesc, dict->contable.num_con, sizeof(*dict->contable.sdesc),
432-
condesc_by_uc_constring);
433+
qsort(sdesc, ct->num_con, sizeof(*ct->sdesc), condesc_by_uc_constring);
433434

434435
/* Enumerate the connectors according to their UC part. */
435436
int uc_num = 0;
436437

437438
sdesc[0]->uc_num = uc_num;
438-
for (size_t n = 1; n < dict->contable.num_con; n++)
439+
for (size_t n = 1; n < ct->num_con; n++)
439440
{
440441
condesc_t **condesc = &sdesc[n];
441442

@@ -461,10 +462,10 @@ static bool sort_condesc_by_uc_constring(Dictionary dict)
461462

462463
lgdebug(+11, "Dictionary %s: %zu different connectors "
463464
"(%d with a different UC part)\n",
464-
dict->name, dict->contable.num_con, uc_num+1);
465+
dict->name, ct->num_con, uc_num+1);
465466

466-
dict->contable.sdesc = sdesc;
467-
dict->contable.num_uc = uc_num + 1;
467+
ct->sdesc = sdesc;
468+
ct->num_uc = uc_num + 1;
468469

469470
/* hdesc is not freed here because it is needed for finding ZZZ.
470471
* It could be freed here if we have ZZZ cached in the dict structure. */
@@ -477,6 +478,7 @@ void condesc_delete(Dictionary dict)
477478

478479
free(ct->hdesc);
479480
pool_delete(ct->desc_pool);
481+
pool_delete(ct->more_pool);
480482
condesc_length_limit_def_delete(ct);
481483
}
482484

@@ -488,14 +490,15 @@ void condesc_reuse(Dictionary dict)
488490
ct->num_uc = 0;
489491
memset(ct->hdesc, 0, ct->size * sizeof(hdesc_t));
490492
pool_reuse(ct->desc_pool);
493+
pool_reuse(ct->more_pool);
491494
}
492495

493496
static hdesc_t *condesc_find(ConTable *ct, const char *constring, uint32_t hash)
494497
{
495498
uint32_t i = hash & (ct->size-1);
496499

497500
while ((NULL != ct->hdesc[i].desc) &&
498-
!string_set_cmp(constring, ct->hdesc[i].string))
501+
!string_set_cmp(constring, ct->hdesc[i].desc->more->string))
499502
{
500503
i = (i + 1) & (ct->size-1);
501504
}
@@ -522,18 +525,18 @@ static bool condesc_grow(ConTable *ct)
522525

523526
for (size_t i = 0; i < old_size; i++)
524527
{
525-
hdesc_t *old_h = &old_hdesc[i];
526-
if (NULL == old_h->desc) continue;
527-
hdesc_t *new_h = condesc_find(ct, old_h->string, old_h->str_hash);
528+
condesc_t *old_desc = old_hdesc[i].desc;
529+
if (NULL == old_desc) continue;
530+
hdesc_t *new_hdesc =
531+
condesc_find(ct, old_desc->more->string, old_desc->more->str_hash);
528532

529-
if (NULL != new_h->desc)
533+
if (NULL != new_hdesc->desc)
530534
{
531535
prt_error("Fatal Error: condesc_grow(): Internal error\n");
532536
free(old_hdesc);
533537
return false;
534538
}
535-
*new_h = *old_h;
536-
new_h->desc->more = new_h;
539+
new_hdesc->desc = old_desc;
537540
}
538541

539542
free(old_hdesc);
@@ -548,12 +551,15 @@ condesc_t *condesc_add(ConTable *ct, const char *constring)
548551
if (NULL == h->desc)
549552
{
550553
lgdebug(+11, "Creating connector '%s' (%zu)\n", constring, ct->num_con);
554+
551555
h->desc = pool_alloc(ct->desc_pool);
552-
h->string = constring;
553556
h->desc->uc_num = UINT32_MAX;
554-
h->str_hash = hash;
555-
h->desc->more = h;
556557
h->desc->con_num = ct->num_con;
558+
559+
condesc_more_t *m = h->desc->more = pool_alloc(ct->desc_pool);
560+
m->string = constring;
561+
m->str_hash = hash;
562+
557563
ct->num_con++;
558564

559565
if ((8 * ct->num_con) > (3 * ct->size))
@@ -573,6 +579,9 @@ void condesc_init(Dictionary dict, size_t num_con)
573579
ct->desc_pool = pool_new(__func__, "condesc_t",
574580
/*num_elements*/num_con, sizeof(condesc_t),
575581
/*zero_out*/true, /*align*/true, /*exact*/false);
582+
ct->more_pool = pool_new(__func__, "condesc_more_t",
583+
/*num_elements*/num_con, sizeof(condesc_more_t),
584+
/*zero_out*/true, /*align*/true, /*exact*/false);
576585

577586
// Connector hash table must be an exact power of two.
578587
int nbits = 0;

link-grammar/connectors.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ typedef struct condesc_struct condesc_t;
8686
typedef struct hdesc
8787
{
8888
condesc_t *desc;
89-
const char *string; /* The connector name w/o the direction mark, e.g. AB */
90-
// float *cost; // Array of cost by connector length (cost[0]: default)
89+
} hdesc_t;
90+
91+
typedef struct
92+
{
93+
const char *string; /* The connector name w/o the direction mark, e.g. ABc */
9194
connector_uc_hash_t str_hash;
9295
uint8_t length_limit; /* If not 0, it gives the limit of the length of the
9396
* link that can be used on this connector type. The
@@ -100,7 +103,9 @@ typedef struct hdesc
100103
/* For connector match speedup when sorting the connector table. */
101104
uint8_t uc_length; /* uc part length */
102105
uint8_t uc_start; /* uc start position */
103-
} hdesc_t;
106+
107+
// float *cost; // Array of cost by connector length (cost[0]: default)
108+
} condesc_more_t;
104109

105110
/* Each connector type has a connector descriptor. The size of this
106111
* struct is 32 byes, to facilitate CPU memory caching during parsing.
@@ -114,7 +119,7 @@ struct condesc_struct
114119
{
115120
lc_enc_t lc_letters;
116121
lc_enc_t lc_mask;
117-
hdesc_t *more; /* More information, for keeping small struct size. */
122+
condesc_more_t *more;/* More information, for keeping small struct size. */
118123
connector_uc_hash_t uc_num; /* uc part enumeration. */
119124
uint32_t con_num; /* Connector ordinal number. */
120125
};
@@ -224,7 +229,7 @@ static inline unsigned int connector_num(const Connector * c)
224229
Connector * connector_new(Pool_desc *, const condesc_t *);
225230
void set_connector_farthest_word(Exp *, int, int, Parse_Options);
226231
void free_connectors(Connector *);
227-
void calculate_connector_info(hdesc_t *);
232+
void calculate_connector_info(condesc_t *);
228233
int condesc_by_uc_constring(const void *, const void *);
229234

230235
/**

link-grammar/dict-atomese/lookup-atomese.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static void update_condesc(Dictionary dict)
422422
if (NULL == condesc) continue;
423423
if (UINT32_MAX != condesc->uc_num) continue;
424424

425-
calculate_connector_info(&ct->hdesc[n]);
425+
calculate_connector_info(condesc);
426426
condesc->more->length_limit = UNLIMITED_LEN;
427427
sdesc[i++] = condesc;
428428
}

link-grammar/parse/prune.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ static void clean_table(unsigned int size, C_list **t)
448448
{
449449
/* Table entry tombstone. */
450450
#define UC_NUM_TOMBSTONE ((connector_uc_hash_t)-1)
451-
static hdesc_t hdesc_no_match =
451+
static condesc_more_t cm_no_match =
452452
{
453453
.string = "TOMBSTONE",
454454
};
455455
static condesc_t desc_no_match =
456456
{
457457
.uc_num = UC_NUM_TOMBSTONE, /* get_power_table_entry() will skip. */
458-
.more = &hdesc_no_match
458+
.more = &cm_no_match
459459
};
460460
static Connector con_no_match =
461461
{

0 commit comments

Comments
 (0)