示例#1
0
文件: random.c 项目: alexmiao/moosefs
uint32_t rndu32_ranged(uint32_t range) {
	uint32_t max,r;
	r = rndu32();
	if (range==0) {
		return r;
	}
	max = -(((uint32_t)-range) % range);
	if (max) {
		while (r>=max) {
			r = rndu32();
		}
	}
	return r%range;
}
示例#2
0
static inline uint8_t tree_insert(treap_node **node,hash_key_t key,void *val) {
	treap_node *n = (*node);
	if (n==NULL) {
		n = malloc(sizeof(treap_node));
		passert(n);
		n->left = NULL;
		n->right = NULL;
		n->key = key;
		n->val = val;
		n->pri = rndu32();
		*node = n;
		return 1;
	} else {
		if (key < n->key) {
			if (tree_insert(&(n->left),key,val)) {
				if (n->left->pri < n->pri) {
					tree_rotate_with_left_child(node);
				}
				return 1;
			}
		} else if (key > n->key) {
			if (tree_insert(&(n->right),key,val)) {
				if (n->right->pri < n->pri) {
					tree_rotate_with_right_child(node);
				}
				return 1;
			}
		}
	}
	return 0;
}