From a18770487227bce6e356f169aa86bdef70370c76 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 18 May 2007 21:19:14 +0000 Subject: r12980@Kushana: nickm | 2007-05-18 14:11:05 -0400 Add a "swap" function to smartlist, add a "shuffle" function for smartlist to crypto.c, and make appropriate hashtable functions be more const. svn:r10208 --- src/common/crypto.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/common/crypto.c') diff --git a/src/common/crypto.c b/src/common/crypto.c index 40cb5fafae..bcb8a375a8 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -1677,6 +1677,21 @@ smartlist_choose(const smartlist_t *sl) return NULL; /* no elements to choose from */ } +/** Scramble the elements of sl into a random order. */ +void +smartlist_shuffle(smartlist_t *sl) +{ + int i; + /* From the end of the list to the front, choose at random from the + positions we haven't looked at yet, and swap that position into the + current position. Remember to give "no swap" the same probability as + any other swap. */ + for (i = smartlist_len(sl)-1; i > 0; --i) { + int j = crypto_rand_int(i+1); + smartlist_swap(sl, i, j); + } +} + /** Base-64 encode srclen bytes of data from src. Write * the result into dest, if it will fit within destlen * bytes. Return the number of bytes written on success; -1 if -- cgit v1.2.3-54-g00ecf