Chr*_*ice 10 c# struct data-structures
我正在为法语文本设计一个语言分析器.我有一个XML格式的字典,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Dictionary>
<!--This is the base structure for every entry in the dictionary. Values on attributes are given
as explanations for the attributes. Though this is the structure of the finished product for each word, definition, context and context examples will be ommitted as they don't have a real effect on the application at this moment. Defini-->
<Word word="The word in the dictionary (any word that would be defined)." aspirate="Whether or not the word starts with an aspirate h. Some adjectives that come before words that start with a non-aspirate h have an extra form (AdjectiveForms -> na [non-aspirate]).">
<GrammaticalForm form="The grammatical form of the word is the grammatical context in which it is used. Forms may consist of a word in noun, adjective, adverb, exclamatory or other form. Each form (generally) has its own definition, as the meaning of the word changes in the way it is used.">
<Definition definition=""></Definition>
</GrammaticalForm>
<ConjugationTables>
<NounForms ms="The masculin singular form of the noun." fs="The feminin singular form of the noun." mpl="The masculin plural form of the noun." fpl="The feminin plural form of the noun." gender="The gender of the noun. Determines"></NounForms>
<AdjectiveForms ms="The masculin singular form of the adjective." fs="The feminin singular form of the adjective." mpl="The masculin plural form of the adjective." fpl="The feminin plural form of the adjective." na="The non-aspirate form of the adjective, in the case where the adjective is followed by a non-aspirate word." location="Where the adjective is placed around the noun (before, after, or both)."></AdjectiveForms>
<VerbForms group="What group the verb belongs to (1st, 2nd, 3rd or exception)." auxillary="The auxillary verb taken by the verb." prepositions="A CSV list of valid prepositions this verb uses; for grammatical analysis." transitive="Whether or not the verb is transitive." pronominal="The pronominal infinitive form of the verb, if the verb allows pronominal construction.">
<Indicative>
<Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
<SimplePast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></SimplePast>
<PresentPerfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PresentPerfect>
<PastPerfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PastPerfect>
<Imperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Imperfect>
<Pluperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Pluperfect>
<Future fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Future>
<PastFuture fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></PastFuture>
</Indicative>
<Subjunctive>
<Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
<Past fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Past>
<Imperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Imperfect>
<Pluperfect fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Pluperfect>
</Subjunctive>
<Conditional>
<Present fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></Present>
<FirstPast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></FirstPast>
<SecondPast fps="(Je) first person singular." sps="(Tu) second person singular." tps="(Il) third person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural." tpp="(Ils) third person plural."></SecondPast>
</Conditional>
<Imperative>
<Present sps="(Tu) second person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural."></Present>
<Past sps="(Tu) second person singular." fpp="(Nous) first person plural." spp="(Vous) second person plural."></Past>
</Imperative>
<Infinitive present="The present infinitive form of the verb." past="The past infinitive form of the verb."></Infinitive>
<Participle present="The present participle of the verb." past="The past partciple of the verb."></Participle>
</VerbForms>
</ConjugationTables>
</Word>
</Dictionary>
Run Code Online (Sandbox Code Playgroud)
对不起,这很长,但有必要准确显示数据的建模方式(树节点结构).
目前我structs用来模拟共轭表,嵌套structs更具体.这是我创建的类,用于模拟XML文件中的单个条目:
class Word
{
public string word { get; set; }
public bool aspirate { get; set; }
public List<GrammaticalForms> forms { get; set; }
struct GrammaticalForms
{
public string form { get; set; }
public string definition { get; set; }
}
struct NounForms
{
public string gender { get; set; }
public string masculinSingular { get; set; }
public string femininSingular { get; set; }
public string masculinPlural { get; set; }
public string femininPlural { get; set; }
}
struct AdjectiveForms
{
public string masculinSingular { get; set; }
public string femininSingular { get; set; }
public string masculinPlural { get; set; }
public string femininPlural { get; set; }
public string nonAspirate { get; set; }
public string location { get; set; }
}
struct VerbForms
{
public string group { get; set; }
public string auxillary { get; set; }
public string[] prepositions { get; set; }
public bool transitive { get; set; }
public string pronominalForm { get; set; }
struct IndicativePresent
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativeSimplePast
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativePresentPerfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativePastPerfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativeImperfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativePluperfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativeFuture
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct IndicativePastFuture
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct SubjunctivePresent
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct SubjunctivePast
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct SubjunctiveImperfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct SubjunctivePluperfect
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct ConditionalPresent
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct ConditionalFirstPast
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct ConditionalSecondPast
{
public string firstPersonSingular { get; set; }
public string secondPersonSingular { get; set; }
public string thirdPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
public string thirdPersonPlural { get; set; }
}
struct ImperativePresent
{
public string secondPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
}
struct ImperativePast
{
public string secondPersonSingular { get; set; }
public string firstPersonPlural { get; set; }
public string secondPersonPlural { get; set; }
}
struct Infinitive
{
public string present { get; set; }
public string past { get; set; }
}
struct Participle
{
public string present { get; set; }
public string past { get; set; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是C#的新手,我不太熟悉数据结构.基于我对C++的有限知识,我知道structs在建模小型,高度相关的数据时很有用,这就是我目前以这种方式使用它们的原因.
所有这些结构都可以现实地制成共轭表 class,并且在很大程度上具有相同的结构.我不确定是将它们变成一个类,还是使用更适合该问题的不同数据结构.为了提供有关问题规范的更多信息,我将说明以下内容:
IndicativePresent必须嵌套在VerbForms; 这同样适用于作为结构成员的所有其他结构VerbForms.毕竟这些是共轭表!Word在XML文件中不具有GrammaticalForm的动词,没有VerbForms结构实际上会针对该项目的创建.这是为了提高效率 - 为什么实例化VerbForms这个词实际上不是动词?避免不必要地创建这些"表单"表(目前表示为struct XXXXXForms)的这种想法绝对是必要的.根据(主要)上述第4点,哪种数据结构最适合用于建模共轭表(不是数据库表)?我是否需要更改数据格式才能符合#4标准?如果我实例化a new Word,那么在当前状态下的结构是否会被实例化并占用大量空间?这是一些数学...在谷歌搜索并最终找到这个问题 ...
在所有的共轭表(名词,形容词,动词)中,总共有(巧合?)100 string分配,并且是空的.因此,如果创建了这些数据结构并且保持为空,则每个100 x 18字节= 1800字节Word,至少,(实际上将填充的值将始终存在至少一些开销).因此,假设(只是随机,可能或多或少)需要在内存中的50,000 Word秒,即9000万字节,或大约85.8307兆字节.
只有拥有空表才有很多开销.那么,什么是我可以把这个数据一起,让我以实例的方式只有某些表(名词,形容词,动词),这取决于什么GrammaticalForms的Word居然有(在XML文件).
我希望这些表是Word该类的成员,但只实例化我需要的表.我想不出办法解决它,现在我做了数学计算,structs我知道这不是一个好的解决方案.我首先想到的是让一个类为每种类型的NounForms,AdjectiveForms以及VerbForms,如果表单出现在XML文件中实例化的类.我不确定这是否正确...
有什么建议?
建议:
List<ConjugationForm> conjugationTable {get; set;}至于内存/GC压力,你实际测量过情况有多糟糕吗?我建议编写一些代码并实际测试以验证您是否会遇到 GC 问题,而不是尝试猜测或做出假设。GC 对于分配和取消分配大量小对象进行了很好的优化,但长期存在的和/或大对象可能会给您带来问题。