.net2.0 非法关键字过滤算法

偶尔在网上看到这一篇文章,有可能会用到此东西,暂时记下来,以后用的上时再研究

.net 2.0 不支持  HashSet ,需实现此类,比HashTab速度快一些

 
  
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;

public class HashSet < T > : ICollection < T > , ISerializable, IDeserializationCallback
{
private readonly Dictionary < T, object > dict;

public HashSet()
{
dict
= new Dictionary < T, object > ();
}

public HashSet(IEnumerable < T > items)
:
this ()
{
if (items == null )
{
return ;
}

foreach (T item in items)
{
Add(item);
}
}

public HashSet < T > NullSet { get { return new HashSet < T > (); } }

#region ICollection<T> Members

public void Add(T item)
{
if ( null == item)
{
throw new ArgumentNullException( " item " );
}

dict[item]
= null ;
}

/// <summary>
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/> .
/// </summary>
/// <exception cref="T:System.NotSupportedException"> The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
public void Clear()
{
dict.Clear();
}

public bool Contains(T item)
{
return dict.ContainsKey(item);
}

/// <summary>
/// Copies the items of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/> , starting at a particular <see cref="T:System.Array"/> index.
/// </summary>
/// <param name="array"> The one-dimensional <see cref="T:System.Array"/> that is the destination of the items copied from <see cref="T:System.Collections.Generic.ICollection`1"/> . The <see cref="T:System.Array"/> must have zero-based indexing. </param><param name="arrayIndex"> The zero-based index in <paramref name="array"/> at which copying begins. </param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0. </exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.-or- <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/> .-or-The number of items in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/> .-or-Type T cannot be cast automatically to the type of the destination <paramref name="array"/> . </exception>
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null ) throw new ArgumentNullException( " array " );
if (arrayIndex < 0 || arrayIndex >= array.Length || arrayIndex >= Count)
{
throw new ArgumentOutOfRangeException( " arrayIndex " );
}

dict.Keys.CopyTo(array, arrayIndex);
}

/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/> .
/// </summary>
/// <returns>
/// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/> ; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/> .
/// </returns>
/// <param name="item"> The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/> . </param><exception cref="T:System.NotSupportedException"> The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
public bool Remove(T item)
{
return dict.Remove(item);
}

/// <summary>
/// Gets the number of items contained in the <see cref="T:System.Collections.Generic.ICollection`1"/> .
/// </summary>
/// <returns>
/// The number of items contained in the <see cref="T:System.Collections.Generic.ICollection`1"/> .
/// </returns>
public int Count
{
get { return dict.Count; }
}

/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
/// </summary>
/// <returns>
/// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
/// </returns>
public bool IsReadOnly
{
get
{
return false ;
}
}

#endregion

public HashSet < T > Union(HashSet < T > set )
{
HashSet
< T > unionSet = new HashSet < T > ( this );

if ( null == set )
{
return unionSet;
}

foreach (T item in set )
{
if (unionSet.Contains(item))
{
continue ;
}

unionSet.Add(item);
}

return unionSet;
}

public HashSet < T > Subtract(HashSet < T > set )
{
HashSet
< T > subtractSet = new HashSet < T > ( this );

if ( null == set )
{
return subtractSet;
}

foreach (T item in set )
{
if ( ! subtractSet.Contains(item))
{
continue ;
}

subtractSet.dict.Remove(item);
}

return subtractSet;
}

public bool IsSubsetOf(HashSet < T > set )
{
HashSet
< T > setToCompare = set ?? NullSet;

foreach (T item in this )
{
if ( ! setToCompare.Contains(item))
{
return false ;
}
}

return true ;
}

public HashSet < T > Intersection(HashSet < T > set )
{
HashSet
< T > intersectionSet = NullSet;

if ( null == set )
{
return intersectionSet;
}

foreach (T item in this )
{
if ( ! set .Contains(item))
{
continue ;
}

intersectionSet.Add(item);
}

foreach (T item in set )
{
if ( ! Contains(item) || intersectionSet.Contains(item))
{
continue ;
}

intersectionSet.Add(item);
}

return intersectionSet;
}

public bool IsProperSubsetOf(HashSet < T > set )
{
HashSet
< T > setToCompare = set ?? NullSet;

// A is a proper subset of a if the b is a subset of a and a != b
return (IsSubsetOf(setToCompare) && ! setToCompare.IsSubsetOf( this ));
}

public bool IsSupersetOf(HashSet < T > set )
{
HashSet
< T > setToCompare = set ?? NullSet;

foreach (T item in setToCompare)
{
if ( ! Contains(item))
{
return false ;
}
}

return true ;
}

public bool IsProperSupersetOf(HashSet < T > set )
{
HashSet
< T > setToCompare = set ?? NullSet;

// B is a proper superset of a if b is a superset of a and a != b
return (IsSupersetOf(setToCompare) && ! setToCompare.IsSupersetOf( this ));
}

public List < T > ToList()
{
return new List < T > ( this );
}

#region Implementation of ISerializable

/// <summary>
/// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
/// </summary>
/// <param name="info"> The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data. </param><param name="context"> The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/> ) for this serialization. </param><exception cref="T:System.Security.SecurityException"> The caller does not have the required permission. </exception>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null ) throw new ArgumentNullException( " info " );
dict.GetObjectData(info, context);
}

#endregion

#region Implementation of IDeserializationCallback

/// <summary>
/// Runs when the entire object graph has been deserialized.
/// </summary>
/// <param name="sender"> The object that initiated the callback. The functionality for this parameter is not currently implemented. </param>
public void OnDeserialization( object sender)
{
dict.OnDeserialization(sender);
}

#endregion

#region Implementation of IEnumerable

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority> 1 </filterpriority>
public IEnumerator < T > GetEnumerator()
{
return dict.Keys.GetEnumerator();
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority> 2 </filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion
}

以下为过滤算法

 
  
public class BadWordsFilter
{
private HashSet < string > hash = new HashSet < string > ();
private byte [] fastCheck = new byte [ char .MaxValue];
private byte [] fastLength = new byte [ char .MaxValue];
private BitArray charCheck = new BitArray( char .MaxValue);
private BitArray endCheck = new BitArray( char .MaxValue);
private int maxWordLength = 0 ;
private int minWordLength = int .MaxValue;

public BadWordsFilter()
{

}

public void Init( string [] badwords)
{
foreach ( string word in badwords)
{
maxWordLength
= Math.Max(maxWordLength, word.Length);
minWordLength
= Math.Min(minWordLength, word.Length);

for ( int i = 0 ; i < 7 && i < word.Length; i ++ )
{
fastCheck[word[i]]
|= ( byte )( 1 << i);
}

for ( int i = 7 ; i < word.Length; i ++ )
{
fastCheck[word[i]]
|= 0x80 ;
}

if (word.Length == 1 )
{
charCheck[word[
0 ]] = true ;
}
else
{
fastLength[word[
0 ]] |= ( byte )( 1 << (Math.Min( 7 , word.Length - 2 )));
endCheck[word[word.Length
- 1 ]] = true ;

hash.Add(word);
}
}
}

public string Filter( string text, string mask)
{
throw new NotImplementedException();
}

public bool HasBadWord( string text)
{
int index = 0 ;

while (index < text.Length)
{
int count = 1 ;

if (index > 0 || (fastCheck[text[index]] & 1 ) == 0 )
{
while (index < text.Length - 1 && (fastCheck[text[ ++ index]] & 1 ) == 0 ) ;
}

char begin = text[index];

if (minWordLength == 1 && charCheck[begin])
{
return true ;
}

for ( int j = 1 ; j <= Math.Min(maxWordLength, text.Length - index - 1 ); j ++ )
{
char current = text[index + j];

if ((fastCheck[current] & 1 ) == 0 )
{
++ count;
}

if ((fastCheck[current] & ( 1 << Math.Min(j, 7 ))) == 0 )
{
break ;
}

if (j + 1 >= minWordLength)
{
if ((fastLength[begin] & ( 1 << Math.Min(j - 1 , 7 ))) > 0 && endCheck[current])
{
string sub = text.Substring(index, j + 1 );

if (hash.Contains(sub))
{
return true ;
}
}
}
}

index
+= count;
}

return false ;
}
}
}

转载于:https://www.cnblogs.com/qingyi/archive/2011/03/08/1977494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值