Programming with the APR – Using APR HashTables
来源:互联网 发布:龙腾世纪审判优化 编辑:程序博客网 时间:2024/06/08 13:44
Programming with the APR – Using APR HashTables
This short article will show how to use the Apache Portable Runtime (APR) with it’s built in hashtable.
Introduction
If you are in need to place data in a memory structure to access it quickly, you are probably best with a hash table data type. The Apache Portable Runtime (APR) luckily, along some others like e.g.apr_table_t or apr_array_header_t (both defined in apr_tables.h), provides such a data type to you.
Hashtables do have the big advantage that you can supply them any data type you like. The nect big thing with hashtables is that they are usually very efficient if the number of elements that they are holding grows.
You can download the example file here.
Let’s hash
In order to demonstrate hashtables we are using a very simple data structure which holds a date, a username and a password. We then set some values onto this data structure and save the created structure into the hashtable.
After storing that object into the hashtable we are reading the objects back from the hashtable and output it’s contents.
The c0de
Now that you do have some overview of what we are actually doing here, it’s time to show you some code. I think it pretty much speaks for itself.
You should however, if you are using strings as keys, use the special APR_HASH_KEY_STRING value to indicate a string valued key to APR. This will use strlen(key) to compute the length (NUL terminator is not included there).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
#include <apr.h>#include <apr_hash.h>#include <apr_time.h>#define APR_WANT_STDIO#include <apr_want.h> struct userRecord{ apr_time_t creationDate; char username[30]; char password[30];}; void print(struct userRecord *user){ apr_pool_t *p = NULL; char *timeReadable = NULL; apr_pool_create(&p, NULL); int i = 0; timeReadable = apr_palloc(p, APR_RFC822_DATE_LEN); apr_rfc822_date(timeReadable, user->creationDate); printf("Username:\t\t %s\n", user->username); printf("Password:\t\t %s\n", user->password); printf("CreationDate:\t\t %s\n", timeReadable); apr_pool_destroy(p); return;} /* Compile with * $> export APR_LIBS="`apr-1-config --cflags --cppflags --includes --ldflags --link-ld --libs`" * $> export APU_LIBS="`apu-1-config --includes --ldflags --link-ld --libs`" * $> gcc $APR_LIBS $APU_LIBS aprHashtable.c -o aprHashtable */int main(int argc, const char* const* argv){ apr_status_t rv; apr_pool_t *p = NULL; struct userRecord *cUser = NULL; struct userRecord *readUser = NULL; int i = 0; char countName[12]; apr_hash_index_t *hidx = NULL; apr_app_initialize(&argc, &argv, NULL); atexit(apr_terminate); apr_pool_create(&p, NULL); apr_hash_t *ht = apr_hash_make(p); apr_cpystrn(countName, "Jens Frey ", 12); for (i=0; i < 26; i++) { countName[10] = (char) i + 65; cUser = apr_palloc(p, sizeof(struct userRecord)); apr_cpystrn(cUser->username, countName, strlen(countName) + 1); apr_cpystrn(cUser->password, "secret", strlen("secret") + 1); cUser->creationDate = apr_time_now(); apr_hash_set(ht, cUser->username, APR_HASH_KEY_STRING, cUser); print(cUser); } for (hidx = apr_hash_first(p, ht); hidx; hidx = apr_hash_next(hidx)) { apr_hash_this(hidx, NULL, NULL, (void*) &readUser); print(readUser); } readUser = apr_hash_get(ht, "Jens Frey G", APR_HASH_KEY_STRING); printf("\n\n"); print(readUser); printf("Hashtable size: %d bytes\n", apr_hash_count(ht) * sizeof(struct userRecord) + apr_hash_count(ht) * sizeof(readUser->username)); apr_terminate(); return rv;}
Now go and hash some values …
- Programming with the APR – Using APR HashTables
- apr
- APR
- configure: error: Cannot use an external APR with the bundled APR-utilq错误解决
- configure: error: Cannot use an external APR with the bundled APR-util
- Linux安装apache源码包报错:Cannot use an external APR with the bundled APR-util
- Tomcat SSL配置 Connector attribute SSLCertificateFile must be defined when using SSL with APR解决
- Tomcat SSL配置 Connector attribute SSLCertificateFile must be defined when using SSL with APR解决
- Tomcat SSL配置 Connector attribute SSLCertificateFile must be defined when using SSL with APR解决
- Tomcat配置Https报错-Connector attribute SSLCertificateFile must be defined when using SSL with APR
- Connector attribute SSLCertificateFile must be defined when using SSL with APR解决方法
- Tomcat SSL配置 Connector attribute SSLCertificateFile must be defined when using SSL with APR解决
- Thu Apr 7 using cscope in emacs
- Thu Apr 7 using cscope in emacs
- The APR based Apache Tomcat Native library
- C++习题(from the teacher)Apr.
- The APR based Apache Tomcat Native library
- The APR based Apache Tomcat Native library
- QT Creator2.7与vs2008
- TabHost中使用startActivityForResult无法接收返回值的解决方案[转]
- c语言的static属性
- Random generator and text with OpenCV¶
- ubuntu软件源
- Programming with the APR – Using APR HashTables
- JUnit4教程
- Java获取网页源代码
- 国际化专业开源的电子商务系统-magento
- 古典加密算法(1)
- 应用程序实现读写PCIE设备配置空间
- 面向对象设计模式总结
- 滴血认亲
- ubuntu 11.10 安装sun-java6-jre