mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	speed up range creation
This commit is contained in:
		| @@ -1007,30 +1007,6 @@ | |||||||
|   (map-template :keep res pred ind inds) |   (map-template :keep res pred ind inds) | ||||||
|   res) |   res) | ||||||
|  |  | ||||||
| (defn range |  | ||||||
|   `Create an array of values [start, end) with a given step. |  | ||||||
|   With one argument, returns a range [0, end). With two arguments, returns |  | ||||||
|   a range [start, end). With three, returns a range with optional step size.` |  | ||||||
|   [& args] |  | ||||||
|   (case (length args) |  | ||||||
|     1 (do |  | ||||||
|         (def [n] args) |  | ||||||
|         (def arr (array/new n)) |  | ||||||
|         (forv i 0 n (put arr i i)) |  | ||||||
|         arr) |  | ||||||
|     2 (do |  | ||||||
|         (def [n m] args) |  | ||||||
|         (def arr (array/new (- m n))) |  | ||||||
|         (forv i n m (put arr (- i n) i)) |  | ||||||
|         arr) |  | ||||||
|     3 (do |  | ||||||
|         (def [n m s] args) |  | ||||||
|         (cond |  | ||||||
|           (zero? s) @[] |  | ||||||
|           (neg? s) (seq [i :down [n m (- s)]] i) |  | ||||||
|           (seq [i :range [n m s]] i))) |  | ||||||
|     (error "expected 1 to 3 arguments to range"))) |  | ||||||
|  |  | ||||||
| (defn find-index | (defn find-index | ||||||
|   ``Find the index of indexed type for which `pred` is true. Returns `dflt` if not found.`` |   ``Find the index of indexed type for which `pred` is true. Returns `dflt` if not found.`` | ||||||
|   [pred ind &opt dflt] |   [pred ind &opt dflt] | ||||||
|   | |||||||
| @@ -426,6 +426,39 @@ JANET_CORE_FN(janet_core_slice, | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | JANET_CORE_FN(janet_core_range, | ||||||
|  |               "(range & args)", | ||||||
|  |               "Create an array of values [start, end) with a given step. " | ||||||
|  |               "With one argument, returns a range [0, end). With two arguments, returns " | ||||||
|  |               "a range [start, end). With three, returns a range with optional step size.") { | ||||||
|  |     janet_arity(argc, 1, 3); | ||||||
|  |     int32_t start = 0, stop = 0, step = 1, count = 0; | ||||||
|  |     switch (argc) { | ||||||
|  |         case 1: | ||||||
|  |             stop = janet_getinteger(argv, 0); | ||||||
|  |             count = stop; | ||||||
|  |             break; | ||||||
|  |         case 2: | ||||||
|  |             start = janet_getinteger(argv, 0); | ||||||
|  |             stop = janet_getinteger(argv, 1); | ||||||
|  |             count = stop - start; | ||||||
|  |             break; | ||||||
|  |         case 3: | ||||||
|  |             start = janet_getinteger(argv, 0); | ||||||
|  |             stop = janet_getinteger(argv, 1); | ||||||
|  |             step = janet_getinteger(argv, 2); | ||||||
|  |             count = (step > 0) ? (stop - start - 1) / step + 1 : | ||||||
|  |                     ((step < 0) ? (stop - start + 1) / step + 1 : 0); | ||||||
|  |             break; | ||||||
|  |     } | ||||||
|  |     JanetArray *array = janet_array(count); | ||||||
|  |     for (int32_t i = 0; i < count; i++) { | ||||||
|  |         array->data[i] = janet_wrap_number(start + i * step); | ||||||
|  |     } | ||||||
|  |     array->count = count; | ||||||
|  |     return janet_wrap_array(array); | ||||||
|  | } | ||||||
|  |  | ||||||
| JANET_CORE_FN(janet_core_table, | JANET_CORE_FN(janet_core_table, | ||||||
|               "(table & kvs)", |               "(table & kvs)", | ||||||
|               "Creates a new table from a variadic number of keys and values. " |               "Creates a new table from a variadic number of keys and values. " | ||||||
| @@ -1024,6 +1057,7 @@ static void janet_load_libs(JanetTable *env) { | |||||||
|         JANET_CORE_REG("int?", janet_core_check_int), |         JANET_CORE_REG("int?", janet_core_check_int), | ||||||
|         JANET_CORE_REG("nat?", janet_core_check_nat), |         JANET_CORE_REG("nat?", janet_core_check_nat), | ||||||
|         JANET_CORE_REG("slice", janet_core_slice), |         JANET_CORE_REG("slice", janet_core_slice), | ||||||
|  |         JANET_CORE_REG("range", janet_core_range), | ||||||
|         JANET_CORE_REG("signal", janet_core_signal), |         JANET_CORE_REG("signal", janet_core_signal), | ||||||
|         JANET_CORE_REG("memcmp", janet_core_memcmp), |         JANET_CORE_REG("memcmp", janet_core_memcmp), | ||||||
|         JANET_CORE_REG("getproto", janet_core_getproto), |         JANET_CORE_REG("getproto", janet_core_getproto), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 primo-ppcg
					primo-ppcg