binary_search

来源:互联网 发布:网络指纹考勤 编辑:程序博客网 时间:2024/05/17 03:20

#ifndef BinarySearch_H#define BinarySearch_H//////////////////////////////////////////////////////////////////////////#include <string.h>//////////////////////////////////////////////////////////////////////////template <typename key_type, typename value_type>struct Key2Value{typedef key_type key_t;typedef value_type value_t;key_t key;value_t value;static bool equal(key_t key1, key_t key2){ return (key1 == key2); }static bool less_equal(key_t key1, key_t key2){ return (key1 <= key2); }};template <typename value_type>struct Key2Value<const char *, value_type>{typedef const char *key_t;typedef value_type value_t;key_t key;value_t value;static bool equal(key_t key1, key_t key2){ return (strcmp(key1, key2) == 0); }static bool less_equal(key_t key1, key_t key2){ return (strcmp(key1, key2) <= 0); }};////////////////////////////////////////////////////////////////////////////Warning: sorted_array must be sorted by key!!!template <typename array_type>size_t binary_search(const array_type *sorted_array, size_t count, typename array_type::key_t key){size_t hi= count;size_t lo= -1;size_t mid= 0;while (hi - lo > 1) {mid = (hi + lo) / 2;if (array_type::less_equal(key, sorted_array[mid].key)) {hi = mid;}else {lo = mid;}}if (array_type::equal(key, sorted_array[hi].key)) {return hi;}return -1;}//////////////////////////////////////////////////////////////////////////#endif


#include <stdio.h>//////////////////////////////////////////////////////////////////////////#include <map>#include <string>using std::map;using std::string;//////////////////////////////////////////////////////////////////////////#include "BinarySearch.h"////////////////////////////////////////////////////////////////////////////These must be sortedstatic Key2Value<size_t, const char *> s_num2text[] ={{ 100,"Continue", }, { 200,"OK", }, { 201,"Created", }, { 250,"Low on Storage Space", }, { 300,"Multiple Choices", }, { 301,"Moved Permanently", }, { 302,"Moved Temporarily", }, { 303,"See Other", }, { 304,"Not Modified", }, { 305,"Use Proxy", }, { 400,"Bad Request", }, { 401,"Unauthorized", }, { 402,"Payment Required", }, { 403,"Forbidden", }, { 404,"Not Found", }, { 405,"Method Not Allowed", }, { 406,"Not Acceptable", }, { 407,"Proxy Authentication Required", }, { 408,"Request Time-out", }, { 410,"Gone", }, { 411,"Length Required", }, { 412,"Precondition Failed", }, { 413,"Request Entity Too Large", }, { 414,"Request-URI Too Large", }, { 415,"Unsupported Media Type", }, { 451,"Parameter Not Understood", }, { 452,"Conference Not Found", }, { 453,"Not Enough Bandwidth", }, { 454,"Session Not Found", }, { 455,"Method Not Valid in This State", }, { 456,"Header Field Not Valid for Resource", }, { 457,"Invalid Range", }, { 458,"Parameter Is Read-Only", }, { 459,"Aggregate operation not allowed", }, { 460,"Only aggregate operation allowed", }, { 461,"Unsupported transport", }, { 462,"Destination unreachable", }, { 500,"Internal Server Error", }, { 501,"Not Implemented", }, { 502,"Bad Gateway", }, { 503,"Service Unavailable", }, { 504,"Gateway Time-out", }, { 505,"Rtsp Version not supported", }, { 551,"Option not supported", }, };//These must be sortedstatic Key2Value<const char *, size_t> s_text2num[] ={{ "Aggregate operation not allowed",459, }, { "Bad Gateway",502, }, { "Bad Request",400, }, { "Conference Not Found",452, }, { "Continue",100, }, { "Created",201, }, { "Destination unreachable",462, }, { "Forbidden",403, }, { "Gateway Time-out",504, }, { "Gone",410, }, { "Header Field Not Valid for Resource",456, }, { "Internal Server Error",500, }, { "Invalid Range",457, }, { "Length Required",411, }, { "Low on Storage Space",250, }, { "Method Not Allowed",405, }, { "Method Not Valid in This State",455, }, { "Moved Permanently",301, }, { "Moved Temporarily",302, }, { "Multiple Choices",300, }, { "Not Acceptable",406, }, { "Not Enough Bandwidth",453, }, { "Not Found",404, }, { "Not Implemented",501, }, { "Not Modified",304, }, { "OK",200, }, { "Only aggregate operation allowed",460, }, { "Option not supported",551, }, { "Parameter Is Read-Only",458, }, { "Parameter Not Understood",451, }, { "Payment Required",402, }, { "Precondition Failed",412, }, { "Proxy Authentication Required",407, }, { "Request Entity Too Large",413, }, { "Request Time-out",408, }, { "Request-URI Too Large",414, }, { "Rtsp Version not supported",505, }, { "See Other",303, }, { "Service Unavailable",503, }, { "Session Not Found",454, }, { "Unauthorized",401, }, { "Unsupported Media Type",415, }, { "Unsupported transport",461, }, { "Use Proxy",305, }, };//////////////////////////////////////////////////////////////////////////int main(){printf("Print Sort String --------------------------------------------------------------");map<string, size_t> text2num;for (size_t i=0; i<(sizeof(s_text2num)/sizeof(s_text2num[0])); ++i) {text2num[s_text2num[i].key] = s_text2num[i].value;}for (map<string, size_t>::const_iterator cit=text2num.begin(); cit!=text2num.end(); ++cit) {printf("%36s, %d\n", cit->first.c_str(), cit->second);}printf("Print Num2Text -----------------------------------------------------------------");for (size_t i=0; i<560; ++i) {size_t index = binary_search(&s_num2text[0], sizeof(s_num2text)/sizeof(s_num2text[0]), i);if (index != -1) {printf("%36d, %s\n", s_num2text[index].key, s_num2text[index].value);}}printf("Print Text2Num -----------------------------------------------------------------");const char *text[] = {"Aggregate operation not allowed", "Bad Gateway", "Created", "Destination unreachable", "Gone", "Header Field Not Valid for Resource", "Invalid Range", "Length Required", "Low on Storage Space", "Method Not Allowed", "Moved Temporarily", "Multiple Choices", "Not Implemented", "Not Modified", "OK", "Only aggregate operation allowed", "Parameter Not Understood", "Proxy Authentication Required", "Request Entity Too Large", "Rtsp Version not supported", "See Other", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "Session Not Found", "Unsupported transport", "Use Proxy", };for (size_t i=0; i<(sizeof(text)/sizeof(text[0])); ++i) {size_t index = binary_search(&s_text2num[0], sizeof(s_text2num)/sizeof(s_text2num[0]), text[i]);if (index != -1) {printf("%36s, %d\n", s_text2num[index].key, s_text2num[index].value);}else {printf("NOT FOUND %s!!!\n", text[i]);}}return 0;}


0 0
原创粉丝点击