3

我有一个需要使用 TCP 通过网络发送的对象。我的工作正常,但我现在已经扩展了我的实现,并且在确定如何定义我的FlatBuffer模式时遇到了麻烦。

要发送的对象是这样的:

public class Prediction
{
    public PredictionMethod PredictionMethod { get; set; }
    public NGramPrediction NGramPrediction { get; set; }
    public DistancePrediction DistancePrediction { get; set; }
    public int NGramOrder { get; set; }
}

PredictionMethod是一个枚举:

public enum PredictionMethod
{
    Distance = 1,
    NGram = 2,
}

NGramPrediction看起来像这样:

 public class NGramPrediction
{
    public KeyValuePair<char, int> Gram { get; set; }
    public double Probability { get; set; }
    public string Pattern { get; set; }
    private int Total { get; set; }
    private int Order { get; set; }

    public NGramPrediction(Gram gram)
    {
        Total = gram.Total();
        var orderedKeyPosibilities = gram.Posibilities.OrderByDescending(x => x.Value);

        Gram = orderedKeyPosibilities.First();

        Pattern = gram.Pattern;
        Probability = (double)Gram.Value / Total;

        Order = Pattern.Length;
    }
}

看起来像这样:

public class Gram
{
    public string Pattern { get; set; }

    public Dictionary<char, int> Posibilities { get; set; }

    public Gram(List<char> posibilities, int initialValue = 0)
    {
        Posibilities = new Dictionary<char, int>();
        foreach (var posibility in posibilities)
        {
            Posibilities.Add(posibility, initialValue);
        }
    }

    public int Total()
    {
        var keys = Posibilities.Keys;
        var total = 0;
        foreach (var key in keys)
        {
            var value = Posibilities[key];
            total += value;
        }
        return total;
        // return keys.Sum(key => Posibilities[key]);
    }

}

距离预测如下所示:

public class DistancePrediction
{
    public IIRVector3 Velocity { get; set; }
    public float DeltaTime { get; set; }
    public IIRVector3 Position { get; set; }
}

最后,IIRVector3看起来像这样:

public class IIRVector3
{
    public IIRVector3()
    {
        X = 0;
        Y = 0;
        Z = 0;
    }
    public float X { get; set; }
    public float Y { get; set; }
    public float Z { get; set; }
}

我正在尝试为 FlatBuffers 定义我的架构:

enum FBPredictionMethod
{
    Distance = 1,
    NGram = 2
}

struct FBIIRVector3
{
    X:float;
    Y:float;
    Z:float;
}

table FBDistancePrediction
{
    Velocity:FBIIRVector3;
    DeltaTime:float;
    Position:FBIIRVector3;
}

table FBGram
{
    Pattern:string;
    Possibilities: ????
}

table FBNGramPrediction
{
   Gram: ????
   Probability:float;
   Pattern:string;
   Total:short;
   Order:short;
}

table FBPrediction
{
    PredictionMethod:FBPredictionMethod;
    NGramPrediction:FBNGramPrediction;
    DistancePrediction:FBDistancePrediction;
    NGramOrder:short;
}

root_type FlatServerToClientMessage;

我认为一切看起来都是正确的,但我不知道该为字典做什么......

FBGram.Possibilities应该是 aDictionary<char, int>并且FBGram应该是KeyValuePair<char, int>

注意: Gram 的构造函数以 aList<char>和 anint作为参数,而 for 的构造函数NGramPrediction以 aGram作为参数。

有人可以帮我解决我的架构吗?

4

1 回答 1

3

应该很容易..要么将KeyValuePair它们存储为两个单独的字段,要么使用struct KeyValuePair { key:byte; value:int; }

Dictionary可以存储为Possibilities:[KeyValuePair](使用相同的结构)。如果你想节省空间,如果Possibilities可以很大,那么PossibilityKeys:[byte]并且PossibilityValues:[int]会更紧凑,但更难读/写。

于 2016-11-29T17:43:22.003 回答