mirror of
https://github.com/janet-lang/janet
synced 2024-11-19 23:24:49 +00:00
Add invert and simplify
env-lookups to env-lookup
This commit is contained in:
parent
98f2c6feab
commit
92202e1c8b
@ -710,6 +710,16 @@
|
|||||||
:tuple tuple.reverse
|
:tuple tuple.reverse
|
||||||
:array array.reverse) t))
|
:array array.reverse) t))
|
||||||
|
|
||||||
|
(defn invert
|
||||||
|
"Returns a table of where the keys of an associative data structure
|
||||||
|
are the values, and the values of the keys. If multiple keys have the same
|
||||||
|
value, one key will be ignored."
|
||||||
|
[ds]
|
||||||
|
(def ret @{})
|
||||||
|
(loop [k :keys ds]
|
||||||
|
(put ret (get ds k) k))
|
||||||
|
ret)
|
||||||
|
|
||||||
(defn zipcoll
|
(defn zipcoll
|
||||||
"Creates an table or tuple from two arrays/tuples. If a third argument of
|
"Creates an table or tuple from two arrays/tuples. If a third argument of
|
||||||
:struct is given result is struct else is table."
|
:struct is given result is struct else is table."
|
||||||
|
@ -100,24 +100,8 @@ static Janet entry_getval(Janet env_entry) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make a reverse lookup table for an environment (for marshaling) */
|
|
||||||
JanetTable *janet_env_rreg(JanetTable *env) {
|
|
||||||
JanetTable *renv = janet_table(env->count);
|
|
||||||
while (env) {
|
|
||||||
for (int32_t i = 0; i < env->capacity; i++) {
|
|
||||||
if (janet_checktype(env->data[i].key, JANET_SYMBOL)) {
|
|
||||||
janet_table_put(renv,
|
|
||||||
entry_getval(env->data[i].value),
|
|
||||||
env->data[i].key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
env = env->proto;
|
|
||||||
}
|
|
||||||
return renv;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Make a forward lookup table from an environment (for unmarshaling) */
|
/* Make a forward lookup table from an environment (for unmarshaling) */
|
||||||
JanetTable *janet_env_reg(JanetTable *env) {
|
JanetTable *janet_env_lookup(JanetTable *env) {
|
||||||
JanetTable *renv = janet_table(env->count);
|
JanetTable *renv = janet_table(env->count);
|
||||||
while (env) {
|
while (env) {
|
||||||
for (int32_t i = 0; i < env->capacity; i++) {
|
for (int32_t i = 0; i < env->capacity; i++) {
|
||||||
@ -1087,14 +1071,11 @@ int janet_unmarshal(
|
|||||||
|
|
||||||
/* C functions */
|
/* C functions */
|
||||||
|
|
||||||
static int cfun_env_lookups(JanetArgs args) {
|
static int cfun_env_lookup(JanetArgs args) {
|
||||||
JanetTable *env;
|
JanetTable *env;
|
||||||
Janet tup[2];
|
|
||||||
JANET_FIXARITY(args, 1);
|
JANET_FIXARITY(args, 1);
|
||||||
JANET_ARG_TABLE(env, args, 0);
|
JANET_ARG_TABLE(env, args, 0);
|
||||||
tup[0] = janet_wrap_table(janet_env_rreg(env));
|
JANET_RETURN_TABLE(args, janet_env_lookup(env));
|
||||||
tup[1] = janet_wrap_table(janet_env_reg(env));
|
|
||||||
JANET_RETURN_TUPLE(args, janet_tuple_n(tup, 2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cfun_marshal(JanetArgs args) {
|
static int cfun_marshal(JanetArgs args) {
|
||||||
@ -1145,7 +1126,7 @@ static int cfun_unmarshal(JanetArgs args) {
|
|||||||
static const JanetReg cfuns[] = {
|
static const JanetReg cfuns[] = {
|
||||||
{"marshal", cfun_marshal},
|
{"marshal", cfun_marshal},
|
||||||
{"unmarshal", cfun_unmarshal},
|
{"unmarshal", cfun_unmarshal},
|
||||||
{"env-lookups", cfun_env_lookups},
|
{"env-lookup", cfun_env_lookup},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1037,8 +1037,7 @@ JANET_API int janet_unmarshal(
|
|||||||
Janet *out,
|
Janet *out,
|
||||||
JanetTable *reg,
|
JanetTable *reg,
|
||||||
const uint8_t **next);
|
const uint8_t **next);
|
||||||
JANET_API JanetTable *janet_env_rreg(JanetTable *env);
|
JANET_API JanetTable *janet_env_lookup(JanetTable *env);
|
||||||
JANET_API JanetTable *janet_env_reg(JanetTable *env);
|
|
||||||
|
|
||||||
/* GC */
|
/* GC */
|
||||||
JANET_API void janet_mark(Janet x);
|
JANET_API void janet_mark(Janet x);
|
||||||
|
@ -141,7 +141,8 @@
|
|||||||
|
|
||||||
# Marshal
|
# Marshal
|
||||||
|
|
||||||
(def [m-lookup um-lookup] (env-lookups _env))
|
(def um-lookup (env-lookup _env))
|
||||||
|
(def m-lookup (invert um-lookup))
|
||||||
|
|
||||||
(defn testmarsh [x msg]
|
(defn testmarsh [x msg]
|
||||||
(def marshx (marshal x m-lookup))
|
(def marshx (marshal x m-lookup))
|
||||||
|
Loading…
Reference in New Issue
Block a user