📄 permutation.cpp
字号:
/* Scalable K-means clustering softwareCopyright (C) 2000 Fredrik Farnstrom and James LewisThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.See the file README.TXT for more information.*//* permutation.cpp */#include <stdio.h>#include "common.h"#include "permutation.h"#include "random.h"// Create a new permutation, returning integers 1..num. The permutation// can be used immediately, reset() does not have to be called first.Permutation::Permutation(int num) : maxNumNumbers(num){ numbers = new int[maxNumNumbers]; randomGenerator = RandomGenerator::getDefaultRandomGenerator(); reset();}Permutation::Permutation(int num, RandomGenerator *randgen) : maxNumNumbers(num), randomGenerator(randgen){ numbers = new int[maxNumNumbers]; reset();}// Destructor.Permutation::~Permutation(){ if(numbers) delete []numbers;}// Reset permutation, i.e the next maxNumNumbers calls to// getNumber will return integers 1..maxNumNumbers in random order.void Permutation::reset(void){ int i, *p = numbers; for(i = 0; i < maxNumNumbers; i++) *p++ = i; currentNumNumbers = maxNumNumbers;}// Reset permutation and set number of numbers in the permutation.void Permutation::reset(int num){ if(numbers) delete []numbers; maxNumNumbers = num; numbers = new int[maxNumNumbers]; reset();}// Return next number from the permutation. After maxNumNumbers calls// to getNumber(), reset() will be called automatically, and a new// sequence of random number will be created.int Permutation::getNumber(void){// int n = (rand() >> 3) % currentNumNumbers, num;// int n = (randomGenerator->randomInt() >> 3) % currentNumNumbers, num;//1+(int) (10.0*rand()/(RAND_MAX+1.0))// printf("getNumber(): %d %d %d\n", maxNumNumbers, currentNumNumbers, this); unsigned long n = (unsigned long)((REAL)currentNumNumbers* randomGenerator->randomInt()/ (randomGenerator->maxRandomInt() + 1.0)); int num = numbers[n]; if(n < currentNumNumbers - 1) numbers[n] = numbers[currentNumNumbers - 1]; if(!--currentNumNumbers) reset(); return num;}/* End of file permutation.cpp */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -