1 文本格式
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer
{
public abstract class Mhash<K, V> : Hashtable<K>
{
private List<V> els { get; set; } = new List<V>();
/// <summary>
/// Links to next sister element under a single key.
/// </summary>
private int[] nextsis { get; set; }
private int nextget { get; set; }
public Mhash(int nh, int nm) : base(nh, nm)
{
nextget = -1;
els = new List<V>(nm);
for (int i = 0; i < nm; i++)
{
els.Add(default(V));
}
nextsis = new int[nm];
for (int j = 0; j < nm; j++)
{
nextsis[j] = -2;
}
}
/// <summary>
/// Store an element el under key.Return index in 0..nmax-1, giving the
/// storage location utilized.
/// </summary>
/// <param name="key"></param>
/// <param name="el"></param>
/// <returns></returns>
public int store(K key, V el)
{
int j = iset(key);
if (nextsis[j] == -2)
{
els[j] = el;
nextsis[j] = -1;
return j;
}
else
{
while (nextsis[j] != -1)
{
j = nextsis[j];
}
int k = ireserve();
els[k] = el;
nextsis[j] = k;
nextsis[k] = -1;
return k;
}
}
/// <summary>
/// Erase an element el previously stored under key.Return 1 for success, or 0
/// if no matching element is found.Note: The == operation must be defined for
/// the type V.
/// </summary>
/// <param name="key"></param>
/// <param name="el"></param>
/// <returns></returns>
public int erase(K key, V el)
{
int j = -1;
int kp = -1;
int kpp = -1;
int k = iget(key);
while (k >= 0)
{
if (j < 0 && el.Equals(els[k]))
{
j = k;
}
kpp = kp;
kp = k;
k = nextsis[k];
}
if (j < 0)
{
return 0;
}
if (kpp < 0)
{
ierase(key);
nextsis[j] = -2;
}
else
{
if (j != kp)
{
els[j] = els[kp];
}
nextsis[kpp] = -1;
irelinquish(kp);
nextsis[kp] = -2;
}
return 1;
}
/// <summary>
/// Return the number of elements stored under key, 0 if none.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public int count(K key)
{
int next;
int n = 1;
if ((next = iget(key)) < 0)
{
return 0;
}
while ((next = nextsis[next]) >= 0)
{
n++;
}
return n;
}
/// <summary>
/// Initialize nextget so that it points to the first element stored under key.
/// Return 1 for success, or 0 if no such element.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public int getinit(K key)
{
nextget = iget(key);
return ((nextget < 0) ? 0 : 1);
}
/// <summary>
/// If nextget points validly, copy its element into el, update nextget to the
/// next element with the same key, and return 1. Otherwise, do not modify el,
/// and return 0.
/// </summary>
/// <param name="el"></param>
/// <returns></returns>
public int getnext(ref V el)
{
if (nextget < 0)
{
return 0;
}
el = els[nextget];
nextget = nextsis[nextget];
return 1;
}
/*
public override ulong fn(K k)
{
//return base.fn(k);
throw new NotImplementedException();
}
*/
}
}
2 代码格式
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer
{
public abstract class Mhash<K, V> : Hashtable<K>
{
private List<V> els { get; set; } = new List<V>();
/// <summary>
/// Links to next sister element under a single key.
/// </summary>
private int[] nextsis { get; set; }
private int nextget { get; set; }
public Mhash(int nh, int nm) : base(nh, nm)
{
nextget = -1;
els = new List<V>(nm);
for (int i = 0; i < nm; i++)
{
els.Add(default(V));
}
nextsis = new int[nm];
for (int j = 0; j < nm; j++)
{
nextsis[j] = -2;
}
}
/// <summary>
/// Store an element el under key.Return index in 0..nmax-1, giving the
/// storage location utilized.
/// </summary>
/// <param name="key"></param>
/// <param name="el"></param>
/// <returns></returns>
public int store(K key, V el)
{
int j = iset(key);
if (nextsis[j] == -2)
{
els[j] = el;
nextsis[j] = -1;
return j;
}
else
{
while (nextsis[j] != -1)
{
j = nextsis[j];
}
int k = ireserve();
els[k] = el;
nextsis[j] = k;
nextsis[k] = -1;
return k;
}
}
/// <summary>
/// Erase an element el previously stored under key.Return 1 for success, or 0
/// if no matching element is found.Note: The == operation must be defined for
/// the type V.
/// </summary>
/// <param name="key"></param>
/// <param name="el"></param>
/// <returns></returns>
public int erase(K key, V el)
{
int j = -1;
int kp = -1;
int kpp = -1;
int k = iget(key);
while (k >= 0)
{
if (j < 0 && el.Equals(els[k]))
{
j = k;
}
kpp = kp;
kp = k;
k = nextsis[k];
}
if (j < 0)
{
return 0;
}
if (kpp < 0)
{
ierase(key);
nextsis[j] = -2;
}
else
{
if (j != kp)
{
els[j] = els[kp];
}
nextsis[kpp] = -1;
irelinquish(kp);
nextsis[kp] = -2;
}
return 1;
}
/// <summary>
/// Return the number of elements stored under key, 0 if none.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public int count(K key)
{
int next;
int n = 1;
if ((next = iget(key)) < 0)
{
return 0;
}
while ((next = nextsis[next]) >= 0)
{
n++;
}
return n;
}
/// <summary>
/// Initialize nextget so that it points to the first element stored under key.
/// Return 1 for success, or 0 if no such element.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public int getinit(K key)
{
nextget = iget(key);
return ((nextget < 0) ? 0 : 1);
}
/// <summary>
/// If nextget points validly, copy its element into el, update nextget to the
/// next element with the same key, and return 1. Otherwise, do not modify el,
/// and return 0.
/// </summary>
/// <param name="el"></param>
/// <returns></returns>
public int getnext(ref V el)
{
if (nextget < 0)
{
return 0;
}
el = els[nextget];
nextget = nextsis[nextget];
return 1;
}
/*
public override ulong fn(K k)
{
//return base.fn(k);
throw new NotImplementedException();
}
*/
}
}