1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//// #License
/// MIT or APPACHE 2.  
/// Use at your own risk, I'm basically a sweaty gorilla that can press computer keys and this is my first published crate mostly made for the joy of learning rust and also as a gift to the exceptional rust community.  
/// Frank crate for Fetch and Rank.  There are several generic friendly ranking functions from ranks described by Wassily Hoeffding in ~1947 to R language style ranking and dense ranks.  It also includes and a generic FETCH function that builds a child vector from a parent vector given a usize list of picks.  
/// A few extras: imean: acurate integer means, fetch_guards: human error tolerant fetch, and a sequence sum of squares tool that uses discrete calculus instead of iteration.  

use num_traits::cast::*;

fn sockball<T>(a: usize, b: T) -> (T, usize) {
    //while learning rust as a second computer language, one has to relearn some routes.  I needed to link two datasets for a sort, and the tupple reminds me of folding socks into balls.
    //couples any type T with a usize index value, useful for linking lists in rus so they can be unsorted later without declaration of custom types for each generic.
    return (b, a); //returns a unnamed tupple type, INDEX last
}

/// rrank() follows the R Language rank() convention with the result as Vec<f64>. 
/// rrank R Example: x<- c(7,2,2,5); rank(x) yields (4.,1.5,1.5,3.)    

pub fn rrank<T: PartialOrd + Clone>(vector: &[T]) -> Vec<f64> {

//Function ROADMAP:  rrank(1) Handle special cases, size 0 and size 1 vectors.  (2)  pair vector to index (3) Sort pairs (4) Crawl across data to generate rank (5) reverse sort and return original structure
//Function ROADMAP:  rrank(1) Special cases
let len = vector.len();
if len<1 {return vec![];}
if len<2 {return vec![1.];} 
let mut tups = vec![];
let mut count:usize = 0;
//Function ROADMAP:  rrank(2)  tupple generic vector to index
for each in vector {
    let pushtups = sockball(count,each.clone());
 tups.push(pushtups );
   count+=1;
} 

//Function ROADMAP:  rrank(3) SORT!
tups.sort_by(|a, b| a.partial_cmp(b).unwrap());  //now lowest to large with index attached
//why sort?  scanning once through a sort of n items should be faster than scanning n*(n-1) times even with the cost of sorting a large n, as long as you are not aiming for feedback variant, bivariate or multidimentional sorting.  
//Function ROADMAP:  rrank(4) count and rrank with a inchworm inspired rank parser 
// Function ROADMAP rrank(4A) crawler setup
let mut inch:usize =0;  // crawler's bum
let mut worm:usize=1;   // crawler's head
let mut ranks = vec![];  // Fills with sort-ordered ranks.
let mut current_rank:f64 = 1.; //current rank
let mut halves:f64 = 0.;  // ties for 1st place result in two 1.5 place metals (half gold / half silver) and third place still comes in third.   Halves accounts for the gap between 1.5 and 3.
//Function ROADMAP rrank(4B)
//inch worm, inch worm, measuring the marigolds
//inch worm toots out ranks while the head keeps moving forward and munching.  
//inch worm doesn't always assign a rank on loop pass, only when it knows the rank to assign.  
//Consider 1,1,1,1,2 -> inchworm needs to eat all the ones first before assigning a rank.  
for i in 1..len {
  if tups[i].0 != tups[i-1].0  {
  for _ in inch..worm { // loop iterates once or once for each match.
      ranks.push(current_rank);  //update prior ranks
  }
  inch=worm; // inchworm moves along and gets ready for next rank
  current_rank += 1. + halves; //  on next pass, inchworm's arithmatic will probably go far.
  halves = 0.; // the halves were used above, so halves is set back to zero.
  worm += 1;  //worm moves its front forward
  
  } else { worm+=1; //  didn't change
  current_rank += 0.5;               //half a rank for now plus
  halves += 0.5;                     //half a rank for later equals one whole rank
    //ranks don't get assigned to vector in this else
  } 
}
//Function ROADMAP rrank(4C) inchworm is done munching values but not done tooting out ranks
for _ in inch..worm {ranks.push(current_rank);}  // worm always needs a final push for last rank assignment

//Function ROADMAP:  (5)reverse sort operation with indexed tups and return ranks accordingly 
let mut outranks = vec![0f64;len]; 

let mut count:usize = 0;

for each in tups {   //put things back where there were when they were passed in.
let reorder = each.1;   
outranks[reorder] = ranks[count];
count +=1;
}

return outranks;
}

/// rank_count_greater(&borrowedboxedtea) borrows a generic vector and returns a usize vector list of the count of greater items for each item.  Large items get small ranks, small items get large ranks.  The greatest value in a list scores a zero rank, while the item with 30 superiors earns rank 30 - based on the
/// ranking system described by Wassily Hoeffding in 1947.  Note: a tie for second place means there can be no third place rank but still allows a fourth place award unless there were multiple second place times.  
/// Useful for nonparametric statistics and javelin throw competitions.   Equivalent to vector.rank() method.
pub fn rank_count_greater<T: PartialOrd + Clone>(vector: &[T]) -> Vec<usize> {
    //pass in a vector, pass out a sorted list of positions, not values A,C,B -> 2,0,1
    //FUNction ROADMAP:  (1) Deal with special cases, (2) link vector to an index with generic tuples, (3) Sort, (4) Scan across sorted vector and assign ranks, (5) Unsort and return ranks
    if vector.len() < 2 {
        //  SPECIAL CASES FIRST! 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        if vector.is_empty() {
            let temp: Vec<usize> = vec![]; //empty vector case, return an empty vector!
            return temp;
        }
        return vec![0usize]; //one element case, return zero as nothing else is larger than first element in a list of one element.
    }

    let index: Vec<usize> = (0..vector.len()).collect(); //maybe I should of called it 'range'.  Link vector to its index 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
    let mut tups = vec![]; //initialize tups, a vector that will contain generic tupple types
    for i in 0..index.len() {
        //This loop converts vector into a vector+index so that the sort order can still be reversed.  Ranking quickly takes a sort()
        tups.push(sockball(index[i], vector[i].clone())); //need to clone borrowed vector to sort, as sort normally reorders a owned list
                                                          //note:  sockball takes index and vector and returns [vector and index] as a mixed generic + known index
    }

    tups.sort_by(|a, b| a.partial_cmp(b).unwrap()); //now tups is sorted and index has become elemental position order from low to high 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 

    let mut fetchorder: Vec<usize> = vec![];
    let mut fetchclone: Vec<usize> = vec![];
    let mut ranking: Vec<usize> = vec![0]; //first or largest element must be zero

    for each in tups {
        // scan & assign ranks 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        let indey = each.1;
        let index = each.1;
        fetchorder.push(index); // pull apart tups into fetch ordering and value vector;
        fetchclone.push(indey); // I need a clone of fetchorder later in roadmap step 5 and I might just as well build it in the same loop that builds fetchorder.  Although maybe the rust compiler is smart enough do that after compile anyway?
    }

    let mut rank: usize = 0; //three preinit's before while loop.  Rank
    let mut same: usize = 0;
    let mut last: usize = fetchorder.pop().unwrap(); //Already insured vector contains two elements, so unwrap

    while !fetchorder.is_empty() {
        //as long as the list fetchorder still contains something, keep poping value assigning ranks.
        let next: usize = fetchorder.pop().unwrap(); //while loop exits when empty so ok to unwrap & does not run on None() condition
        if vector[last] > vector[next] {
            rank = rank + 1 + same; //increase rank, and if here was a run of the sames, and those sames to the rank,
            same = 0; //and wipe number of sames back to zero so as not to count anything multiple times.
            ranking.push(rank);
        // if sorted last and next values are different rank increases
        } else {
            //if sorted last and next values are the same, the count of sames increases;
            same = same + 1;

            ranking.push(rank);
        }
        last = next;
    }
    //count of greater than items now exists that may be correlated unsorted orderrust
    //fetchorder is empty
    // Now just Unsort and return ranks in original order 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

    let mut fetchorder: Vec<usize> = vec![];
    for _i in 0..vector.len() {
        // simple costs "do twice"  brush up on box
        fetchorder.push(0usize); //copy in zeros to fetorder array
    }

    for i in fetchclone {
        fetchorder[i] = ranking.pop().unwrap(); //ranking built in reverse order
    } //rebuild fetchorder with values

    return fetchorder;
}

/// vector.rank_count_lesser() borrows a generic vector and returns a usize vector list of the count of lesser items in the generic vector.  Small values score small ranks, large values score large ranks.  The smallest value in a list scores a zero rank, based on a ranking system described by Wassily Hoeffding in 1947.
///  Note: a tie for second place means there can be no third place rank but still allows a fourth place award.  Useful for nonparametric statistics, say for comparison of 5k race times... which reminds me of a joke.
///  So twin photons, a zippy electron and a massive proton all have a race.  The proton is positive he'll finish last, and of course the photon twins win because they travel light.  The ranks of such a race would likely be:  0,0,2,3 or 0,0,2,2.
pub fn rank_count_lesser<T: PartialOrd + Clone>(vector: &[T]) -> Vec<usize> {
    //pass in a vector, pass out a sorted list of positions, not values A,C,B -> 0,2,1.
    //FUNction ROADMAP:  (1) Deal with special cases, (2) link vector to an index with generic tuples, (3) Sort, (4) Scan across sorted vector and assign ranks, (5) Unsort and return ranks
    if vector.len() < 2 {
        //SPECIAL CASES FIRST:
        if vector.is_empty() {
            //EMPTY VECTOR CASE
            let temp: Vec<usize> = vec![];
            return temp;
        }
        return vec![0usize]; //ONE ITEM vector, return a zero.
    }

    let index: Vec<usize> = (0..vector.len()).collect(); //this is a range collected into a vector.
    let mut tups = vec![]; //initialize tups, a vector that will contain generic tupple types
    for i in 0..index.len() {
        //This loop converts vector into a vector+index so that the sort order can still be reversed.  Ranking quickly takes a sort().
        tups.push(sockball(index[i], vector[i].clone()));
    }

    tups.sort_by(|a, b| a.partial_cmp(b).unwrap()); //I think this is the excellent pattern defeating quicksort PDQSORT crate that was integrated into rust 1.20 and above.  MY TODO LIST: find if it actually is
    tups.reverse(); //reverse the sort order for lesser counts.  Why?  partial_cmp works with many generics, leads to default sort order that gets reversed here before ranking.
    let mut fetchorder: Vec<usize> = vec![];
    let mut fetchclone: Vec<usize> = vec![];
    let mut ranking: Vec<usize> = vec![0];

    for each in tups {
        let indey = each.1;
        let index = each.1;
        fetchorder.push(index); // pull apart tups into fetch ordering and value vector;
        fetchclone.push(indey); // I need a clone of fetchorder later and I might just as well build it in the same loop that builds fetchorder because that saves a few of the milliseconds I lost in reversing the sort for the price of spagetti code
    }

    let mut rank: usize = 0; //ranks start at zero
    let mut same: usize = 0; //count of repeated values or twins starts at zero
    let mut last: usize = fetchorder.pop().unwrap();

    while !fetchorder.is_empty() {
        //as long as the list fetchorder still contains something, keep poping value assigning ranks.
        let next: usize = fetchorder.pop().unwrap(); //while loop exits when empty so ok to unwrap & does not run on None() condition
        if vector[last] < vector[next] {
            //if diff, rank increases.  If same, rank stalls and same count increases.
            rank = rank + 1 + same; //increase rank, and if here was a run of the sames, and those sames to the rank,
            same = 0; //the number of same values multiples must be zero if last and next are not equal.
            ranking.push(rank); //assign rank to slot
        } else {
            same = same + 1; //count multiples
            ranking.push(rank);
        }
        last = next;
    }
    //count of greater than items now exists that may be correlated unsorted order
    //And fetchorder is empty, but we still have fetchclone
    //All that's left is to undo the sort so that the order is what it was

    let mut fetchorder: Vec<usize> = vec![];
    for _i in 0..vector.len() {
        fetchorder.push(0usize); //fills the vector with placeholders.  Could this be done better?
    }

    for i in fetchclone {
        fetchorder[i] = ranking.pop().unwrap(); //fill the vector with rankings in original positions
    }

    return fetchorder; //It isn't needed, but I like how explicit the return statement is, especially if the function headers have scrolled off screen
}

///rank_dense_greater(&generictea) returns dense ranks where large values get smaller ranks, and small values get larger ranks.  Dense Ranks Explained: If three javelin throwers win in a tie should they be awarded three third place metals and no first place metals, or three first places?  And would the second place and third place metals get be awarded to other competitors?  If scored with dense ranks, the answer is yes there are three first places, and yes there is a second and third place rank, no ranks get skipped.   Uneven steps between clusters can hatch data illusions, and dense ranks always have predictable steps.  That said, dense ranks do lose cluster size information.  
pub fn rank_dense_greater<T: PartialOrd + Clone>(vector: &[T]) -> Vec<usize> {
    //pass in a vector, pass out a sorted list of positions, not values A,C,B -> 2,0,1
    //FUNction ROADMAP:  (1) Deal with special cases, (2) link vector to an index with generic tuples, (3) Sort, (4) Scan across sorted vector and assign ranks, (5) Unsort and return ranks
    if vector.len() < 2 {//(1) Deal with special cases 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
        if vector.is_empty() {
            let temp: Vec<usize> = vec![];
            return temp;
        } //empty vector for empty vector
        return vec![0usize]; //return zero as nothing else is larger than first element.
    }

    let index: Vec<usize> = (0..vector.len()).collect();  //(2) Link vector to index 2 2 2 22 2 2 2 2 22 2 2 2 2 2 22 2 2 2 2 2 
    let mut tups = vec![];
    for i in 0..index.len() {
        tups.push(sockball(index[i], vector[i].clone())); //need to clone borrowed vector to sort
                                                          //note:  sockball takes index and vector and returns [vector and index]
    }

    tups.sort_by(|a, b| a.partial_cmp(b).unwrap()); //now tups is sorted and index has become elemental position order from low to high (3) Sort 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 
    let mut fetchorder: Vec<usize> = vec![];  
    let mut fetchclone: Vec<usize> = vec![];
    let mut ranking: Vec<usize> = vec![0]; //first or largest element must be zero

    for each in tups {   // (4) Scan sorted vector and assign ranks 4 4 4 4 4 4 4 4 4 44 4 4 4 4 44 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 
        let indey = each.1; //well a little housekeeping before scanning first.
        let index = each.1;
        fetchorder.push(index); //I need fetchorder, it gets consumed in the scanning while loop in step 4.
        fetchclone.push(indey); //this is my fetchorder clone for step five.  Making it in the same loop that makes fetchorder may save us a few microseconds.
    }
     let mut rank: usize = 0; //three preinit's before while loop
    let mut last: usize = fetchorder.pop().unwrap(); //Already insured vector contains two elements, so unwrap
    while !fetchorder.is_empty() {
        let next: usize = fetchorder.pop().unwrap(); //while loop exits when empty so ok to unwrap & does not run on None() condition
        if vector[last] > vector[next] {
            rank += 1;
            ranking.push(rank);
        } else {
            ranking.push(rank);
        }
        last = next;
    }
    //count of greater than items now exists that may be correlated unsorted order
    //fetchorder is empty

    let _iterator = 0usize;

    let mut fetchorder: Vec<usize> = vec![]; //(5) Unsort and return ranks 5 5 5 55 5 5 55 5 5 55 5 5 5 5 55 5 5 55 5 5 5 55 5 5 55 5 5 5 5 5 5 5 55 5 5 
    for _i in 0..vector.len() {
        fetchorder.push(0usize); //copy in zeros to fresh fetchorder array
    }

    for i in fetchclone {
        fetchorder[i] = ranking.pop().unwrap(); //ranking built in reverse order
    } //rebuild fetchorder with values

    return fetchorder;
}

///rank_dense_lesser(&generictea) returns dense ranks where large values get larger ranks, and small values get smaller ranks.  Smallest value takes a zero rank.  Dense Ranks Explained: If three racers win in a time tie should they be awarded three third place metals and no first place metals, or three first places?  And would the second place and third place metals get be awarded to other competitors?  If scored with dense ranks, the answer is yes there are three first places, and yes there is a second and third place rank, no ranks get skipped.  Uneven steps between clusters can hatch data illusions, and dense ranks always have predictable steps.  That said, dense ranks do lose cluster size information.  
pub fn rank_dense_lesser<T: PartialOrd + Clone>(vector: &[T]) -> Vec<usize> {
    //pass in a vector, pass out a sorted list of positions, not values A,C,B -> 1,3,2
    if vector.len() < 2 {
        if vector.is_empty() {
            let temp: Vec<usize> = vec![];
            return temp;
        } //empty vector for empty vector
        return vec![0usize]; //return zero as nothing else is larger than first element.
    }

    let index: Vec<usize> = (0..vector.len()).collect();
    let mut tups = vec![];
    for i in 0..index.len() {
        tups.push(sockball(index[i], vector[i].clone())); //need to clone borrowed vector to sort
                                                          //note:  sockball takes index and vector and returns generic tuple [vector and index]
    }

    tups.sort_by(|a, b| a.partial_cmp(b).unwrap());
    tups.reverse(); //now tups is sorted and index has become elemental position order
    let mut fetchorder: Vec<usize> = vec![];
    let mut fetchclone: Vec<usize> = vec![];
    let mut ranking: Vec<usize> = vec![0]; //first or largest element must be zero

    for each in tups {
        let indey = each.1;
        let index = each.1;
        fetchorder.push(index); // pull apart tups into fetch ordering and value vector;
        fetchclone.push(indey);
    }

    let mut rank: usize = 0; //three preinit's before while loop
    let mut last: usize = fetchorder.pop().unwrap(); //Already insured vector contains two elements, so unwrap

    while !fetchorder.is_empty() {
        let next: usize = fetchorder.pop().unwrap(); //while loop exits when empty so ok to unwrap & does not run on None() condition
        if vector[last] < vector[next] {
            rank += 1;
            ranking.push(rank);
        } else {
            ranking.push(rank);
        }
        last = next;
    }
    //count of greater than items now exists that may be correlated unsorted order
    //fetchorder is empty

    let _iterator = 0usize;

    let mut fetchorder: Vec<usize> = vec![];
    for _i in 0..vector.len() {
        // simple costs "do twice"  brush up on box
        fetchorder.push(0usize);
    }

    for i in fetchclone {
        fetchorder[i] = ranking.pop().unwrap(); //ranking built in reverse order
    } //rebuild fetchorder

    return fetchorder;
}

/// vector.fetch(&my_borrowed_usize_list) will return a vector of your usized picks.  Error tollerant and useful for user guided selective statistics, computation of quatriles and building custom scrolling levels out of 8 bit character bitmaps from selections that previously  caused the most player deaths  (maybe?)
/// use one of two ways:  genericvector.fetch(&vector_usize_index_picks)    or  from the function with fetch_guard(&genericvector, &your_usize_picklist)
/// #Example  
/// use frank::Fetching;  
/// fn test_fetch() {
///     let a = vec!["oh", "1", "two", "3", "four"];
///     let pick = vec![4usize, 0, 4];
///     let lost = a.fetch(&pick);
///     println!("{:?}", lost);
/// }
pub fn fetch_guard<T: Clone>(vector: &Vec<T>, picks: &Vec<usize>) -> Vec<T> {
    //a error tollerant function that gathers picks items from a vector list
    let limit = vector.len();
    let mut result = vec![];
    for each in picks {
        //I don't like the idea of checking each item to be within bounds for  production software, but I also write genetic algorithms and expect unforseen behavior.
        if *each < limit {
            result.push(vector[*each].clone())
        } else {
            eprint!("!Warn! fn fetch_guard: No item index {} in vec list of {} items.  Returning computable results!\n",*each,limit);
        }
    }
    return result;
}
/// fetch_fast has no slow branching if-then checks on index values, it just returns what it is asked to and terminates program if out of index.
pub fn fetch_fast<T: Clone>(vector: &Vec<T>, picks: &Vec<usize>) -> Vec<T> {
    // picks items from a vector list

    let mut result = vec![];
    for each in picks {
        result.push(vector[*each].clone())
    }
    return result;
}

/// frank::Fetching can be implemented for generic vectors that support Clone with 'use frank::Fetching;'   It allows vector.fetch(&your_picks_vec_usize) and vector.fetch_guard(&your_picks_vec_usize).  fetch is fast and fetch_guard is index guarded, won't terminate but does eprint!'s index errors and still returns computable results.  
pub trait Fetching<T: Clone> {
    //A new trait to apply to data
    fn fetch(&self, index: &Vec<usize>) -> Vec<T>;
    fn fetch_guard(&self, index: &Vec<usize>) -> Vec<T>; //index: &Vec<usize> is pretty specific and not very flexible or DRY.
}

impl<T> Fetching<T> for Vec<T>
//This line is high idea density. <T> may refer to the memory lifetime of the type or the Vector of type <T>
where
    T: Clone, //T needs to have a trait 'Clone' (can be copied) for FetchGuards to work.  Not all situations allow memory safely faithfully copies
{
    fn fetch(&self, index: &Vec<usize>) -> Vec<T> {
        //the method will be implemented thus:  myvector.fetch( picklist ) and return a new vector of type <T>
        fetch_fast(&self, &index) //Note it borrows the original vector.
    }
    fn fetch_guard(&self, index: &Vec<usize>) -> Vec<T> {
        //the method will be implemented thus:  myvector.fetch( picklist ) and return a new vector of type <T>
        fetch_guard(&self, &index) //Note it borrows the original vector.
    }
}

///fn imean(vector) takes generic vector primitives and calculates a every bit accurate (if not especially fast) integer mean for long vector primitives 64 bits or smaller.  Potentially useful for signal processing where the signal could be obscured by sig. digit loss in f64 values.  Rounds half and up away from zero and less than half towards zero (-9/4-> -2, 9/2 -> 4).  Not recommended for 128bit primitives unless addition overflow in 128bits can always be avoided.
pub fn imean<T: Clone + num_traits::cast::AsPrimitive<i128>>(vector: &[T]) -> T
where
    i128: num_traits::cast::AsPrimitive<T>, //where i128: std::convert::From<T>, T: std::convert::From<i128> //if a type can be converted into an i128 and back, this fn should work - that might include things like characters and bools that might not have a lot of meaning as means.
{
    let mut len = vector.len() as i128; //not especially likely to overflow for i64,u64 and smaller primitives, unpredictable overflow prone behavior for i128's and probably uninformative if you seek the meaning of this code by imean reduction into a single boolean value.
    let mut sum = 0i128;
    for each in vector {
        let convert: i128 = each.clone().as_();
        sum = sum + convert;
    }
    let mut remains: i128 = 0; //code works as intended despite "never used" warning here for Rust 2018 v 1.33-nightly.  Play that fiddle.  Keep this line.  Dancing in an iffy lifetime.
    if len == 0 {
        len = 1;
    };
    {
        remains = (sum % len) / (len / 2); //gives right last digit for integer no decimal
    }

    let longmean: i128 = remains + sum / len;
    let lmean: T = longmean.as_();
    //let mean = T::from(longmean);
    return lmean;
}

/// frank::Ranking can be used with any generic vector that implements PartialOrd and Clone, and will return rankings from "greatest = 0" to least.  just 'use frank::Ranking' for bolt on vector ranking functions like 'let myranks = myvector.rank();'  Useful for non-parametric statistics like the difference between  vec![82, 65, 78, 69, 68].rank() and vec!["r","a","n","k","ed"].rank()
pub trait Ranking<T>: Clone + PartialOrd {
    fn rank(&self) -> Vec<usize>; // same as rank_count_greater()
    fn rrank(&self) -> Vec<f64>;
    fn rank_count_lesser(&self) -> Vec<usize>;
    fn rank_dense_greater(&self) -> Vec<usize>;
    fn rank_dense_lesser(&self) -> Vec<usize>;
}

impl<T> Ranking<T> for Vec<T>
where
    T: Clone + PartialOrd,
{
    fn rank(&self) -> Vec<usize> {
        rank_count_greater(self)
    }
    fn rrank(&self) -> Vec<f64> {
        rrank(self)
    }

    fn rank_count_lesser(&self) -> Vec<usize> {
        rank_count_lesser(self)
    }
    fn rank_dense_greater(&self) -> Vec<usize> {
        rank_dense_greater(self)
    }
    fn rank_dense_lesser(&self) -> Vec<usize> {
        rank_dense_lesser(self)
    }
}

///fn seq_sum_of_squares(usize_n) uses discrete calculus to compute the sum of 1^2 + 2^2 + 3^2... n^2 --> (2n^3 + 3n^2 + n)/6 without the repetition of adding each element by way of a tiny bit of discrete calculus.  Overflow possible for large n like 3810776 for 64bit usize.
///useful for certain comparisions, or do twice and subtract for a partial sum of squares between say 23901^2 and 24824^2.  
pub fn sequence_square_sum(upto: usize) -> usize {
    //this exists mostly because I'm mildly annoyed every time I see slow brute force iteration taught in a 'for loop' example.   Discrete calculus is fast and elegant.
    let aaa = 2 * upto * upto * upto; //cubic term : 2*upto^3
    let bbb = 3 * upto * upto; //square term : 3*upto^2
    let ccc = upto; //linear term.  I think this variable rename is zero cost post compile, but need to check
    let ddd = (aaa + bbb + ccc) / 6; // (aaa+bbb+ccc)/6 is always a whole number provided 'upto' is an integer... um, right?  Yes because calculus magic.
    return ddd;
}

///fn sequence_linear_sum(usize_n) computes the total of a range from 1 to usize_n.  Overflow possible for large n, like 6074000999 with 64bit usize.
pub fn sequence_linear_sum(upto: usize) -> usize {
    if upto % 2 == 0 {
        let output: usize = upto / 2 + upto * upto / 2;
        return output;
    } else {
        let output: usize = upto / 2 + upto * upto / 2 + 1;
        return output;
    }
}

pub fn example_code() {
    println!(
        "Frank fetch and rank (and a few extras) crate.  By Dustan Doud, MIT or APPACHE2 license "
    );
    let avec = vec!['s', 'o', 'r', 't'];
    let picklist = vec![2, 1, 3, 1, 2, 0];
    let spinning = avec.fetch(&picklist);
    let _rcl = rank_count_lesser(&spinning);
    let _rdg = rank_dense_greater(&spinning);
    let _rdl = rank_dense_lesser(&spinning);
    println!(
        "vector = {:?}\npicks={:?}\nvector.fetch(picks) = {:?}\nvector.fetch(&picks).rank() = {:?}",
        avec,
        &picklist,
        &spinning,
        spinning.rank()
    );
    println!("Hoeffding style ranks are a tally of greater (or lesser) values for each position.  2,4,9 -> 2,1,0");
    println!(
        "vector.rank_count_lesser() = {:?}",
        avec.rank_count_lesser()
    );
    println!("dense ranks allow ties and don't jump values after a time, for example: 3rd place can still exist even if 2nd place was a tie)");
    println!(
        "rank_dense_greater(vector.fetch(&picks) ) =  {:?} ",
        rank_dense_greater(&avec.fetch(&picklist))
    );
    println!("nearest integer mean of picks: {}", imean(&picklist));
    println!("max: {}", u32::max_value());
    if (usize::max_value() as u64) == u64::max_value() {
        println!(
            "iteration free (calculus) sum of squares from 1 to 1.5million^2 = {}",
            sequence_square_sum(1500000)
        ); // works on 64bit, expect overflow on 16bit or 32bit platforms.
    } else {
        println!(
            "iteration free (calculus) sum of squares from 1 to 150^2 = {}",
            sequence_square_sum(150)
        );
    }
    let test = vec!(1i64,1,1,1,2,3,2,3,3,4,6,5,5,5,5,5);
    let result = rrank(&test);
    println!("{:?}\nR style ranking result in floating point values: {:?}",&test,&result);
    println!("R style rank 1,5,1,3 ->{:?}",vec![1,5,1,3].rrank() );
}