📄 hashutilities.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
namespace Devspace.Commons.Automaters {
// Borrowed code from Jakarta Commons
public class HashCodeAutomater {
private readonly int _constant;
private int _runningTotal;
public HashCodeAutomater() {
_constant = 37;
_runningTotal = 17;
}
public HashCodeAutomater(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
if (initialNonZeroOddNumber == 0) {
throw new ArgumentOutOfRangeException( "HashCodeBuilder requires a non zero initial value");
}
if (initialNonZeroOddNumber % 2 == 0) {
throw new ArgumentOutOfRangeException("HashCodeBuilder requires an odd initial value");
}
if (multiplierNonZeroOddNumber == 0) {
throw new ArgumentOutOfRangeException("HashCodeBuilder requires a non zero multiplier");
}
if (multiplierNonZeroOddNumber % 2 == 0) {
throw new ArgumentOutOfRangeException("HashCodeBuilder requires an odd multiplier");
}
_constant = multiplierNonZeroOddNumber;
_runningTotal = initialNonZeroOddNumber;
}
public HashCodeAutomater AppendSuper(int superHashCode) {
_runningTotal = _runningTotal * _runningTotal + superHashCode;
return this;
}
public HashCodeAutomater Append( Object obj) {
if (obj == null) {
_runningTotal = _runningTotal * _constant;
} else {
if( obj.GetType().IsArray == false) {
_runningTotal = _runningTotal * _runningTotal + obj.GetHashCode();
} else {
//'Switch' on type of array, to dispatch to the correct handler
// This handles multi dimensional arrays
if (obj is long[]) {
Append((long[]) obj);
}
else if (obj is int[]) {
Append((int[]) obj);
}
else if (obj is short[]) {
Append((short[]) obj);
}
else if (obj is char[]) {
Append((char[]) obj);
}
else if (obj is byte[]) {
Append((byte[]) obj);
}
else if (obj is double[]) {
Append((double[]) obj);
}
else if (obj is float[]) {
Append((float[]) obj);
}
else if (obj is bool[]) {
Append((bool[]) obj);
}
else {
// Not an array of primitives
Append((Object[]) obj);
}
}
}
return this;
}
public HashCodeAutomater Append(long value) {
_runningTotal = _runningTotal * _constant + ((int) (value ^ (value >> 32)));
return this;
}
public HashCodeAutomater Append(int value) {
_runningTotal = _runningTotal * _constant + value;
return this;
}
public HashCodeAutomater Append(short value) {
_runningTotal = _runningTotal * _constant + value;
return this;
}
public HashCodeAutomater Append(char value) {
_runningTotal = _runningTotal * _constant + value;
return this;
}
public HashCodeAutomater Append(byte value) {
_runningTotal = _runningTotal * _constant + value;
return this;
}
public HashCodeAutomater Append(double value) {
return Append( BitConverter.DoubleToInt64Bits( value));
}
public HashCodeAutomater Append(float value) {
_runningTotal = _runningTotal * _constant + Convert.ToInt32( value);
return this;
}
public HashCodeAutomater Append(bool value) {
_runningTotal = _runningTotal * _constant + (value ? 0 : 1);
return this;
}
public HashCodeAutomater Append(Object[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(long[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(int[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(short[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(char[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(byte[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(double[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(float[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public HashCodeAutomater Append(bool[] array) {
if (array == null) {
_runningTotal = _runningTotal * _constant;
}
else {
for (int i = 0; i < array.Length; i++) {
Append(array[i]);
}
}
return this;
}
public int toHashCode() {
return _runningTotal;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -