2013-10-17 35 views
1

我在考慮如何在數據結構中表示函數依賴關係。代表函數依賴關係的數據結構

功能降級(數據庫的關係模式)將一組屬性映射到一組屬性。例如。在{A,B} - > {C,d}的屬性C和d是有功能的依賴於A和B.

我能想到的四種可能的情況這裏:

  1. {A} - > {B}(兩個單一屬性)
  2. {A,B} - > {C}(一組屬性意味着一個屬性)
  3. {A} - > {B,C}屬性集合)
  4. {A,B} - > {C,D}(一組屬性意味着一組屬性)

我的第一種方法是簡單的兩個成員等級:

class attribute 
{ 
    string name; 
    set<attribute*> dependent_on; 
} 

這與像(1),但與(2)函數依賴工作 - (4) - 我想。 實際上,我可以分解(3),但我無法用這樣的類來表示(2)和(4)。

我要保持的信息是c d是有功能的依賴於 B,所以我將不得不使一個類似attributegroup其中一組屬性被映射到一組屬性。例如: -

class attributegroup 
{ 
    set<attribute*> members; 
    set<attribute*> dependent_on; 
} 

因此,實際上我可以簡單地代表一個單獨的屬性爲attributegroup只有一個成員。但我不認爲這是做這件事的最好方法。

任何幫助/想法讚賞:)

+0

對於您建議的方法,您有哪些具體問題? –

+0

我沒有特別的問題。我只是覺得可能會有更好的一個。通過我的方法,這將產生一個'set ',這就像@DieterLücking提供的方法一樣,它實現了必須迭代所有'attributegroup'的算法。 –

+1

好的 - 下一個問題:我們要做什麼而不必檢查所有'atributegroup's?我們需要知道後面想要實現的算法,以便說明啓用它們的數據結構應該是什麼樣子。 –

回答

1

我不會存儲在依賴屬性:

include <iostream> 
#include <map> 
#include <vector> 

struct Attribute; 
typedef std::vector<Attribute> Attributes; 
struct Attribute { 
    char name; 
    operator Attributes() const { return Attributes{ 1, *this }; } 
}; 

inline bool operator < (const Attribute& a, const Attribute& b) { 
    return a.name < b.name; 
} 

inline bool operator < (const Attributes& a, const Attributes& b) { 
    return std::lexicographical_compare(
     a.begin(), a.end(), 
     b.begin(), b.end()); 
} 

inline std::ostream& operator << (std::ostream& stream, const Attributes& attributes) { 
    for(const auto& a: attributes) { 
     std::cout << a.name; 
    } 
    return stream; 
} 

typedef std::multimap<Attributes, Attributes> AttributeDependencies; 
typedef AttributeDependencies::value_type AttributeDependency; 

int main(int argc, const char* argv[]) { 
    Attribute a {'A'}; 
    Attribute b {'B'}; 
    Attribute c {'C'}; 
    Attribute d {'D'}; 

    AttributeDependencies dpendencies; 
    dpendencies.insert(AttributeDependency(a, b)); 
    dpendencies.insert(AttributeDependency(Attributes{a, b}, c)); 
    dpendencies.insert(AttributeDependency(a, Attributes{b, c})); 
    dpendencies.insert(AttributeDependency(Attributes{a, b}, Attributes{c, d})); 
    for(const auto& d: dpendencies) { 
     std::cout << '{' << d.first << "} -> {" << d.second << "}\n"; 
    } 
    return 0; 
} 

注:一個std ::地圖可能是正確的容器,但標準:: multimap中配合示例數據。

+0

那麼,其實我正在考慮這種實現(雖然我寧願將std :: set設置爲std :: vector以避免重複)。但是當我需要循環遍歷所有的依賴關係(將會是O(n^2)還是我缺少某些東西?)時,我對運行時有一些擔心。 所以我希望有一些「怪異」的數據結構,大多數人都不會想到。 –