lists1
Full source code
C++
// -*- mode: c++ -*-
// $Id$
// http://www.bagley.org/~doug/shootout/
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <numeric>
template <class _ForwardIterator, class _Tp>
void
iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
{
while (__first != __last)
*__first++ = __value++;
}
using namespace std;
void list_print_n (list<int> L, int n) {
int c, lastc = n - 1;
list<int>::iterator i;
for (c = 0, i = L.begin(); i != L.end() && c < n; ++i, ++c) {
cout << (*i);
if (c < lastc) cout << " ";
}
cout << endl;
}
int main(int argc, char* argv[]) {
#ifdef SMALL_PROBLEM_SIZE
#define LENGTH 100000
#else
#define LENGTH 1000000
#endif
int N = (argc == 2 ? (atoi(argv[1]) < 1 ? 1 : atoi(argv[1])): LENGTH);
list<int>::iterator i;
// create empty list B
list<int> B;
// create list (A) of integers from 1 through N
list<int> A(N);
::iota(A.begin(), A.end(), 1);
// move each individual item from A to B, in a loop, reversing order
while (! A.empty()) {
B.push_front(A.front());
A.pop_front();
}
// print first 2 elements of B
list_print_n(B, 2);
// reverse B (can be done in place)
B.reverse();
// reverse(B.begin(), B.end());
// is 0 a member of B?
cout << ((find(B.begin(), B.end(), 0) == B.end()) ? "false" : "true") << endl;
// is N a member of B?
cout << ((find(B.begin(), B.end(), N) == B.end()) ? "false" : "true") << endl;
// filter values from B to A that are less than N/2, preserving order
int mid = N/2;
for (i = B.begin(); i != B.end(); ++i) {
if ((*i) < mid) A.push_back(*i);
}
// print first ten items of A
list_print_n(A, 10);
// print sum of items in A that are less than 1000
int sum = 0;
for (i = A.begin(); i != A.end(); ++i) {
if ((*i) < 1000) sum += (*i);
}
cout << sum << endl;
// append B to end of A
A.splice(A.end(), B);
// print length and last element of A
cout << A.size() << " " << A.back() << endl;
}
Rust
// Adapted from https://github.com/llvm/llvm-test-suite and // http://www.bagley.org/~doug/shootout/ use std::env; use std::collections::LinkedList; use std::collections::linked_list::IterMut; #[cfg(feature = "small_problem_size")] const LENGTH: i32 = 100000; #[cfg(not(feature = "small_problem_size"))] const LENGTH: i32 = 1000000; fn iota(iter: IterMut<'_, i32>, v: i32) { let mut i = 0; for e in iter { *e = i + v; i += 1; } } fn list_print_n(list: &LinkedList<i32>, n: i32) { let lastc = n - 1; let mut c = 0; for elem in list.iter() { if c >= n { break; } print!("{}", elem); if c < lastc { print!(" "); } c += 1; } println!(); } fn list_reverse<T>(list: LinkedList<T>) -> LinkedList<T> { let mut reversed = LinkedList::new(); for elem in list { reversed.push_front(elem); } return reversed } fn main() { let mut args = env::args(); let n = if args.len() == 2 { if args.nth(1).unwrap().parse::<i32>().unwrap() < 1 { 1 } else { args.nth(1).unwrap().parse::<i32>().unwrap() } } else { LENGTH }; let mut b = LinkedList::<i32>::new(); let mut a = LinkedList::<i32>::new(); for _i in 0..n { a.push_back(Default::default()); } iota(a.iter_mut(), 1); while a.len() > 0 { b.push_front(a.pop_front().unwrap()); } list_print_n(&b, 2); b = list_reverse(b); println!("{}", if !b.contains(&0) { "false" } else { "true" }); println!("{}", if !b.contains(&n) { "false" } else { "true" }); let mid = n / 2; for elem in b.iter() { if *elem < mid { a.push_back(*elem); } } list_print_n(&a, 10); let mut sum = 0; for elem in b.iter() { if *elem < 1000 { sum += *elem; } } println!("{}", sum); a.append(&mut b); println!("{} {}", a.len(), *a.back().unwrap()); }
Porting notes
Many parts are shared with lists with some differences.
There are no particular notes.