摘要:p_154//5x/8define MSB_BIT (~(~0U >> 1)) int mul5div8(int val){ int sign = (val & MSB_BIT) == MSB_BIT; //取符号位 int bias = (7 + !sign) & 7; int q = val >> 3; int r = val - (q > 3; //向零取整 q = (q > 31; ...
阅读全文
摘要://此处只是获得了字符串的hash值,但是该如何散列到hash表中呢?哪个算法会好些?! 1 //在处理以字符串为键值的哈希时,times33哈希算法有着极快的计算效率和很好的哈希分布 2 3 //小写英文单词 4 #if 1 5 unsigned int str_hash(const char* key) 6 { 7 unsigned int hashval = 0; 8 while(*key) 9 {10 hash = ((hash << 5) + hash) + (unsigned int)*key++;11 }12 13 ret...
阅读全文
摘要:1 //StringHash.h 2 3 #ifndef __STRINGHASH__ 4 #define __STRINGHASH__ 5 6 #define MAXTABLELEN 1024 //默认哈希索引表大小 7 8 //哈希索引表定义 9 typedef struct _HASHTABLE10 {11 long nHashA; 12 long nHashB; 13 b...
阅读全文
摘要:1 int maxSum(int *array, int n) 2 { 3 int rvsum = array[0]; 4 int newsum = 0; 5 6 int i = 0; 7 for(i = 0; i = 0)10 newsum += array[i];11 else 12 newsum = array[i];13 14 if(rvsum maxinum)18 maxinum = sum;19 20 s...
阅读全文
摘要:除法散列法:散列函数:h(k) = k mod m;关键字为k,m是散列数组的长度m不应是2的幂,当k是一个按基数2^p解释的字符串时,选m=2^p-1可能是一个比较糟糕的选择可以选做m的值常常是与2的整数幂不太接近的质数. 乘法散列法:例子:k = 123456, p = 14, m = 2^14 = 16384,因为当s/(2^32)与(sqrt(5) - 1)/2接近时,s为2654435,故(k*s)&(~0) = 17612864,取其高14位,得散列值67
阅读全文
摘要:1 #include <iostream> 2 3 using namespace std; 4 5 struct node 6 { 7 int data; 8 node *next; 9 }; 10 11 12 node *list_create(void) 13 { 14 node *head = NULL; 15 node **curr...
阅读全文
摘要:使数组中的一个元素始终保留不用,这样当队列“满”时front和rear的值便不相同,可以和队列为空的情况区分开来。通过不允许数组完全填满,问题得以避免。若数组元素共有ARRAY_SIZE个,则有效利用的只有QUEUE_SIZE = ARRAY_SIZE - 1个。当满足下列条件时数组队列为空: (rear + 1) % ARRAY_SIZE == front当满足下列条件时数组队列为“满”: (rear + 2) % ARRAY_SIZE == front数组队列实现如下: queue.h1 /* ...
阅读全文
摘要:1 #include 2 3 void show(unsigned int value, unsigned int start, unsigned seq_end) 4 { 5 int i = 0; 6 printf("%d = %d + ", value, start); 7 for(i = 1; i = value)37 break;3...
阅读全文
摘要:看了idr的源码,思想基本同基数树一致,在网上找了几副图:图片引用自:http://blog.csdn.net/yyttiao/article/details/8164029pre_get:单层情况下的布局:在idr.layers == 1 的情况就是上面这图所示,所有的ptr都落在ary对应的地址区域...而对应得到的id值就是0到31.也就是5位..用4字节的低5位表示0到31 这32个地址对应的区域..下面再看下2层的情况从前副图中,我们总结下..在1层中,id最大值为1 << 5(32只需要5个二进制位) * 1 = 在2层中1 << 5(32只需要5个二进制位
阅读全文
摘要:Makefile:1 mytest: radix-tree.o test.o 2 gcc -Wall -fno-builtin $^ -o $@3 radix-tree.o: radix-tree.c4 gcc -c $^ -o $@5 test.c: test.o6 gcc -c $^ -o $@7 8 clean:9 rm radix-tree.o test.o myte...
阅读全文
摘要:makefile: 1 mytest:test.o multiply.o 2 gcc $^ -o $@ 3 test.o:test.c 4 gcc -I include -c $^ -o $@ 5 multiply.o:multiply.c 6 gcc -I include -c $^ -o $@ 7 8 clean: 9 rm test.o multiply.o mytestinclude/multiply.h://File : multiply.h 1 #ifndef __Jerry_Mul__ 2 #define __Jerry_Mul 3 4 extern int mul...
阅读全文
摘要:./Makefile: 1 mytest:main.o stack.o 2 gcc $^ -o $@ 3 main.o:main.c 4 gcc -I include -c $^ -o $@ 5 stack.o:stack.c 6 gcc -I include -c $^ -o $@ 7 8 clean: 9 rm main.o stack.o mytest./include/stack.h: 1 #ifndef __Jerry_stack__ 2 #define __Jerry_stack__ 3 4 typedef int stackelem; 5 typedef stackele...
阅读全文
摘要:gdb中显示数组内容:p (int [10])*a一: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define NUM 10 6 const int shuffle_initial[NUM] = {1,2,3,4,5,6,7,8,9,10}; 7 int shuffle_flags[NUM] = {0}; 8 int shuffle_final[NUM] = {0}; 9 10 int generate(void) 11 { 12 int i , k , j , l
阅读全文
摘要:1 int isqrt(unsigned x) 2 { 3 unsigned m, y, b; 4 5 m = 0x40000000; 6 y = 0; 7 while(m != 0) 8 { 9 b = y | m; 10 y = y >> 1; 11 if(x >= b) 12 { 13 x = x - b; 14 y = y | m; 15 } 16 m = m >> 2; 17 } 18 return y; 19 }
阅读全文
摘要:对于两个寄存器x和y以及掩码m,当第i位的掩码m(i)=1时,交换x和y的第i位内容,而当第i位的掩码m(i)=0时,保留x和y的第i位内容不变x = x ^ y;y = y ^ (x & m);x = x ^ y;
阅读全文
摘要:写了大概一下午,本以为很easy的,写了才知道“边界+细节”,尤其“(ones >> 2) >> ntz”,如果写成了“ones>>(2+ntz)"就会报错,边界部分自己调了。关于bitcount部分,做了修改,基本只用一个大的常数,以及一些小的数字。 4 int bitcount(unsigned int n) 5 { 6 unsigned int tmp; 7 tmp = n & 0x33333333; 8 n = n - tmp; 9 n = (n >> 2) & 0x33333333; 10 tmp = tmp
阅读全文
摘要:3 int bit_pos(unsigned int n) 4 { 5 n = n & (-n); 6 n = n - 1; 7 n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-((n>>3)&0x11111111); 8 n = (n + (n>>4))&0xf0f0f0f; 9 n = n + ((n >> 8)& 0xf) + ((n >> 16)& 0xf) + ((n >> 24)& 0
阅读全文
摘要:4 int clp2(unsigned int n) 5 { 6 --n; 7 n = n | (n >> 1); 8 n = n | (n >> 2); 9 n = n | (n >> 4); 10 n = n | (n >> 8); 11 n = n | (n >> 16); 12 return n + 1; 13 }unsigned flp2 (unsigned x){ x = x | (x >> 1) x = x | (x >> 2) x = x | (x >> 4); x = x | (x
阅读全文
摘要:4 int bitcount(unsigned int n) 5 { 6 n = n - ((n>>1)&033333333333) - ((n>>2)&011111111111); 7 n = (n + (n>>3))&030707070707; 8 n = n % 0x3f; 9 return n; 10 } 17 int bitcount(unsigned int n) 18 { 19 n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-((
阅读全文