0

我正在编写比较两组二进制数据之间差异的软件。二进制数据包含坐标,它还从 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。速度损失很大,以至于我的应用程序仅适用于比较数据之间的微小差异。

但是,我需要使用列表,因为您不能在哈希集中有重复项,但是列表只是减慢速度的一种方式。不能使用字典,因为键必须是唯一的

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

我当前的比较代码

    //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;

}

4

1 回答 1

0

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

我的程序从检测 3,200 条删除记录的 30 分钟缩短到了 7 秒。

有关更多信息,请参阅此问题:

字符串与 StringBuilder

于 2013-07-19T17:47:41.267 回答