亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
樓主: Godbach
打印 上一主題 下一主題

Linux內(nèi)核中的紅黑樹 [復(fù)制鏈接]

論壇徽章:
36
IT運維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
41 [報告]
發(fā)表于 2009-01-04 10:06 |只看該作者
原帖由 lbaby 于 2009-1-4 10:05 發(fā)表
http://lxr.linux.no/linux+v2.6.28/include/linux/hash.h

另外內(nèi)核里也有不錯的 hash 實現(xiàn),Godbach 兄可以去看看


多謝lbaby兄指點啊。。

論壇徽章:
0
42 [報告]
發(fā)表于 2009-01-04 10:21 |只看該作者
好文章,收藏。

論壇徽章:
36
IT運維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
43 [報告]
發(fā)表于 2009-01-05 22:28 |只看該作者
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
                                struct rb_node ** rb_link)
node為當(dāng)前要插入的節(jié)點;
parent為node的父節(jié)點;
rb_link為node節(jié)點要插入的位置,即parent->left 或parent->right。這個可有前面在查找過程中確定。


如果rb_link_node如我解釋的一樣,那么也就是調(diào)用該函數(shù)的時候應(yīng)該保證tree至少有一個節(jié)點,也就是根節(jié)點存在。不然的話,parent本身就為空,無法執(zhí)行l(wèi)ink

論壇徽章:
0
44 [報告]
發(fā)表于 2009-01-11 00:35 |只看該作者
看了,但是“知其然而不知其所以然”,版主大哥,。。

論壇徽章:
36
IT運維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
45 [報告]
發(fā)表于 2009-01-11 12:31 |只看該作者
原帖由 maxxfire 于 2009-1-11 00:35 發(fā)表
看了,但是“知其然而不知其所以然”,版主大哥,。。


其實這里講的基本上就是《算法導(dǎo)論》中講的紅黑樹的實現(xiàn)啊。如果不明白這個,是不是本身對紅黑樹不太了解。

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
46 [報告]
發(fā)表于 2009-01-11 20:31 |只看該作者

回復(fù) #45 Godbach 的帖子

算法導(dǎo)論的中文版,紅黑樹那一章講得有點云里霧里的,呵呵。可能英文版講得好一點吧

論壇徽章:
36
IT運維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
47 [報告]
發(fā)表于 2009-01-11 21:46 |只看該作者
原帖由 dreamice 于 2009-1-11 20:31 發(fā)表
算法導(dǎo)論的中文版,紅黑樹那一章講得有點云里霧里的,呵呵。可能英文版講得好一點吧


呵呵,insert部分大致明白了。erase比較復(fù)雜。

論壇徽章:
0
48 [報告]
發(fā)表于 2009-01-16 22:49 |只看該作者

論壇徽章:
1
CU十二周年紀(jì)念徽章
日期:2013-10-24 15:41:34
49 [報告]
發(fā)表于 2009-01-17 19:41 |只看該作者
很好,頂一下,學(xué)習(xí)!!

論壇徽章:
0
50 [報告]
發(fā)表于 2009-01-17 22:58 |只看該作者
這兩天在看內(nèi)核中的文檔,里面也有很詳細(xì)的講解。
Documentation/rbtree.txt

如下:

Red-black Trees (rbtree) in Linux
January 18, 2007
Rob Landley <rob@landley.net>
=============================

What are red-black trees, and what are they for?
------------------------------------------------

Red-black trees are a type of self-balancing binary search tree, used for
storing sortable key/value data pairs.  This differs from radix trees (which
are used to efficiently store sparse arrays and thus use long integer indexes
to insert/access/delete nodes) and hash tables (which are not kept sorted to
be easily traversed in order, and must be tuned for a specific size and
hash function where rbtrees scale gracefully storing arbitrary keys).

Red-black trees are similar to AVL trees, but provide faster real-time bounded
worst case performance for insertion and deletion (at most two rotations and
three rotations, respectively, to balance the tree), with slightly slower
(but still O(log n)) lookup time.

To quote Linux Weekly News:

    There are a number of red-black trees in use in the kernel.
    The anticipatory, deadline, and CFQ I/O schedulers all employ
    rbtrees to track requests; the packet CD/DVD driver does the same.
    The high-resolution timer code uses an rbtree to organize outstanding
    timer requests.  The ext3 filesystem tracks directory entries in a
    red-black tree.  Virtual memory areas (VMAs) are tracked with red-black
    trees, as are epoll file descriptors, cryptographic keys, and network
    packets in the "hierarchical token bucket" scheduler.

This document covers use of the Linux rbtree implementation.  For more
information on the nature and implementation of Red Black Trees,  see:

  Linux Weekly News article on red-black trees
    http://lwn.net/Articles/184495/

  Wikipedia entry on red-black trees
    http://en.wikipedia.org/wiki/Red-black_tree

Linux implementation of red-black trees
---------------------------------------

Linux's rbtree implementation lives in the file "lib/rbtree.c".  To use it,
"#include <linux/rbtree.h>".

The Linux rbtree implementation is optimized for speed, and thus has one
less layer of indirection (and better cache locality) than more traditional
tree implementations.  Instead of using pointers to separate rb_node and data
structures, each instance of struct rb_node is embedded in the data structure
it organizes.  And instead of using a comparison callback function pointer,
users are expected to write their own tree search and insert functions
which call the provided rbtree functions.  Locking is also left up to the
user of the rbtree code.

Creating a new rbtree
---------------------

Data nodes in an rbtree tree are structures containing a struct rb_node member:

  1.   struct mytype {
  2.           struct rb_node node;
  3.           char *keystring;
  4.   };
復(fù)制代碼

When dealing with a pointer to the embedded struct rb_node, the containing data
structure may be accessed with the standard container_of() macro.  In addition,
individual members may be accessed directly via rb_entry(node, type, member).

At the root of each rbtree is an rb_root structure, which is initialized to be
empty via:

  1.   struct rb_root mytree = RB_ROOT;
復(fù)制代碼

Searching for a value in an rbtree
----------------------------------

Writing a search function for your tree is fairly straightforward: start at the
root, compare each value, and follow the left or right branch as necessary.

Example:

  1.   struct mytype *my_search(struct rb_root *root, char *string)
  2.   {
  3.           struct rb_node *node = root->rb_node;

  4.           while (node) {
  5.                   struct mytype *data = container_of(node, struct mytype, node);
  6.                 int result;

  7.                 result = strcmp(string, data->keystring);

  8.                 if (result < 0)
  9.                           node = node->rb_left;
  10.                 else if (result > 0)
  11.                           node = node->rb_right;
  12.                 else
  13.                           return data;
  14.         }
  15.         return NULL;
  16.   }
復(fù)制代碼

Inserting data into an rbtree
-----------------------------

Inserting data in the tree involves first searching for the place to insert the
new node, then inserting the node and rebalancing ("recoloring") the tree.

The search for insertion differs from the previous search by finding the
location of the pointer on which to graft the new node.  The new node also
needs a link to its parent node for rebalancing purposes.

Example:

  1.   int my_insert(struct rb_root *root, struct mytype *data)
  2.   {
  3.           struct rb_node **new = &(root->rb_node), *parent = NULL;

  4.           /* Figure out where to put new node */
  5.           while (*new) {
  6.                   struct mytype *this = container_of(*new, struct mytype, node);
  7.                   int result = strcmp(data->keystring, this->keystring);

  8.                 parent = *new;
  9.                   if (result < 0)
  10.                           new = &((*new)->rb_left);
  11.                   else if (result > 0)
  12.                           new = &((*new)->rb_right);
  13.                   else
  14.                           return FALSE;
  15.           }

  16.           /* Add new node and rebalance tree. */
  17.           rb_link_node(data->node, parent, new);
  18.           rb_insert_color(data->node, root);

  19.         return TRUE;
  20.   }
復(fù)制代碼

Removing or replacing existing data in an rbtree
------------------------------------------------

To remove an existing node from a tree, call:

  1.   void rb_erase(struct rb_node *victim, struct rb_root *tree);
復(fù)制代碼

Example:

  1.   struct mytype *data = mysearch(mytree, "walrus");

  2.   if (data) {
  3.           rb_erase(data->node, mytree);
  4.           myfree(data);
  5.   }
復(fù)制代碼

To replace an existing node in a tree with a new one with the same key, call:

  1.   void rb_replace_node(struct rb_node *old, struct rb_node *new,
  2.                           struct rb_root *tree);
復(fù)制代碼

Replacing a node this way does not re-sort the tree: If the new node doesn't
have the same key as the old node, the rbtree will probably become corrupted.

Iterating through the elements stored in an rbtree (in sort order)
------------------------------------------------------------------

Four functions are provided for iterating through an rbtree's contents in
sorted order.  These work on arbitrary trees, and should not need to be
modified or wrapped (except for locking purposes):

  1.   struct rb_node *rb_first(struct rb_root *tree);
  2.   struct rb_node *rb_last(struct rb_root *tree);
  3.   struct rb_node *rb_next(struct rb_node *node);
  4.   struct rb_node *rb_prev(struct rb_node *node);
復(fù)制代碼

To start iterating, call rb_first() or rb_last() with a pointer to the root
of the tree, which will return a pointer to the node structure contained in
the first or last element in the tree.  To continue, fetch the next or previous
node by calling rb_next() or rb_prev() on the current node.  This will return
NULL when there are no more nodes left.

The iterator functions return a pointer to the embedded struct rb_node, from
which the containing data structure may be accessed with the container_of()
macro, and individual members may be accessed directly via
rb_entry(node, type, member).

Example:

  1.   struct rb_node *node;
  2.   for (node = rb_first(&mytree); node; node = rb_next(node))
  3.           printk("key=%s\n", rb_entry(node, int, keystring));

復(fù)制代碼

[ 本帖最后由 scutan 于 2009-1-17 23:06 編輯 ]
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP