当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 8 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 0:35 
头像

注册: 2009-04-03 20:52
帖子: 101
地址: Guangzhou -China
送出感谢: 0 次
接收感谢: 0 次
虽然我用ubuntu不到半年,但一用上,我就不可以再离开ubuntu了……
想为社区做点贡献,只是能力还有限,力不从心。我订阅了ubuntu官网英文论坛,查看到有用的贴,就拿来这与大家分享吧,并作力所能及的翻译,只作意译哦,大家一定要支持哦!
ubuntu forums:http://ubuntuforums.org/,让我们与世界同步,有兴趣的跟我一起来!

带头贴先跟大家分享一副图表,这副图从整体上解释了自由软件的概念(Free Software Concept Map),作者表示,他作这图采取的意见是:把零散的概念都集合到一个整体概念图里去将简化很多问题(Less concepts in one concept map makes it easier.)。官网链接是:http://bulma.net/body.phtml?nIdNoticia=2277
附件:
map-en-i-800.png
map-en-i-800.png [ 257.8 KiB | 被浏览 1700 次 ]


早期版本:
附件:
MapangKonseptualngLibrengSoftware.png
MapangKonseptualngLibrengSoftware.png [ 278.63 KiB | 被浏览 1700 次 ]



两幅图以GPL协议发布,原帖链接是:http://ubuntuforums.org/showthread.php?t=1221769


_________________
There should be one-- and preferably only one --obvious way to do it.


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 1:09 

注册: 2005-11-09 19:48
帖子: 219
送出感谢: 0 次
接收感谢: 0 次
强烈支持! :em11 看你能坚持多久。 :em04


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 1:16 
头像

注册: 2009-04-03 20:52
帖子: 101
地址: Guangzhou -China
送出感谢: 0 次
接收感谢: 0 次
其实看了看,很多的那论坛上的东西,能有价值的,没有多少,就当转载下有价值的,有意思的,可能很久才更新,不过会坚持的 :em11


_________________
There should be one-- and preferably only one --obvious way to do it.


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 2:31 
头像

注册: 2009-04-03 20:52
帖子: 101
地址: Guangzhou -China
送出感谢: 0 次
接收感谢: 0 次
免翻译吧,偷懒……
nice hashmap for C
从 Ubuntu Forums 作者:manualqr
Every time I want to do a little bit of hobby programming, I consider what I want to get done and which language to do it in. It used to be that C almost never got picked because there was no way to use <feature foo>.

One of those features were hashmaps. C++ offers map and unordered_map from the STL, and there are many great libraries (e.g google's sparse-hash) that work well.

I found a couple C hashmap implementations online but most of them were limited in some way, or just not to my liking. So I wrote my own, and I want to share it..

Features:
* maps keys to values :). e.g char* to double, long to int, etc.
* can grow (i.e not limited to n values)
* uses simple bools to indicate errors
* insert and find functions
* option to make the hashmap thread safe
* option to use a destructor when deleting the hashmap
* option to build a hash functions (for integer keys)
* uses open-addressing to resolve collisions (and rehashes before the load degrades performance to the level of closed-addressing)
* (relatively) friendly interface: macros are used to handle casting for you

API:
* hashmap mk_hmap(hash function, equality function, optional destructor)
* bool hmap_add(hashmap, key, value)
* val hmap_get(hashmap, key)

I've done some basic tests with this and it seems to work well.. I don't have a reliable benchmark, so I won't post numbers.. But keep in mind, the hashmap implementation isn't normally a bottleneck - it's usually the hash function.

Code:
代码:
/*
 * hashmap.h
 * Copyright (c) 2009 Vedant Kumar
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#pragma once

#define HMAP_PRESET_SIZE        2 << 6 // use a power of 2 for faster array access
#define HMAP_GROWTH_RATE        2
#define HMAP_MAKE_HASHFN
#define HMAP_THREAD_SAFE        // build with -lrt
#define HMAP_DESTRUCTORS

#ifdef HMAP_THREAD_SAFE
        #include <semaphore.h>
#endif

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef void* key;
typedef void* val;

typedef struct key_val_map {
        key k;
        val v;
} key_val_map;

typedef struct hashmap {
        key_val_map *map;
        uint32_t size;
        uint32_t capacity;
        uint32_t (*hash_fn)(key);
        bool (*eq_fn)(key, key);
#ifdef HMAP_DESTRUCTORS
        void (*del_fn)(val);
#endif
#ifdef HMAP_THREAD_SAFE
        sem_t lock;
#endif
} hashmap;

// hashmaps need a hash function, an equality function, and a destructor
hashmap mk_hmap(uint32_t (*hash_fn)(key),
                                bool (*eq_fn)(key, key)
                        #ifdef HMAP_DESTRUCTORS
                                , void (*del_fn)(val)
                        #endif
                                ) {
                                       
        hashmap hmap;
        hmap.map = (key_val_map*) malloc(sizeof(key_val_map) * HMAP_PRESET_SIZE);
        hmap.size = 0;
        hmap.capacity = HMAP_PRESET_SIZE;
        hmap.hash_fn = hash_fn;
        hmap.eq_fn = eq_fn;
#ifdef HMAP_DESTRUCTORS
        hmap.del_fn = del_fn;
#endif
#ifdef HMAP_THREAD_SAFE
        sem_init(&hmap.lock, 0, 1);
#endif
        return hmap;
}

void free_hmap(hashmap* hmap) {
#ifdef HMAP_THREAD_SAFE
        sem_wait(&hmap->lock);
#endif

#ifdef HMAP_DESTRUCTORS
        static uint32_t it;
        for (it=0; it < hmap->size; ++it) {
                if (hmap->map[it].v != NULL) {
                        hmap->del_fn(hmap->map[it].v);
                }
        }
#endif

        free(hmap->map);
       
#ifdef HMAP_THREAD_SAFE
        sem_post(&hmap->lock);
#endif
}
 
void __oa_hmap_add(key_val_map* map, uint32_t size, uint32_t (*hash_fn)(key),
                                  key in, val out) {
        static uint32_t hash;
        hash = hash_fn(in) % size;
       
        while (map[hash].v != NULL) {
                hash = (hash + 1) % size;
        }
       
        map[hash].k = in;
        map[hash].v = out;
}

bool __hmap_add(hashmap* hmap, key in, val out) {
#ifdef HMAP_THREAD_SAFE
        sem_wait(&hmap->lock);
#endif

        // performace degrades after a certain load
        if (((float) hmap->size) / hmap->capacity > 0.70) {
                key_val_map* temp = (key_val_map*) malloc(hmap->capacity * HMAP_GROWTH_RATE);
                if (temp != NULL) {
                        hmap->capacity *= HMAP_GROWTH_RATE;
                } else {
                #ifdef HMAP_THREAD_SAFE
                        sem_post(&hmap->lock);
                #endif
                        // we're out of memory
                        return false;
                }
               
                // re-posn all elements
                static uint32_t it;
                for (it=0; it < hmap->capacity; ++it) {
                        if (hmap->map[it].v != NULL) {
                                __oa_hmap_add(temp, hmap->capacity, hmap->hash_fn, in, out);
                        }
                }
               
                // swap out the old map with the new one
                free(hmap->map);
                hmap->map = temp;
        }
       
        __oa_hmap_add(hmap->map, hmap->capacity, hmap->hash_fn, in, out);
        hmap->size += 1;

#ifdef HMAP_THREAD_SAFE
        sem_post(&hmap->lock);
#endif

        return true;
}

#define hmap_add(hmap, in, out) __hmap_add(&hmap, (key) in, (val) out)

val __hmap_get(hashmap* hmap, key in) {
#ifdef HMAP_THREAD_SAFE
        sem_wait(&hmap->lock);
#endif

        static uint32_t hash;
        hash = hmap->hash_fn(in) % hmap->capacity;
       
        while (hmap->map[hash].v != NULL) {
                if (hmap->eq_fn(in, hmap->map[hash].k)) {
                #ifdef HMAP_THREAD_SAFE
                        sem_post(&hmap->lock);
                #endif                       
                       
                        return hmap->map[hash].v;
                }
               
                hash = (hash + 1) % hmap->capacity;
        }

       
#ifdef HMAP_THREAD_SAFE
        sem_post(&hmap->lock);
#endif
       
        return NULL;
}

#define hmap_get(hmap, obj) __hmap_get(&hmap, (val) obj)       

#ifdef HMAP_MAKE_HASHFN
// Robert Jenkins' 32 bit integer hash function
uint32_t int_hash_fn(key in) {
        static uint32_t a;
        a = *((uint32_t*) in);
       
        a = (a+0x7ed55d16) + (a<<12);
        a = (a^0xc761c23c) ^ (a>>19);
        a = (a+0x165667b1) + (a<<5);
        a = (a+0xd3a2646c) ^ (a<<9);
        a = (a+0xfd7046c5) + (a<<3);
        a = (a^0xb55a4f09) ^ (a>>16);
       
        return a;
}

bool int_eq_fn(key a, key b) {
        return *((uint32_t*) a) == *((uint32_t*) b) ? true : false;
}

#endif

Enjoy!

(If anyone would like to implement hmap_del, go for it!)

原帖链接:http://ubuntuforums.org/showthread.php?t=1222055


_________________
There should be one-- and preferably only one --obvious way to do it.


页首
 用户资料  
 
5 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 9:43 
头像

注册: 2008-02-17 23:47
帖子: 22110
地址: 美丽富饶的那啥星球
送出感谢: 11
接收感谢: 82
至少把图翻译一下吧 :em06


_________________
明天就换大三八!
——8核CPU、8G内存、8T硬盘……
8卡交火,80寸大屏放8个……
IPv8的光纤要8条……

---------------------------------------------------------------------------------
[图片版]新手当自强(续)FAQ
[新手进阶]挂载、fstab、调整linux分区
[图片版]E17桌面环境配置手把手


页首
 用户资料  
 
6 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 11:35 
头像

注册: 2007-05-06 2:46
帖子: 15634
送出感谢: 0 次
接收感谢: 2
这哥们才4楼就化了

:em04


页首
 用户资料  
 
7 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 11:50 
头像

注册: 2008-12-21 15:27
帖子: 5349
地址: TARDIS
送出感谢: 1
接收感谢: 10
:em20 整点小技巧什么的吧
这个一帖一堆代码,看不进去…… :em20


_________________


页首
 用户资料  
 
8 楼 
 文章标题 : Re: 外文ubuntu社区论坛的精华贴选,长期翻译更新中……
帖子发表于 : 2009-07-25 17:34 
头像

注册: 2008-09-05 23:31
帖子: 1363
系统: LinuxMint17
送出感谢: 35
接收感谢: 11
纯支持 :em11


_________________
茕茕
问题大海之一粟:http://forum.ubuntu.org.cn/viewtopic.php?f=74&t=212497
NEMO-Actions使用方法:http://forum.ubuntu.org.cn/viewtopic.php?f=175&t=456548
在线编码转换:http://2cyr.com/decode/?lang=en


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 8 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译