使用相同的哈希码和类似数据比较两个HashSets / Data列表

时间:2013-07-18 18:17:38

标签: c# asp.net binaryfiles arcgis

我正在编写比较两组二进制数据之间差异的软件。二进制数据包含坐标,它还从DBF文件中读取以获取有关每个对象的属性信息。我的软件指示对象是否已移动,如果其属性已更改,则删除/添加了对象。

我的自定义对象哈希码是根据每个实例中的坐标列表生成的。 每个对象中还有一个数据行。当我最初尝试找到“不完美的记录”(基本上任何基于坐标和数据路径没有匹配的对象)时,它会考虑数据行,并且我可以使用HashSet,因为数据行使其足够独特。

public override int GetHashCode()
    {

        if (considerAttrs)
        {
            return (value.GetHashCode() + dbString.GetHashCode());
        }
        else
        {
            return value.GetHashCode();
        }

    }

我在哪里:

此时软件返回没有完美匹配的对象(没有完全匹配的坐标和属性)。这个数据是正确的(至少我希望)

origUnfoundCount:114 modUnfoundCount:223

在原版中找不到114,因为113已修改属性,1已被移动。它在修改后找不到223,因为有113个修改后的属性,1个被移动,109个被添加。

我被困的地方:

该软件可以很好地处理较小的数据(每个列表中有几百个)

我将每个对象中的ConsideAttrs更改为false并使用List而不是HashSet。速度惩罚非常严重,我的应用程序只能用于比较数据之间的微小差异。

我需要使用List,因为你不能在hashset中有重复项,但是列表只是放慢速度的方法。由于密钥必须是唯一的

,因此无法使用字典

我需要一种新方法,下面的逻辑应该为您提供我需要它做的一般要点。

我目前的比较代码

    //ignored modified records
    HashSet<int> ignoredRecNo = new HashSet<int>();
    //ignoring moved records
    HashSet<String> ignoredDBstrings = new HashSet<string>();

    HashSet<String> columnNames = new HashSet<string>();
    HashSet<int> modModdedRNs = new HashSet<int>();
    columnNames = (HashSet<String>)HttpContext.Current.Session["columnNames"];

    List<PolyLineZ> originalNFs = new List<PolyLineZ>();
    List<PolyLineZ> modifiedNFs = new List<PolyLineZ>();

    List<PolyLineZ> removedList = new List<PolyLineZ>();
    List<PolyLineZ> movedList = new List<PolyLineZ>();
    List<PolyLineZ> modifiedList = new List<PolyLineZ>();
    List<PolyLineZ> modifiedMatchList = new List<PolyLineZ>();

    List<PolyLineZ> movedOrDeleted = new List<PolyLineZ>();

    if (HttpContext.Current.Session["origNFList"] != null)
    {
        origPolyLineZNFList = (HashSet<PolyLineZ>)HttpContext.Current.Session["origNFList"];
    }

    if (HttpContext.Current.Session["modNFList"] != null)
    {
        modPolyLineZNFList = (HashSet<PolyLineZ>)HttpContext.Current.Session["modNFList"];
    }


    //----------Generate Lists of string for each row---------------//
    foreach (PolyLineZ polyLineZ in origPolyLineZNFList)
    {
        origDBStrings.Add(polyLineZ.dbString);
        PolyLineZ temp = new PolyLineZ();
        temp = polyLineZ;
        temp.considerAttrs = false;
        originalNFs.Add(temp);
    }

    foreach (PolyLineZ polyLineZ in modPolyLineZNFList)
    {
        modDBStrings.Add(polyLineZ.dbString);
        PolyLineZ temp = new PolyLineZ();
        temp = polyLineZ;
        temp.considerAttrs = false;
        modifiedNFs.Add(temp);
    }


    foreach (PolyLineZ modpolyLineZ in modifiedNFs)
    {
        bool foundAmatch = false;
        foreach (PolyLineZ origPolyLineZ in originalNFs)
        {
            if (origPolyLineZ.Equals(modpolyLineZ))
            {
                if (!modDBStrings.Contains(origPolyLineZ.dbString))
                {
                    //database modifications are in here                        
                    modModdedRNs.Add(origPolyLineZ.RecordNumber);
                    foundAmatch = true;
                    break;
                }
            }
        }

    }

    foreach (PolyLineZ polyLineZ in originalNFs)
    {
        bool foundAmatch = false;
        foreach (PolyLineZ modpolyLineZ in modifiedNFs)
        {
            if (foundAmatch)
            {
                break;
            }
            if (modpolyLineZ.Equals(polyLineZ))
            {
                if (!origDBStrings.Contains(modpolyLineZ.dbString))
                {

                    foundAmatch = true;
                    //database modifications are in here                            
                    ignoredRecNo.Add(modpolyLineZ.RecordNumber);
                    ignoredDBstrings.Add(modpolyLineZ.dbString);
                    modifiedList.Add(polyLineZ);
                    modifiedMatchList.Add(modpolyLineZ);
                    break;

                } // end db string comparison

            } //end shape equals if

        } //end modNF loop

        if (!foundAmatch)
        {
            movedOrDeleted.Add(polyLineZ);
            ignoredDBstrings.Add(polyLineZ.dbString);
            ignoredRecNo.Add(polyLineZ.RecordNumber);
        }

    } //end origNF loop


    result += "movedDeletedCount: " + movedOrDeleted.Count + "<br/>";
    foreach (PolyLineZ polylineZ in movedOrDeleted)
    {

        if (!modDBStrings.Contains(polylineZ.dbString))
        {
                removedList.Add(polylineZ);
        }
        else
        {

                movedList.Add(polylineZ);   
        }
    }

    /*************************** ITERATE DATABASE CHANGES***********************************/
    for(int i=0; i < modifiedList.Count;i++)
    {
        if (modModdedRNs.Contains(modifiedList[i].RecordNumber))
        {
            if (modifiedAttrs < 1001)
            {
                //database modifications are in here                            
                ignoredRecNo.Add(modifiedMatchList[i].RecordNumber);
                ignoredDBstrings.Add(modifiedMatchList[i].dbString);
                modifiedAttrs++;
                modifiedResults += "<div class='turnBlue'>";
                //show where the change was made at
                modifiedResults += "Change Detected at original FID# " + (modifiedList[i].RecordNumber - 1) + " and modified FID#";
                HashSet<String> mismatchedColumns = new HashSet<String>();
                modifiedResults += (modifiedMatchList[i].RecordNumber - 1);
                modifiedResults += "</div>"; //end turnblue div
                DataRow origRow = modifiedList[i].datarow;
                DataRow modRow = modifiedMatchList[i].datarow;
                foreach (String columnName in columnNames)
                {
                    String origRowValue = "" + origRow.Field<Object>(columnName);
                    String modRowValue = "" + modRow.Field<Object>(columnName);
                    if (!modRowValue.Equals(origRowValue))
                    {
                        mismatchedColumns.Add(columnName);

                    }
                }
                foreach (String mismatchedColumn in mismatchedColumns)
                {

                    //grab original attr value
                    String origMismatchedRowValue = "" + origRow.Field<Object>(mismatchedColumn);
                    //grab the modified value
                    String modMismatchedRowValue = "" + modRow.Field<Object>(mismatchedColumn);
                    //generate a heading, letting the user know about the situation
                    modifiedResults += "<div class='turnBlue'>Value at Column: &nbsp;<b>" + mismatchedColumn + "</b> has been modified<br/>";
                    modifiedResults += "<div class='pushLeft'>Original value: &nbsp;<b>" + origMismatchedRowValue + "</b><br/></div>";
                    modifiedResults += "<div class='pushLeft'>Modified value: &nbsp;<b>" + modMismatchedRowValue + "</b><br/></div>";
                    modifiedResults += "</div>"; //end modified div 
                }
            }
            else
            {
                modifiedAttrs++;
            }


        }
        else
        {
            if (removed < 1001)
            {
                //iterate removed data here
                removed++;
            }
            else
            {
                removed++;
            }

        }
    }
    //****************************this determines which ones have been added ***************************/

    foreach (PolyLineZ modpolyLineZ in modifiedNFs)
    {
        if (!ignoredRecNo.Contains(modpolyLineZ.RecordNumber) && (!ignoredDBstrings.Contains(modpolyLineZ.dbString)))
        {
            //iterate added data here
        }

    }

    foreach (PolyLineZ polylineZ in removedList)
    {
        //iterate removed data here
    }
    foreach (PolyLineZ polylineZ in movedList)
    {
        //iterate  moved data here

    }

    result += "<div id='addedJump'></div>" + addedResult;
    result += "<div id='moddedJump'></div>" + modifiedResults;
    result += "<div id='removedJump'></div>" + removedResults;
    result += "<div id='movedJump'></div>" + movedResults;

}

1 个答案:

答案 0 :(得分:0)

我从将所有数据附加到单个字符串切换到使用stringbuilder ...

我的程序从30分钟开始,将3,200个已删除的记录检测到7秒钟。

有关详细信息,请参阅此问题:

String vs. StringBuilder

相关问题