Commit 5d19de3b authored by Moe Jette's avatar Moe Jette
Browse files

patch from Gerrit: 01_hostlist_parse_int_into_array.diff

hostlist: shorter conversion

This replaces the hostlist_parse_int_to_array() routine with a simpler and 
equivalent routine. I was wondering what the algorithm does, when suddenly
the flow became simpler.

Problem: we want to create an array 'out' of 'dims' dimensions where
         out[i], 0 <= i < dims, has the coefficients a_j of the polynomial

	 a_(dims-1) * B^(dims-1) + ... + a_1 * B + a_0

	 where B is the base of the polynomial 'in' and
	    a_0        = out[dims - 1]
	    a_1        = out[dims - 2]
	    ...
	    a_(dims-1) = a[0]

Using the Horner Scheme, 'in' is represented as
 * dims = 1:                                   a_0 = out[0]
 * dims = 2:                        a_1  * B + a_0 = out[0] * B + out[1]
 * dims = 3:            (a_2  * B + a_1) * B + a_0 
 * dims = 4: ((a_3 * B + a_2) * B + a_1) * B + a_0
   ...
 * dims = n: ((a_(n-1) * B + a_(n-2)) * B ... + a_1) * B + a_0

We can get a_0 = out[dims-1] as "in % B" and then derive a_1, a_2, ... by
first dividing by B, and then repeating the process.

If in is 0, all 'dims' coefficients are 0, if it is negative, all coefficients
are negative.
parent 9ea0c22d
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment