Warning, /sdk/rust-qt-binding-generator/demo/rust/src/implementation/fibonacci.rs is written in an unsupported language. File is not indexed.
0001 // Copyright 2017 Jos van den Oever <jos@vandenoever.info> 0002 // 0003 // This program is free software; you can redistribute it and/or 0004 // modify it under the terms of the GNU General Public License as 0005 // published by the Free Software Foundation; either version 2 of 0006 // the License or (at your option) version 3 or any later version 0007 // accepted by the membership of KDE e.V. (or its successor approved 0008 // by the membership of KDE e.V.), which shall act as a proxy 0009 // defined in Section 14 of version 3 of the license. 0010 // 0011 // This program is distributed in the hope that it will be useful, 0012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 // GNU General Public License for more details. 0015 // 0016 // You should have received a copy of the GNU General Public License 0017 // along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 0019 use std::thread; 0020 use interface::*; 0021 use std::sync::atomic::AtomicUsize; 0022 use std::sync::atomic::Ordering; 0023 use std::sync::Arc; 0024 0025 fn fibonacci(input: u32) -> usize { 0026 if input <= 1 { 0027 return input as usize; 0028 } 0029 let mut i = 0; 0030 let mut sum = 0; 0031 let mut last = 0; 0032 let mut cur = 1; 0033 while i < input - 1 { 0034 sum = last + cur; 0035 last = cur; 0036 cur = sum; 0037 i += 1; 0038 } 0039 sum 0040 } 0041 0042 pub struct Fibonacci { 0043 emit: FibonacciEmitter, 0044 input: u32, 0045 result: Arc<AtomicUsize>, 0046 } 0047 0048 impl FibonacciTrait for Fibonacci { 0049 fn new(emit: FibonacciEmitter) -> Fibonacci { 0050 Fibonacci { 0051 emit, 0052 input: 0, 0053 result: Arc::new(AtomicUsize::new(0)), 0054 } 0055 } 0056 fn emit(&mut self) -> &mut FibonacciEmitter { 0057 &mut self.emit 0058 } 0059 fn input(&self) -> u32 { 0060 self.input 0061 } 0062 fn set_input(&mut self, value: u32) { 0063 self.input = value; 0064 self.emit.input_changed(); 0065 let mut emit = self.emit.clone(); 0066 let result = self.result.clone(); 0067 result.swap(0, Ordering::SeqCst); 0068 emit.result_changed(); 0069 thread::spawn(move || { 0070 let r = fibonacci(value); 0071 result.swap(r, Ordering::SeqCst); 0072 emit.result_changed(); 0073 }); 0074 } 0075 fn result(&self) -> u64 { 0076 self.result.fetch_add(0, Ordering::SeqCst) as u64 0077 } 0078 } 0079 0080 pub struct FibonacciList { 0081 emit: FibonacciListEmitter, 0082 } 0083 0084 impl FibonacciListTrait for FibonacciList { 0085 fn new(emit: FibonacciListEmitter, _: FibonacciListList) -> FibonacciList { 0086 FibonacciList { 0087 emit, 0088 } 0089 } 0090 fn emit(&mut self) -> &mut FibonacciListEmitter { 0091 &mut self.emit 0092 } 0093 fn row_count(&self) -> usize { 0094 93 0095 } 0096 fn row(&self, row: usize) -> u64 { 0097 row as u64 + 1 0098 } 0099 fn fibonacci_number(&self, row: usize) -> u64 { 0100 fibonacci(row as u32 + 1) as u64 0101 } 0102 }