2020-06-15 14:18:57 +00:00
|
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-11-25 16:19:00 +00:00
|
|
|
|
#include "libc/alg/arraylist.internal.h"
|
|
|
|
|
#include "libc/alg/arraylist2.internal.h"
|
2020-12-27 15:02:35 +00:00
|
|
|
|
#include "libc/bits/safemacros.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
#include "libc/mem/mem.h"
|
2020-12-25 00:01:48 +00:00
|
|
|
|
#include "libc/nexgen32e/crc32.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
|
#include "libc/x/x.h"
|
|
|
|
|
#include "tool/build/lib/interner.h"
|
|
|
|
|
|
|
|
|
|
#define kInitialItems 16
|
|
|
|
|
|
|
|
|
|
struct InternerObject {
|
|
|
|
|
struct Interner pool;
|
|
|
|
|
size_t i, n;
|
|
|
|
|
struct InternerHash {
|
|
|
|
|
unsigned hash; /* 0 means empty */
|
|
|
|
|
unsigned index;
|
|
|
|
|
} * p;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void rehash(struct InternerObject *it) {
|
|
|
|
|
size_t i, j, n, step;
|
|
|
|
|
struct InternerHash *p;
|
|
|
|
|
n = it->n;
|
|
|
|
|
p = it->p;
|
|
|
|
|
it->p = xcalloc((it->n <<= 1), sizeof(struct InternerHash));
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
|
if (!p[i].hash) continue;
|
|
|
|
|
step = 0;
|
|
|
|
|
do {
|
2020-12-24 07:42:56 +00:00
|
|
|
|
j = (p[i].hash + step * ((step + 1) >> 1)) & (it->n - 1);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
step++;
|
|
|
|
|
} while (it->p[j].hash);
|
|
|
|
|
memcpy(&it->p[j], &p[i], sizeof(p[i]));
|
|
|
|
|
}
|
|
|
|
|
free_s(&p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates new interner.
|
|
|
|
|
*/
|
|
|
|
|
struct Interner *newinterner(void) {
|
|
|
|
|
struct InternerObject *it;
|
|
|
|
|
it = xcalloc(1, sizeof(*it));
|
|
|
|
|
it->p = xcalloc((it->n = kInitialItems), sizeof(*it->p));
|
|
|
|
|
it->pool.p = xcalloc((it->pool.n = kInitialItems), sizeof(*it->pool.p));
|
|
|
|
|
return &it->pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroys interner.
|
|
|
|
|
*/
|
|
|
|
|
void freeinterner(struct Interner *t) {
|
|
|
|
|
struct InternerObject *it = (struct InternerObject *)t;
|
|
|
|
|
if (it) {
|
|
|
|
|
free(it->pool.p);
|
|
|
|
|
free(it->p);
|
|
|
|
|
free(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns number of unique items interned.
|
|
|
|
|
*/
|
|
|
|
|
size_t interncount(const struct Interner *t) {
|
|
|
|
|
struct InternerObject *it = (struct InternerObject *)t;
|
|
|
|
|
return it->i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interns object.
|
|
|
|
|
*
|
|
|
|
|
* @return index into 𝑡→𝑝 holding equal item
|
|
|
|
|
* @note use consistent size w/ non-string items
|
|
|
|
|
*/
|
|
|
|
|
size_t internobj(struct Interner *t, const void *data, size_t size) {
|
2020-09-07 04:39:00 +00:00
|
|
|
|
char *item;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
unsigned hash;
|
|
|
|
|
size_t i, step;
|
2020-09-07 04:39:00 +00:00
|
|
|
|
struct InternerObject *it;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
step = 0;
|
|
|
|
|
item = data;
|
2020-09-07 04:39:00 +00:00
|
|
|
|
it = (struct InternerObject *)t;
|
2020-12-25 00:01:48 +00:00
|
|
|
|
hash = max(1, crc32c(0, data, size));
|
2020-06-15 14:18:57 +00:00
|
|
|
|
do {
|
2020-07-01 02:55:47 +00:00
|
|
|
|
/* it is written that triangle probe halts iff i<n/2 && popcnt(n)==1 */
|
2020-12-24 07:42:56 +00:00
|
|
|
|
i = (hash + step * ((step + 1) >> 1)) & (it->n - 1);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
if (it->p[i].hash == hash && it->p[i].index + size <= it->pool.n &&
|
|
|
|
|
memcmp(item, &it->pool.p[it->p[i].index], size) == 0) {
|
|
|
|
|
return it->p[i].index;
|
|
|
|
|
}
|
|
|
|
|
step++;
|
|
|
|
|
} while (it->p[i].hash);
|
|
|
|
|
if (++it->i == (it->n >> 1)) {
|
|
|
|
|
rehash(it);
|
|
|
|
|
step = 0;
|
|
|
|
|
do {
|
2020-12-24 07:42:56 +00:00
|
|
|
|
i = (hash + step * ((step + 1) >> 1)) & (it->n - 1);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
step++;
|
|
|
|
|
} while (it->p[i].hash);
|
|
|
|
|
}
|
|
|
|
|
it->p[i].hash = hash;
|
2020-09-07 04:39:00 +00:00
|
|
|
|
return (it->p[i].index =
|
|
|
|
|
CONCAT(&it->pool.p, &it->pool.i, &it->pool.n, item, size));
|
2020-06-15 14:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interns string.
|
|
|
|
|
*
|
|
|
|
|
* The NUL-terminated string 𝑠 is concatenated to the relocatable
|
|
|
|
|
* double-NUL terminated string list 𝑡→𝑝 with de-duplication and
|
|
|
|
|
* preservation of insertion order.
|
|
|
|
|
*
|
|
|
|
|
* @return index into 𝑡→𝑝 holding string equal to 𝑠
|
|
|
|
|
*/
|
|
|
|
|
size_t intern(struct Interner *t, const char *s) {
|
|
|
|
|
return internobj(t, s, strlen(s) + 1);
|
|
|
|
|
}
|