misc.cc

来自「模拟器提供了一个简单易用的平台」· CC 代码 · 共 80 行

CC
80
字号
/************************************************** *  File: misc.cc  Author: Suman Banerjee <suman@cs.umd.edu>  Date: 15th March, 2001  Terms: GPL  Narada implementation in myns  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ***************************************************/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <randomize.h>int get_rand (int max) {//   return (rand() % max);   return (get_generic_random() % max);}/* Returns a set *count* numbers between 0 and max-1 *//* The return list is sorted in ascending order */int * get_rand_set (int count, int max) {  assert (count <= max);  bool *ret_bools = new bool [max];  for (int i = 0; i < max; i++)    ret_bools[i] = false;  for (int i = 0; i < count; i++) {    int nonconsec_final_index = get_rand(max - i);    int nonconsec_running_index = 0;    for (int j = 0; j < max; j++) {      if (ret_bools[j] == false) {        if (nonconsec_running_index == nonconsec_final_index) {          ret_bools[j] = true;          break;        }        nonconsec_running_index ++;      }    }  }  int * ret_nums = new int [count];  int x = 0;  for (int i = 0; i < max; i++) {    if (ret_bools[i] == true) {      assert (x < count);      ret_nums[x] = i;      x++;    }  }  assert (x == count);  delete [] ret_bools;  return ret_nums;}void * safe_malloc (int size) {  void *p = malloc (size);  if (p == NULL) {     perror("malloc:");     exit(-2);     return NULL;  }  return p;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?