C#、数値計算 - ハッシュテーブルの計算方法とソースプログラム

 

1 テキスト形式

システムを使用する;
System.Collections を使用します。
System.Collections.Generic を使用します。

namespace Legalsoft.Truffer
{     public abstract class Hashtable<K>     {         private int nhash { get; セット; プライベート         int nmax {取得; セット; プライベート         int nn {取得; セット; プライベート         int ng { 取得; セット; プライベート         int[] htable {取得; セット; プライベート         int[] next {get; セット; プライベート         int[] garbg {取得; セット; プライベート         ulong[] thehash {取得; セット; }









        public virtual ulong fn(K k) { return 0L; }

        public Hashtable(int nh, int nm)
        {             nhash = nh;             nmax = nm;             nn = 0;             ng = 0;             htable = 新しい int[nh];             次 = 新しい int[nm];             garbg = 新しい int[nm];             thehash = 新しい ulong[nm];             for (int j = 0; j < nh; j++)             {                 htable[j] = -1;             }         }












        public int iget(K key)
        {             //head pp = hash.fn(key);             //head pp = new Hashfn1(nhash).fn(key);             ヘッド pp = fn(キー);


            int j = (int)(pp % (ulong)nhash);
            for (int k = htable[j]; k != -1; k = next[k])
            {                 if (thehash[k] == pp)                 {                     return k;                 -1             を             返します。         }






        public int iset(K key)
        {             int kprev = 0;             //head pp = new Hashfn1(nhash).fn(key);             ヘッド pp = fn(キー);             int j = (int)(pp % (head)nhash);             int k;             if (htable[j] == -1)             {                 k = > 0 ? garbg[--ng] : nn++;                 htable[j] = k;             }             else             {                 k = htable[j];                 for (; k != -1; k = next[k])                 {                     if (thehash[k] == pp)                     {                         return k;


















                    kprev
                    = k;
                k
                = ng > 0 ? garbg[--ng] : nn++;
                次[kprev] = k;             if (k >= nmax)             {                 throw new Exception("格納されている値が多すぎます")
            ;             ハッシュ             [k] = pp;             次[k] = -1;             k を返します。         }








        public int ierase(K key)
        {             //ulong pp = new Hashfn1(nhash).fn(key);             ulong pp = fn(キー);             int j = (int)(pp % (ulong)nhash);             if (htable[j] == -1)             {                 return -1;             int             kprev = -1;             for (int k = htable[j]; k != -1; k = next[k])             {                 if (thehash[k] == pp)                 {                     if (kprev == -1)                     {                         htable[j] = next [k];                     その他                     _

















                    {                         next[kprev] = next[k];                     garbg                     [ng++] = k;                     k を返します。                 kprev                 = k;             -1             を返します。         }








        public int ireserve()
        {             int k = ng > 0 ? garbg[--ng] : nn++;             if (k >= nmax)             {                 throw new Exception("予約されている値が多すぎます");             next             [k] = -2;             k を返します。         }







        public int irelinquish (int k)
        {             if (next [k] ! = - 2)             {                 return - 1 ;             garbg             [ng++] = k;             k を返します。         } }     }









 

2コード形式

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer
{
    public abstract class Hashtable<K>
    {
        private int nhash { get; set; }
        private int nmax { get; set; }
        private int nn { get; set; }
        private int ng { get; set; }
        private int[] htable { get; set; }
        private int[] next { get; set; }
        private int[] garbg { get; set; }
        private ulong[] thehash { get; set; }

        public virtual ulong fn(K k) { return 0L; }

        public Hashtable(int nh, int nm)
        {
            nhash = nh;
            nmax = nm;
            nn = 0;
            ng = 0;
            htable = new int[nh];
            next = new int[nm];
            garbg = new int[nm];
            thehash = new ulong[nm];
            for (int j = 0; j < nh; j++)
            {
                htable[j] = -1;
            }
        }

        public int iget(K key)
        {
            //ulong pp = hash.fn(key);
            //ulong pp = new Hashfn1(nhash).fn(key);
            ulong pp = fn(key);

            int j = (int)(pp % (ulong)nhash);
            for (int k = htable[j]; k != -1; k = next[k])
            {
                if (thehash[k] == pp)
                {
                    return k;
                }
            }
            return -1;
        }

        public int iset(K key)
        {
            int kprev = 0;
            //ulong pp = new Hashfn1(nhash).fn(key);
            ulong pp = fn(key);
            int j = (int)(pp % (ulong)nhash);
            int k;
            if (htable[j] == -1)
            {
                k = ng > 0 ? garbg[--ng] : nn++;
                htable[j] = k;
            }
            else
            {
                k = htable[j];
                for (; k != -1; k = next[k])
                {
                    if (thehash[k] == pp)
                    {
                        return k;
                    }
                    kprev = k;
                }
                k = ng > 0 ? garbg[--ng] : nn++;
                next[kprev] = k;
            }
            if (k >= nmax)
            {
                throw new Exception("storing too many values");
            }
            thehash[k] = pp;
            next[k] = -1;
            return k;
        }


        public int ierase(K key)
        {
            //ulong pp = new Hashfn1(nhash).fn(key);
            ulong pp = fn(key);
            int j = (int)(pp % (ulong)nhash);
            if (htable[j] == -1)
            {
                return -1;
            }
            int kprev = -1;
            for (int k = htable[j]; k != -1; k = next[k])
            {
                if (thehash[k] == pp)
                {
                    if (kprev == -1)
                    {
                        htable[j] = next[k];
                    }
                    else
                    {
                        next[kprev] = next[k];
                    }
                    garbg[ng++] = k;
                    return k;
                }
                kprev = k;
            }
            return -1;
        }

        public int ireserve()
        {
            int k = ng > 0 ? garbg[--ng] : nn++;
            if (k >= nmax)
            {
                throw new Exception("reserving too many values");
            }
            next[k] = -2;
            return k;
        }

        public int irelinquish(int k)
        {
            if (next[k] != -2)
            {
                return -1;
            }
            garbg[ng++] = k;
            return k;
        }
    }
}

おすすめ

転載: blog.csdn.net/beijinghorn/article/details/132982502