C#根据url生成唯一的key
根据url生成唯一的idkey,记录并分享:
public class UrlToUniqueKey
{
private static Hashtable generators = new Hashtable();
public static long[,] polynomials = new long[, ]
{
{
0L,
0L
},
{
-4611686018427387904L,
-4611686018427387904L
},
{
-2305843009213693952L,
-2305843009213693952L
},
{
-3458764513820540928L,
-5764607523034234880L
},
{
-576460752303423488L,
-576460752303423488L
},
{
-1441151880758558720L,
-4899916394579099648L
},
{
-2738188573441261568L,
-5332261958806667264L
},
{
-1945555039024054272L,
-1945555039024054272L
},
{
-7602076171001397248L,
-3134505340649865216L
},
{
-9169328841326329856L,
-8628896886041870336L
},
{
-5719571526760529920L,
-1612288666598637568L
},
{
-2742692173068632064L,
-3913628076184961024L
},
{
-1776670052997660672L,
-2407174000829530112L
},
{
-2005227734086713344L,
-7718043861406187520L
},
{
-3112550292466434048L,
-5147051424131055616L
},
{
-2113595600120315904L,
-7045600142044430336L
},
{
-3682677857793867776L,
-7529033414544982016L
},
{
-2812286861060341760L,
-6211097204841512960L
},
{
-7338580408428134400L,
-181868019327172608L
},
{
-4070603152259284992L,
-7580349821236543488L
},
{
-4690912428278415360L,
-1656541810593366016L
},
{
-8730258663983284224L,
-1124153882377715712L
},
{
-7455702586042089472L,
-7995369281233616896L
},
{
-58297206016311296L,
-5584750510474264576L
},
{
-8198983991290757120L,
-6900195776585007104L
},
{
-3368309616348758016L,
-777578196575977472L
},
{
-7950798515817414656L,
-5274165001345564672L
},
{
-1814110279349305344L,
-3073563053254508544L
},
{
-7333090856108294144L,
-299958969764413440L
},
{
-5862047463258456064L,
-1088406921112715264L
},
{
-2699705949936943104L,
-6180435270106611712L
},
{
-5010893113942605824L,
-745326200523587584L
},
{
-7247248171544346624L,
-7637035222663430144L
},
{
-379617898928275456L,
-2128952956840574976L
},
{
-8982278211127738368L,
-8328970742175629312L
},
{
-2699136824088985600L,
-657743201922187264L
},
{
-5723859659287166976L,
-3191693334452109312L
},
{
-5937852116755283968L,
-8585462099679903744L
},
{
-4340513297460625408L,
-2535761738024878080L
},
{
-7223736098545991680L,
-5810321976182439936L
},
{
-8771886216598519808L,
-8198507976365965312L
},
{
-1991715210309664768L,
-3768584310714531840L
},
{
-9162551332419141632L,
-8616797614972600320L
},
{
-719027294788648960L,
-6649233720883544064L
},
{
-3559483092657242112L,
-7475962079365038080L
},
{
-713657193466691584L,
-3981624362359062528L
},
{
-2043424698022887424L,
-3853773324916752384L
},
{
-4041900042031005696L,
-2750302262364798976L
},
{
-4436928417950760960L,
-8365357674703716352L
},
{
-5903553087924551680L,
-232537924545363968L
},
{
-8728448178608234496L,
-4183444001187078144L
},
{
-2341229233224585216L,
-6731777037870903296L
},
{
-1649801635265304576L,
-2988673641163274240L
},
{
-7620924682850272256L,
-4999586984279010304L
},
{
-6766504171168936448L,
-3233736554084598272L
},
{
-4307058212505990400L,
-7352830162319492864L
},
{
-6559722688844616064L,
-3544821563555732608L
},
{
-5503660899170514624L,
-9177641346571454016L
},
{
-187437317177603296L,
-1362648669985435232L
},
{
-24562796859796592L,
-1298422235250900464L
},
{
-3825548837209328312L,
-4007863133653759080L
},
{
-5023278451590057620L,
-8135395730479815484L
},
{
-1370071926941500434L,
-7957612307809224958L
},
{
-2163743556238479977L,
-2346333074831914631L
},
{
-2935493925047926053L,
-7378668602973054136L
}
};
public static UrlToUniqueKey std64 = UrlToUniqueKey.make(UrlToUniqueKey.polynomials[, ], );
public static UrlToUniqueKey std32 = UrlToUniqueKey.make(UrlToUniqueKey.polynomials[, ], );
public static UrlToUniqueKey std40 = UrlToUniqueKey.make(UrlToUniqueKey.polynomials[, ], );
public static UrlToUniqueKey std24 = UrlToUniqueKey.make(UrlToUniqueKey.polynomials[, ], );
private long zero = 0L;
private long one = long.MinValue;
public long empty;
public int degree;
public long polynomial;
private long[,] ByteModTable; private UrlToUniqueKey(long polynomial, int degree)
{
this.degree = degree;
this.polynomial = polynomial;
this.ByteModTable = new long[, ];
long[] numArray = new long[];
long num1 = long.MinValue;
long num2 = this.rightMove(long.MinValue, degree - );
for (int index = ; index < ; ++index)
{
numArray[index] = num1;
bool flag = (num1 & num2) != 0L;
num1 = this.rightMove(num1, );
if (flag)
num1 ^= polynomial;
}
this.empty = numArray[];
for (int index1 = ; index1 < ; ++index1)
{
for (int index2 = ; index2 < ; ++index2)
{
long num3 = 0L;
for (int index3 = ; index3 < ; ++index3)
{
if ((index2 & << index3) != )
num3 ^= numArray[(int)sbyte.MaxValue - index1 * - index3];
}
this.ByteModTable[index1, index2] = num3;
}
}
} public static UrlToUniqueKey make(long polynomial, int degree)
{
long num = polynomial;
UrlToUniqueKey heritrix = (UrlToUniqueKey)UrlToUniqueKey.generators[(object)];
if (heritrix == null)
{
heritrix = new UrlToUniqueKey(polynomial, degree);
UrlToUniqueKey.generators.Add((object)num, (object)heritrix);
}
return heritrix;
} private long rightMove(long value, int pos)
{
if (pos != )
{
long num = long.MaxValue;
value >>= ;
value &= num;
value >>= pos - ;
}
return value;
} public static long long_rightMove(long value, int pos)
{
if (pos != )
{
long num = long.MaxValue;
value >>= ;
value &= num;
value >>= pos - ;
}
return value;
} private int int_rightMove(int value, int pos)
{
if (pos != )
{
int num = int.MaxValue;
value >>= ;
value &= num;
value >>= pos - ;
}
return value;
} public long reduce(long fp)
{
int num1 = - this.degree / ;
long num2 = num1 == ? 0L : fp & -1L << * num1;
long num3 = 0L;
for (int index = ; index < num1; ++index)
{
num3 ^= this.ByteModTable[ + index, (int)fp & (int)byte.MaxValue];
fp = this.rightMove(fp, );
}
return num2 ^ num3;
} public long extend_byte(long f, int v)
{
f ^= (long)((int)byte.MaxValue & v);
int num = (int)f;
return this.rightMove(f, ) ^ this.ByteModTable[, num & (int)byte.MaxValue];
} public long extend_char(long f, int v)
{
f ^= (long)((int)ushort.MaxValue & v);
int num = (int)f;
return this.rightMove(f, ) ^ this.ByteModTable[, num & (int)byte.MaxValue] ^ this.ByteModTable[, this.int_rightMove(num, ) & (int)byte.MaxValue];
} public long extend_int(long f, int v)
{
f ^= (long)uint.MaxValue & (long)v;
int num1 = (int)f;
long num2 = this.rightMove(f, ) ^ this.ByteModTable[, num1 & (int)byte.MaxValue];
int num3 = this.int_rightMove(num1, );
long num4 = num2 ^ this.ByteModTable[, num3 & (int)byte.MaxValue];
int num5 = this.int_rightMove(num3, );
return num4 ^ this.ByteModTable[, num5 & (int)byte.MaxValue] ^ this.ByteModTable[, this.int_rightMove(num5, ) & (int)byte.MaxValue];
} public long extend_long(long f, long v)
{
f ^= v;
long num1 = this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num2 = num1 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num3 = num2 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num4 = num3 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num5 = num4 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num6 = num5 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
long num7 = num6 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
f = this.rightMove(f, );
return num7 ^ this.ByteModTable[, (int)(f & (long)byte.MaxValue)];
} public long fp(byte[] buf, int start, int n)
{
return this.extend(this.empty, buf, start, n);
} public long fp(char[] buf, int start, int n)
{
return this.extend(this.empty, buf, start, n);
} public long fp(string s)
{
return this.extend(this.empty, s);
} public long fp(int[] buf, int start, int n)
{
return this.extend(this.empty, buf, start, n);
} public long fp(long[] buf, int start, int n)
{
return this.extend(this.empty, buf, start, n);
} public long fp8(string s)
{
return this.extend8(this.empty, s);
} public long fp8(char[] buf, int start, int n)
{
return this.extend8(this.empty, buf, start, n);
} public long extend(long f, byte v)
{
return this.reduce(this.extend_byte(f, (int)v));
} public long extend(long f, char v)
{
return this.reduce(this.extend_char(f, (int)v));
} public long extend(long f, int v)
{
return this.reduce(this.extend_int(f, v));
} public long extend(long f, long v)
{
return this.reduce(this.extend_long(f, v));
} public long extend(long f, byte[] buf, int start, int n)
{
for (int index = ; index < n; ++index)
f = this.extend_byte(f, (int)buf[start + index]);
return this.reduce(f);
} public long extend(long f, char[] buf, int start, int n)
{
for (int index = ; index < n; ++index)
f = this.extend_char(f, (int)buf[start + index]);
return this.reduce(f);
} public long extend(long f, string s)
{
int length = s.Length;
for (int index = ; index < length; ++index)
{
int v = (int)s[index];
f = this.extend_char(f, v);
}
return this.reduce(f);
} public long extend(long f, int[] buf, int start, int n)
{
for (int index = ; index < n; ++index)
f = this.extend_int(f, buf[start + index]);
return this.reduce(f);
} public long extend(long f, long[] buf, int start, int n)
{
for (int index = ; index < n; ++index)
f = this.extend_long(f, buf[start + index]);
return this.reduce(f);
} public long extend8(long f, string s)
{
int length = s.Length;
for (int index = ; index < length; ++index)
{
int v = (int)s[index];
f = this.extend_byte(f, v);
}
return this.reduce(f);
} public long extend8(long f, char[] buf, int start, int n)
{
for (int index = ; index < n; ++index)
f = this.extend_byte(f, (int)buf[start + index]);
return this.reduce(f);
} public static ulong createUrlkey(string url)
{
int length = url.IndexOf("://");
if (length > )
length = url.IndexOf('/', length + "://".Length);
string s = length == - ? url : url.Substring(, length);
return Convert.ToUInt64(Convert.ToString(UrlToUniqueKey.std32.fp(s) | UrlToUniqueKey.long_rightMove(UrlToUniqueKey.std40.fp(url), ), ).PadLeft(, ''), );
}
/// <summary>
/// 根据url生成唯一id
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static long createUrlToLong(string url)
{
int length = url.IndexOf("://");
if (length > )
length = url.IndexOf('/', length + "://".Length);
string s = length == - ? url : url.Substring(, length);
return UrlToUniqueKey.std32.fp(s) | UrlToUniqueKey.long_rightMove(UrlToUniqueKey.std40.fp(url), );
}
}
C#根据url生成唯一的key的更多相关文章
- 在C#中生成唯一的字符串和数字【GUID】转
转自:http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html 当我们想要获得一个唯一的key的时候,通常会想到GUID.这个key非常 ...
- PHP使用SnowFlake算法生成唯一ID
前言:最近需要做一套CMS系统,由于功能比较单一,而且要求灵活,所以放弃了WP这样的成熟系统,自己做一套相对简单一点的.文章的详情页URL想要做成url伪静态的格式即xxx.html 其中xxx考虑过 ...
- YII框架路由和URL生成
路由和URL生成 当一个YII应用开始处理一个请求的时候,它首先要做的便是将请求的URL转化成一个路由.路由的作用是用于后续实例化相应的控制器和操作,以便处理请求,整个处理过程便叫做路由.路由的逆过程 ...
- 转:C#生成唯一值的方法汇总
这篇文章主要介绍了C#生成唯一值的方法汇总,有需要的朋友可以参考一下 生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: 一.在 .NET ...
- C#生成唯一值的方法汇总
生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: https://www.cnblogs.com/xinweichen/p/4287640 ...
- Laravel 深入理解路由和URL生成
原文地址: Laravel 深入理解路由和URL生成 在模板中我们一般不会直接写死url,而是用url助手生成url,本文介绍一下url助手的使用以及遇到的一些比较头疼的问题. 首先,我们创建了一个路 ...
- php生成唯一订单号
支持更改长度/** * 生成唯一订单号 * */ function build_order_no(){ return date('Ymd').substr(implode(NULL, array_ma ...
- ASP.NET根据URL生成网页缩略图示例程序(C#语言)
工作中可能马上要用到根据URL生成网页缩略图功能,提前做好准备. 在网上找了份源码,但是有错误:当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a ...
- php生成唯一随机码
最终使用: echo md5(time() . mt_rand(1,1000000)) //A:利用时间戳的方法 md5("admin"); // B:32位MD5加密 subst ...
随机推荐
- {Reship}{Sparse Representation}稀疏表示入门
声明:本人属于绝对的新手,刚刚接触“稀疏表示”这个领域.之所以写下以下的若干个连载,是鼓励自己不要急功近利,而要步步为赢!所以下文肯定有所纰漏,敬请指出,我们共同进步! 踏入“稀疏表达”(Sparse ...
- nodejs之assert
assert断言在nodejs中的用法: 1.引入assert模块 2.语法 assert('条件', '条件不成立时显示信息'); 例如:assert.js文件 const assert = req ...
- travis-cli 使用
1. 添加项目 登录 travis 选择对应项目即可 2. 添加持续集成文件 .travis.yml language: node_js node_js: - "node" bef ...
- shell监控网卡状态,故障时自动重启网卡
今天朋友找我写个监控网卡状态的脚本,要求在系统网卡挂了可以自己启动起来,这个要求是不是很bt,我考虑了半天,简单的写了个shell脚本来监控,实现原理是使用ping来测试网络连通性,如果不通就重启 ...
- k-means算法Java一维实现
这里的程序稍微有点变形.k_means方法返回K-means聚类的若干中心点.代码: import java.util.ArrayList; import java.util.Collections; ...
- 语义分割【semantic-segmentation】资料备忘
https://github.com/mrgloom/awesome-semantic-segmentation
- 【转】Jmeter入门:如何建立和使用Jmeter测试环境
一.工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用, 后来又扩充了其它的功能.j ...
- 杂项:HTML5-1/3-发展历程
ylbtech-杂项:HTML5-1/3-发展历程 万维网的核心语言.标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改(这是一项推荐标准.外语原文:W3C Recommendat ...
- 【学步者日记】C#使用线程
http://note.youdao.com/noteshare?id=2810300cdfa3f4d973792dcf30a31db9 System.Threading.Thread th; th ...
- nginx上传文件大小
采用nginx作反向代理,出现了一个诡异的问题,小文件可以提交,大文件会报500内部错误.这个是什么原因导致的呢? 查wiki可知,上传文件大小相关的有三个配置 client_body_buffer_ ...