mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +00:00 
			
		
		
		
	Add invert and simplify
env-lookups to env-lookup
This commit is contained in:
		| @@ -710,6 +710,16 @@ | ||||
|      :tuple tuple.reverse | ||||
|      :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 | ||||
|   "Creates an table or tuple from two arrays/tuples. If a third argument of | ||||
|   :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) */ | ||||
| JanetTable *janet_env_reg(JanetTable *env) { | ||||
| JanetTable *janet_env_lookup(JanetTable *env) { | ||||
|     JanetTable *renv = janet_table(env->count); | ||||
|     while (env) { | ||||
|         for (int32_t i = 0; i < env->capacity; i++) { | ||||
| @@ -1087,14 +1071,11 @@ int janet_unmarshal( | ||||
|  | ||||
| /* C functions */ | ||||
|  | ||||
| static int cfun_env_lookups(JanetArgs args) { | ||||
| static int cfun_env_lookup(JanetArgs args) { | ||||
|     JanetTable *env; | ||||
|     Janet tup[2]; | ||||
|     JANET_FIXARITY(args, 1); | ||||
|     JANET_ARG_TABLE(env, args, 0); | ||||
|     tup[0] = janet_wrap_table(janet_env_rreg(env)); | ||||
|     tup[1] = janet_wrap_table(janet_env_reg(env)); | ||||
|     JANET_RETURN_TUPLE(args, janet_tuple_n(tup, 2)); | ||||
|     JANET_RETURN_TABLE(args, janet_env_lookup(env)); | ||||
| } | ||||
|  | ||||
| static int cfun_marshal(JanetArgs args) { | ||||
| @@ -1145,7 +1126,7 @@ static int cfun_unmarshal(JanetArgs args) { | ||||
| static const JanetReg cfuns[] = { | ||||
|     {"marshal", cfun_marshal}, | ||||
|     {"unmarshal", cfun_unmarshal}, | ||||
|     {"env-lookups", cfun_env_lookups}, | ||||
|     {"env-lookup", cfun_env_lookup}, | ||||
|     {NULL, NULL} | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -1037,8 +1037,7 @@ JANET_API int janet_unmarshal( | ||||
|         Janet *out, | ||||
|         JanetTable *reg, | ||||
|         const uint8_t **next); | ||||
| JANET_API JanetTable *janet_env_rreg(JanetTable *env); | ||||
| JANET_API JanetTable *janet_env_reg(JanetTable *env); | ||||
| JANET_API JanetTable *janet_env_lookup(JanetTable *env); | ||||
|  | ||||
| /* GC */ | ||||
| JANET_API void janet_mark(Janet x); | ||||
|   | ||||
| @@ -141,7 +141,8 @@ | ||||
|  | ||||
| # 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] | ||||
|   (def marshx (marshal x m-lookup)) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose