锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鎶撶媯浜?浼間箮寮濮嬫湁涓浜?浜屽垎鏌ユ壘鎭愭儳鐥?.
涓轟簡浠ュ悗鑳藉涓嬈″皢榪欎釜鍩烘湰鐨勭畻娉曞啓瀵?鎴戝喅瀹氬啀浠旂粏鐮旂┒涓涓?鎴戜箣鍓嶆湁鍐欒繃涓涓簩鍒嗘煡鎵劇殑綆楁硶,鍦?a href="http://m.shnenglu.com/converse/archive/2009/02/28/75190.html">榪欓噷,榪欎竴嬈″啀浠ヨ繖涓棶棰樹負(fù)渚嬫潵璇存槑.
鎴戜粖鏃╁啓涓嬬殑閿欒浠g爜綾諱技浜庝笅闈㈢殑鏍峰瓙:
int search(int array[], int n, int v)
{
int left, right, middle;
left = 0, right = n;
while (left < right)
{
middle = (left + right) / 2;
if (array[middle] > v)
{
right = middle - 1;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
int main()
{
int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 13, 19};
int m = search(array, sizeof(array)/sizeof(array[0]), 1);
printf("m = %d\n", m);
return 0;
}
鍦ㄨ繖閲?寰幆鐨勫紑濮嬪,鎶婂驚鐜亶鍘嗙殑搴忓垪鍖洪棿鏄繖鏍風(fēng)殑:
while (left < right)
{
//
寰幆浣?/span>
}
浣嗘槸,鍦ㄥ驚鐜唴閮? 鍗翠笉鏄繖鏍鋒搷浣滅殑:
if (array[middle] > v)
{
right = middle - 1;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
鍥犳,榪欑閿欒鐨勫啓娉曞茍涓嶆槸鍦ㄦ墍鏈夌殑鎯呭喌涓嬮兘浼?xì)鍑洪?鏈夋椂榪樻槸鍙互鎵懼埌姝g‘鐨勭粨鏋滅殑.
榪欐槸涓縐嶅吀鍨嬬殑浜屽垎鏌ユ壘綆楁硶鍐欓敊鐨勬儏鍐?寰幆浣撴槸宸﹂棴鍙沖紑鍖洪棿,鑰屽驚鐜綋鍐呴儴鍗存槸閲囩敤宸﹂棴鍙抽棴鍖洪棿鐨勭畻娉曡繘琛屾搷浣?
涓嬮潰緇欏嚭鐨勪袱縐嶆紜殑綆楁硶,綆楁硶search鏄乏闂彸闂尯闂寸畻娉?鑰岀畻娉晄earch2鏄乏闂彸寮鍖洪棿綆楁硶,鍙互瀵規(guī)瘮涓涓嬪樊寮?
{
int left, right, middle;
left = 0, right = n - 1;
while (left <= right)
{
middle = (left + right) / 2;
if (array[middle] > v)
{
right = middle - 1;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
int search2(int array[], int n, int v)
{
int left, right, middle;
left = 0, right = n;
while (left < right)
{
middle = (left + right) / 2;
if (array[middle] > v)
{
right = middle;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
涓嬮潰鍐嶇粰鍑哄彟涓縐嶅吀鍨嬬殑閿欒鐨勪簩鍒嗘煡鎵劇畻娉?褰撴煡鎵劇殑鍏冪礌涓嶅湪搴忓垪鍐呮椂,瀹冨彲鑳介犳垚紼嬪簭鐨勬寰幆.
{
int left, right, middle;
left = 0, right = n - 1;
while (left <= right)
{
middle = (left + right) / 2;
if (array[middle] > v)
{
right = middle;
}
else if (array[middle] < v)
{
left = middle;
}
else
{
return middle;
}
}
return -1;
}
浠庡驚鐜潯浠舵潵鐪?榪欎釜綆楁硶鐨勬搷浣滃尯闂存槸宸﹂棴鍙抽棴鍖洪棿鐨?鍥犳褰揳rray[middle] > v鏃?v濡傛灉瀛樺湪鐨勮瘽搴旇鍦╗left, middle- 1]涓?鍥犳姝ゆ椂right搴旇鏄痬iddle - 1,鑰屼笉鏄痬iddle;綾諱技鐨?褰揳rray[middle] < v鏃?涓嬩竴嬈℃搷浣滅殑鍖洪棿搴旇鏄痆middle + 1, right]涓?鑰屽綋鍏冪礌涓嶅瓨鍦ㄨ繖涓簭鍒椾腑鏃?綆楁硶鍦ㄤ竴涓敊璇殑鍖洪棿涓驚鐜?浣嗘槸鍙堜笉鑳界粓姝㈠驚鐜?浜庢槸灝遍犳垚浜嗘寰幆.
鍥犳,瑕佸皢浜屽垎鏌ユ壘綆楁硶鍐欏,鍏跺疄寰堝浜洪兘澶ф鐭ラ亾鎬濇兂,鍏蜂綋鍒扮紪鐮佺殑鏃跺?灝變細(xì)琚繖浜涚湅浼煎井灝忕殑鍦版柟鎼炵硦娑?鍥犳,闇瑕佹敞鎰忚繖涓鐐?
綆楁硶鎵鎿嶄綔鐨勫尯闂?鏄乏闂彸寮鍖洪棿,榪樻槸宸﹂棴鍙抽棴鍖洪棿,榪欎釜鍖洪棿,闇瑕佸湪寰幆鍒濆鍖?寰幆浣撴槸鍚︾粓姝㈢殑鍒ゆ柇涓?浠ュ強(qiáng)姣忔淇敼left,right鍖洪棿鍊艱繖涓変釜鍦版柟淇濇寔涓鑷?鍚﹀垯灝卞彲鑳藉嚭閿?
]]>
]]>
]]>
]]>
]]>
]]>
]]>
http://m.shnenglu.com/converse/archive/2007/11/28/37430.html
鏈榪戝洜涓鴻緇?a >ccache鍔犲叆綰㈤粦鏍?wèi)鐨勬敮鎸? 鎵懼嚭鏉ユ浘緇忓疄鐜扮殑浠g爜浣滀負(fù)鍙傝? 榪欐墠鍙戠幇鍘熸潵鐨勫疄鐜伴兘鏄湁闂鐨?涔熸垜鐨勬祴璇曠敤渚嬪啓鐨勪笉濂? 浠呬粎瀵規(guī)彃鍏ユ搷浣滆繘琛屼簡嫻嬭瘯, 鎴戝悜鎵鏈夊洜涓洪槄璇諱簡榪欎喚浠g爜鑰岄犳垚鍥版儜鐨勬湅鍙嬭〃紺洪亾姝?
榪欐閲嶆柊瀹炵幇, 鎵鏈夌殑浠g爜鎺ㄥ掗噸鏂扮紪鍐? 鍙傝冧簡linux鍐呮牳涓孩榛戞爲(wèi)鐨勫疄鐜扮畻娉?/a>, 騫朵笖瀵規(guī)祴璇曠敤渚嬭繘琛屼簡鍔犲己,甯屾湜榪欐槸鏈鍚庝竴涓綰㈤粦鏍?wèi)绠楁硶鐨勪慨璁㈢増鏈?
RB-Tree鐨勬彃鍏ュ拰鍒犻櫎鎿嶄綔鐨勫疄鐜扮畻娉?br> 鍙傝冭祫鏂?
1) <<Introduction to algorithm>>
2) http://lxr.linux.no/linux/lib/rbtree.c
浣滆咃細(xì)http://m.shnenglu.com/converse/
鎮(zhèn)ㄥ彲浠ヨ嚜鐢辯殑浼犳挱錛屼慨鏀硅繖浠戒唬鐮侊紝杞澆澶勮娉ㄦ槑鍘熶綔鑰?br>
綰㈤粦鏍?wèi)鐨勫嚑涓ц川:
1) 姣忎釜緇撶偣鍙湁綰㈠拰榛戜袱縐嶉鑹?br> 2) 鏍圭粨鐐規(guī)槸榛戣壊鐨?br> 3)絀鴻妭鐐規(guī)槸榛戣壊鐨勶紙綰㈤粦鏍?wèi)涓Q屾牴鑺傜偣鐨刾arent浠ュ強(qiáng)鎵鏈夊彾鑺傜偣lchild銆乺child閮戒笉鎸囧悜NULL錛岃屾槸鎸囧悜涓涓畾涔夊ソ鐨勭┖鑺傜偣錛夈?
4) 濡傛灉涓涓粨鐐規(guī)槸綰㈣壊鐨?閭d箞瀹冪殑宸﹀彸涓や釜瀛愮粨鐐圭殑棰滆壊鏄粦鑹茬殑
5) 瀵逛簬姣忎釜緇撶偣鑰岃█,浠庤繖涓粨鐐瑰埌鍙跺瓙緇撶偣鐨勪換浣曡礬寰勪笂鐨勯粦鑹茬粨鐐?br> 鐨勬暟鐩浉鍚?br>-------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int key_t;
typedef int data_t;
typedef enum color_t
{
RED = 0,
BLACK = 1
}color_t;
typedef struct rb_node_t
{
struct rb_node_t *left, *right, *parent;
key_t key;
data_t data;
color_t color;
}rb_node_t;
/* forward declaration */
rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root);
rb_node_t* rb_search(key_t key, rb_node_t* root);
rb_node_t* rb_erase(key_t key, rb_node_t* root);
int main()
{
int i, count = 900000;
key_t key;
rb_node_t* root = NULL, *node = NULL;
srand(time(NULL));
for (i = 1; i < count; ++i)
{
key = rand() % count;
if ((root = rb_insert(key, i, root)))
{
printf("[i = %d] insert key %d success!\n", i, key);
}
else
{
printf("[i = %d] insert key %d error!\n", i, key);
exit(-1);
}
if ((node = rb_search(key, root)))
{
printf("[i = %d] search key %d success!\n", i, key);
}
else
{
printf("[i = %d] search key %d error!\n", i, key);
exit(-1);
}
if (!(i % 10))
{
if ((root = rb_erase(key, root)))
{
printf("[i = %d] erase key %d success\n", i, key);
}
else
{
printf("[i = %d] erase key %d error\n", i, key);
}
}
}
return 0;
}
static rb_node_t* rb_new_node(key_t key, data_t data)
{
rb_node_t *node = (rb_node_t*)malloc(sizeof(struct rb_node_t));
if (!node)
{
printf("malloc error!\n");
exit(-1);
}
node->key = key, node->data = data;
return node;
}
/*-----------------------------------------------------------
| node right
| / \ ==> / \
| a right node y
| / \ / \
| b y a b
-----------------------------------------------------------*/
static rb_node_t* rb_rotate_left(rb_node_t* node, rb_node_t* root)
{
rb_node_t* right = node->right;
if ((node->right = right->left))
{
right->left->parent = node;
}
right->left = node;
if ((right->parent = node->parent))
{
if (node == node->parent->right)
{
node->parent->right = right;
}
else
{
node->parent->left = right;
}
}
else
{
root = right;
}
node->parent = right;
return root;
}
/*-----------------------------------------------------------
| node left
| / \ / \
| left y ==> a node
| / \ / \
| a b b y
-----------------------------------------------------------*/
static rb_node_t* rb_rotate_right(rb_node_t* node, rb_node_t* root)
{
rb_node_t* left = node->left;
if ((node->left = left->right))
{
left->right->parent = node;
}
left->right = node;
if ((left->parent = node->parent))
{
if (node == node->parent->right)
{
node->parent->right = left;
}
else
{
node->parent->left = left;
}
}
else
{
root = left;
}
node->parent = left;
return root;
}
static rb_node_t* rb_insert_rebalance(rb_node_t *node, rb_node_t *root)
{
rb_node_t *parent, *gparent, *uncle, *tmp;
while ((parent = node->parent) && parent->color == RED)
{
gparent = parent->parent;
if (parent == gparent->left)
{
uncle = gparent->right;
if (uncle && uncle->color == RED)
{
uncle->color = BLACK;
parent->color = BLACK;
gparent->color = RED;
node = gparent;
}
else
{
if (parent->right == node)
{
root = rb_rotate_left(parent, root);
tmp = parent;
parent = node;
node = tmp;
}
parent->color = BLACK;
gparent->color = RED;
root = rb_rotate_right(gparent, root);
}
}
else
{
uncle = gparent->left;
if (uncle && uncle->color == RED)
{
uncle->color = BLACK;
parent->color = BLACK;
gparent->color = RED;
node = gparent;
}
else
{
if (parent->left == node)
{
root = rb_rotate_right(parent, root);
tmp = parent;
parent = node;
node = tmp;
}
parent->color = BLACK;
gparent->color = RED;
root = rb_rotate_left(gparent, root);
}
}
}
root->color = BLACK;
return root;
}
static rb_node_t* rb_erase_rebalance(rb_node_t *node, rb_node_t *parent, rb_node_t *root)
{
rb_node_t *other, *o_left, *o_right;
while ((!node || node->color == BLACK) && node != root)
{
if (parent->left == node)
{
other = parent->right;
if (other->color == RED)
{
other->color = BLACK;
parent->color = RED;
root = rb_rotate_left(parent, root);
other = parent->right;
}
if ((!other->left || other->left->color == BLACK) &&
(!other->right || other->right->color == BLACK))
{
other->color = RED;
node = parent;
parent = node->parent;
}
else
{
if (!other->right || other->right->color == BLACK)
{
if ((o_left = other->left))
{
o_left->color = BLACK;
}
other->color = RED;
root = rb_rotate_right(other, root);
other = parent->right;
}
other->color = parent->color;
parent->color = BLACK;
if (other->right)
{
other->right->color = BLACK;
}
root = rb_rotate_left(parent, root);
node = root;
break;
}
}
else
{
other = parent->left;
if (other->color == RED)
{
other->color = BLACK;
parent->color = RED;
root = rb_rotate_right(parent, root);
other = parent->left;
}
if ((!other->left || other->left->color == BLACK) &&
(!other->right || other->right->color == BLACK))
{
other->color = RED;
node = parent;
parent = node->parent;
}
else
{
if (!other->left || other->left->color == BLACK)
{
if ((o_right = other->right))
{
o_right->color = BLACK;
}
other->color = RED;
root = rb_rotate_left(other, root);
other = parent->left;
}
other->color = parent->color;
parent->color = BLACK;
if (other->left)
{
other->left->color = BLACK;
}
root = rb_rotate_right(parent, root);
node = root;
break;
}
}
}
if (node)
{
node->color = BLACK;
}
return root;
}
static rb_node_t* rb_search_auxiliary(key_t key, rb_node_t* root, rb_node_t** save)
{
rb_node_t *node = root, *parent = NULL;
int ret;
while (node)
{
parent = node;
ret = node->key - key;
if (0 < ret)
{
node = node->left;
}
else if (0 > ret)
{
node = node->right;
}
else
{
return node;
}
}
if (save)
{
*save = parent;
}
return NULL;
}
rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root)
{
rb_node_t *parent = NULL, *node;
parent = NULL;
if ((node = rb_search_auxiliary(key, root, &parent)))
{
return root;
}
node = rb_new_node(key, data);
node->parent = parent;
node->left = node->right = NULL;
node->color = RED;
if (parent)
{
if (parent->key > key)
{
parent->left = node;
}
else
{
parent->right = node;
}
}
else
{
root = node;
}
return rb_insert_rebalance(node, root);
}
rb_node_t* rb_search(key_t key, rb_node_t* root)
{
return rb_search_auxiliary(key, root, NULL);
}
rb_node_t* rb_erase(key_t key, rb_node_t *root)
{
rb_node_t *child, *parent, *old, *left, *node;
color_t color;
if (!(node = rb_search_auxiliary(key, root, NULL)))
{
printf("key %d is not exist!\n");
return root;
}
old = node;
if (node->left && node->right)
{
node = node->right;
while ((left = node->left) != NULL)
{
node = left;
}
child = node->right;
parent = node->parent;
color = node->color;
if (child)
{
child->parent = parent;
}
if (parent)
{
if (parent->left == node)
{
parent->left = child;
}
else
{
parent->right = child;
}
}
else
{
root = child;
}
if (node->parent == old)
{
parent = node;
}
node->parent = old->parent;
node->color = old->color;
node->right = old->right;
node->left = old->left;
if (old->parent)
{
if (old->parent->left == old)
{
old->parent->left = node;
}
else
{
old->parent->right = node;
}
}
else
{
root = node;
}
old->left->parent = node;
if (old->right)
{
old->right->parent = node;
}
}
else
{
if (!node->left)
{
child = node->right;
}
else if (!node->right)
{
child = node->left;
}
parent = node->parent;
color = node->color;
if (child)
{
child->parent = parent;
}
if (parent)
{
if (parent->left == node)
{
parent->left = child;
}
else
{
parent->right = child;
}
}
else
{
root = child;
}
}
free(old);
if (color == BLACK)
{
root = rb_erase_rebalance(child, parent, root);
}
return root;
}
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
typedef struct {
unsigned int size; /* sizes of items */
unsigned int perslab; /* how many items per slab */
void **slots; /* list of item ptrs */
unsigned int sl_total; /* size of previous array */
unsigned int sl_curr; /* first free slot */
void *end_page_ptr; /* pointer to next free item at end of page, or 0 */
unsigned int end_page_free; /* number of items remaining at end of last alloced page */
unsigned int slabs; /* how many slabs were allocated for this class */
void **slab_list; /* array of slab pointers */
unsigned int list_size; /* size of prev array */
unsigned int killing; /* index+1 of dying slab, or zero if none */
} slabclass_t;
紼嬪簭涓湁涓涓叏灞鐨勬暟緇?br>static slabclass_t slabclass[POWER_LARGEST + 1]鐢ㄤ簬淇濆瓨slab,棰勫垎閰嶅唴瀛樻睜鏃惰皟鐢ㄧ殑鏄痸oid slabs_init(const size_t limit, const double factor) 鍑芥暟,鍏朵腑limit鏄唴瀛樻睜鐨勬渶澶у閲?factor鏄垎閰嶆椂鐨勫闀垮洜瀛?
姣旀柟璇?鍔犲叆factor鏄?,絎竴涓湪slabclass鏁扮粍涓殑slab鐨勬瘡涓猧tem澶у皬鏄?28瀛楄妭,閭d箞涓嬩竴涓猻lab姣忎釜item鐨勫ぇ灝忓氨鏄?28*2,鍐嶄笅涓涓氨鏄?28*2*2(娉ㄦ剰,涓轟簡綆鍖栭棶棰樼殑璇存槑,涓婇潰娌℃湁鑰冭檻鍦板潃瀵歸綈鐨勫洜绱?.
鍦ㄩ鍒嗛厤鍐呭瓨姹犳椂,鏈澶氱粰姣忎釜slab淇濆瓨item鐨勫閲忔槸1M鍐呭瓨,榪欎釜鏁板肩敱#define POWER_BLOCK 1048576鍐沖畾.
鍥犳,slab涓殑鍑犱釜鍏冪礌鍦ㄩ鍒嗛厤鍐呭瓨鏃舵槸榪欎箞瀹氱殑:
size鏈変竴涓搗濮嬪?榪欎釜鍊間互鍚庣殑澧為暱鐢眆actor鍐沖畾,澧為暱鐨勮繃紼嬪墠闈㈠凡緇忛槓榪拌繃浜?
perslab淇濆瓨鐨勬槸涓涓猻lab瀛樻斁鐨刬tem鏁伴噺,鍥犳perslab = POWER_BLOCK / slabclass[i].size;
濡傛灉棰勫厛鍒嗛厤涓孌靛唴瀛樹緵浣跨敤鐨勮瘽,涔熷氨鏄病鏈夊畾涔塂ONT_PREALLOC_SLABS瀹?閭d箞灝辮皟鐢╯labs_preallocate榪涜棰勫垎閰嶅唴瀛?
鍏朵腑,end_page_ptr鎸囧悜榪欎釜棰勫垎閰嶅ソ鐨勬寚閽?end_page_free琛ㄧず鐨勬槸鐩墠絀洪棽鍙敤item鐨勬暟閲?鍦ㄩ鍒嗛厤鏃?榪欎釜鍊間笌perslab鐩稿悓.
鍦ㄨ繖涓唴瀛樻睜妯″瀷涓?姣忎釜page瀹為檯涓婃槸涓涓暟緇?鏁扮粍涓瘡涓厓绱犵殑澶у皬灝辨槸榪欎釜slab涓璱tem鐨勫ぇ灝?
鍙﹀,slots淇濆瓨鐨勬槸閲婃斁鍑烘潵鐨刬tem鎸囬拡,sl_total琛ㄧず鎬葷殑鏁伴噺,sl_curr琛ㄧず鐨勬槸鐩墠鍙敤鐨勫凡緇忛噴鏀懼嚭鏉ョ殑item鏁伴噺.
姣忎竴嬈¤鍒嗛厤鍐呭瓨鐨勬椂鍊?棣栧厛鏍規(guī)嵁闇瑕佸垎閰嶇殑鍐呭瓨澶у皬鍦╯labclass鏁扮粍涓煡鎵劇儲寮曟渶灝忕殑涓涓ぇ浜庢墍瑕佹眰鍐呭瓨鐨剆lab,濡傛灉slots涓嶄負(fù)絀?閭d箞灝變粠榪欓噷榪斿洖鍐呭瓨,鍚﹀垯鍘繪煡鎵緀nd_page_ptr,濡傛灉涔熸病鏈?閭d箞灝卞彧鑳借繑鍥濶ULL浜?
姣忎竴嬈¢噴鏀懼唴瀛樼殑鏃跺?鍚屾牱鐨勬壘鍒板簲璇ヨ繑鍥炲唴瀛樼殑slab鍏冪礌,鏀瑰啓鍓嶉潰鎻愬埌鐨剆lot鎸囬拡鍜宻l_curr鏁?
鏈夌偣浠撲績,浠ュ悗鍐嶅畬鍠剘~
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>
]]>