3

.net の専門家は何を推奨しますか? 2 つの異なるキーを使用してアクセスする必要があるデータがあります。データが次のようになっているとします。

key1
key2
data

いずれかのキーでデータを追加、検索し、いずれかのキーを何千回も使用してデータを削除する必要があり、非常に迅速にしたいと考えています。

LINQ がコードに追加する明快さは本当に気に入っていますが、ループ検索の状況で LINQ を辞書と比較しました。単一のデータを取得するのにはるかに時間がかかるように見えるため、LINQ は好きではありません。検索がとても速いので、私は辞書が好きです。

2 つの辞書を使用するカスタム クラスを作成することを考えていました。

key1
data

key2
data

クラスのインスタンスにデータ項目を追加するたびに、クラスはデータを 2 つの異なるバッキング ディクショナリに追加する必要があります。データ項目を削除するたびに、両方のバッキング ディクショナリからそれらを削除する必要があります。

これはこの問題を処理する最善の方法ですか、それとも .net に同じデータに対して 2 つのキーを持つことができる高速な「辞書のような」データ構造がありますか?

4

4 に答える 4

1

これはこの問題を処理する最善の方法ですか、それとも .net に同じデータに対して 2 つのキーを持つことができる高速な「辞書のような」データ構造がありますか?

いずれかのキーを使用できる 2 つのキーを提供する組み込みのデータ構造はありません。

両方の辞書を格納するオーバーヘッドが問題にならない限り、2 つのキーをカプセル化するカスタム クラスを使用することは理にかなっています。Dictionary(Of TKey, Of TValue)

于 2013-09-19T18:11:58.340 に答える
0

たとえば、複合キーを定義できます

Structure CKey
    Public Key1 As String
    Public Key2 As String
End Structure

そして、そのキーを使用する辞書

Dim myDict As New Dictionary(Of CKey, String)

myDict.Add(New CKey() With {.Key1 = "key01", .Key2 = "key02"}, "Data0102")
myDict.Add(New CKey() With {.Key1 = "key11", .Key2 = "key12"}, "Data0103")
myDict.Add(New CKey() With {.Key1 = "key01", .Key2 = "key22"}, "Data0104")
myDict.Add(New CKey() With {.Key1 = "key11", .Key2 = "key22"}, "Data0105")

次に、次のような単純な LINQ クエリを実行できます。

Dim result = From el In myDict Where el.Key.Key1 = "key01" Select el

LINQ が気に入らない場合でも、独自のメソッドで辞書をループして、自分でキーをチェックできます。

于 2013-09-19T18:49:01.563 に答える
0

ここで解決策が得られたことに本当に驚いています (私はこの種のプログラミングに慣れていないため)。

したがって、考慮すべき点が 2 つあります。検索に使用しているキーを把握し、エントリを追加および削除する必要があります。エントリを追加および削除するための共有サブシステム、またはクラスを使用して、これらのことを克服できると思います(それらについてはあまり知りません)。

誰かがそれを改善できるかもしれません。

私はしました、以下のセクションを見てください;-]

    Dim myDict As New Dictionary(Of String, Double)
    Dim myKeyDict As New Dictionary(Of String, String)
    myDict.Add("key1", 5.5)
    myKeyDict.Add("key2", "key1")

    ' Access Dictionary with key1
    myDict("key1") = 7.7
    Debug.Print(CStr(myDict("key1")))

    ' Acces Dictionary with key2
    Dim thisKey1 As String = myKeyDict("key2")  ' retrieve key1 with the help of key2
    Debug.Print(CStr(myDict(thisKey1)))         ' Acces the actuall dictionary with the retrieved key 1

そこで、key1 を文字列、key2 を文字列、val を double として、基本的な入力操作と出力操作を行う辞書のクラスを作成しました。

Public Class MultiKeyDictonaryDbl
    Public DictVal As New Dictionary(Of String, Double)         ' key1=Variablenname, val=Wert
    Public DictKey1 As New Dictionary(Of String, String)        ' key1=In/Output-Variablenname (Textdatei), key2=Variablenname
    Public DictKey2 As New Dictionary(Of String, String)        ' key2=In/Output-Variablenname (Textdatei), key1=Variablenname
    Public length As Integer
    Public Sub Add(key1 As String, key2 As String, val As Double)
        DictVal.Add(key1, val)
        DictKey1.Add(key1, key2)
        DictKey2.Add(key2, key1)
    End Sub
    Public Sub Remove(key As String, id As Integer)
        Dim key1 As String = Nothing
        Dim key2 As String = Nothing
        Dim chk As Boolean
        If id = 1 Then
            key1 = key
            chk = DictKey1.TryGetValue(key1, key2)
        ElseIf id = 2 Then
            key2 = key
            chk = DictKey2.TryGetValue(key2, key1)
        End If
        If chk = True Then
            DictVal.Remove(key1)
            DictKey1.Remove(key1)
            DictKey2.Remove(key2)
        End If
    End Sub
    Public Function getValue(key As String, id As Integer) As Double
        Dim key1 As String = Nothing
        Dim key2 As String = Nothing
        Dim chk As Boolean
        If id = 1 Then
            key1 = key : chk = True
        ElseIf id = 2 Then
            key2 = key
            chk = DictKey2.TryGetValue(key2, key1)
        End If
        If chk = True Then
            chk = DictVal.TryGetValue(key1, getValue)
        End If
        If chk = False Then getValue = Double.PositiveInfinity
    End Function
    Public Function getList() As String(,)
        Dim val As Double
        Dim key1 As String = Nothing
        Dim key2 As String = Nothing
        Dim i As Integer = -1
        ' getLength in one line of code
        length = -1 : Dim l1 As Integer = DictVal.Count : Dim l2 As Integer = DictKey1.Count : Dim l3 As Integer = DictKey2.Count : If l1 = l2 And l2 = l3 Then length = l1
        If length < 1 Then Exit Function
        Dim List(length - 1, 2) As String
        For Each ele In DictKey2
            i += 1
            key2 = ele.Key : key1 = DictKey2(key2) : val = DictVal(key1)
            List(i, 0) = key1 : List(i, 1) = key2 : List(i, 2) = CStr(val)
        Next
        getList = List
    End Function
    Public Function getLength() As Integer
        getLength = -1
        Dim l1 As Integer = DictVal.Count
        Dim l2 As Integer = DictKey1.Count
        Dim l3 As Integer = DictKey2.Count
        If l1 = l2 And l2 = l3 Then getLength = l1
        length = getLength
    End Function
End Class

Sub testDictionaryVariablenVerarbeitung()
    ' some tests
    Dim testit As New MultiKeyDictonaryDbl
    testit.Add("Variablenname", "In/Output-Variablenname", 55.7)
    testit.Add("Variablenname2", "In/Output-Variablenname2", 90.7)
    Debug.Print(CStr(testit.getLength()))
    testit.Add("Blub", "dabdi", 916)
    testit.Remove("Variablenname", 1)

    Dim liste(,) As String = testit.getList
    Debug.Print(CStr(testit.getValue("Variablenname2", 1)))
    Debug.Print(CStr(testit.getValue("dabdi", 2)))
    Debug.Print(CStr(testit.getValue("dabdi", 1)))

End Sub
于 2015-11-17T16:19:04.767 に答える
0

少し遅れましたが、次のように key1 と key 2 を連結してはどうでしょうか。

key1key2
data

これにより、手間をかけずにいつでもデータを取得できます。次のように追加できます。

 Public DictVal As New Dictionary(Of String, String)

  DictVal.Add(key1 & key2, value)
于 2020-10-13T15:22:12.227 に答える