考慮以下情況:如何管理原始對象之間的數據依賴關係?
items = [
{
id: 1
attributes: [
{ key: a, value: 2 }
{ key: b, value: 3 }
],
requirements: null
}
{
id: 2
attributes: [
{ key: b, value: 2 }
],
requirements: a > 2
}
{
id: 3
attributes: [
{ key: a, value: 1 }
{ key: c, value: 1 }
],
requirements: a > 1 and b > 2
}
{
id: 4
attributes: [
{ key: a, value: 2 }
{ key: d, value: 7 }
],
requirements: b > 5 and h < 10
}
]
預期的結果,相加(總和)的各種attributes
是:在對象之間
result = [
{ key: a, value: 3 }
{ key: b, value: 5 }
{ key: c, value: 1 }
]
正如你可以看到,有依賴關係(requirements
)名單。特別是,因爲從不檢查條件b > 5 and h < 10
,所以id: 4
(系列中的最後一個)的對象從計算中被丟棄。相反,具有id: 2
的對象最初被丟棄,然後作爲id: 3
(通過將屬性a
加上1,使條件爲a > 2
)的對象的結果落入計算中。
獲得具有N個對象的所需結果所需的算法是什麼?
聲明:所提出的結構只是一個例子。您可以建議您相信實現結果的任何更改。我正在使用JavaScript(CoffeeScript)編程語言,但其他任何都可以。
您是否有時間或空間複雜性要求?顯而易見的解決方案是'O(n^2)' - 你放棄了這個選擇嗎?另外,你需要一些方法來存儲需求(當'items'被實例化時,你的當前代碼將評估它們爲布爾值,所以它們不能被重新檢查。你解決了這個問題,還是它也是你問題的一部分? – 2012-07-20 17:40:49
@AaronDufour對'需求'鍵的評估並不打算在對象實例化上進行,而是在每次被稱爲'sum'方法時進行;我認爲有必要在每個項目的需求之間建立某種聯繫條件和'sum'過程(一個事件驅動的方法?):每個對象都應該監聽數組中的變化(item add/remove),並且如果符合條件,則將其狀態更改爲'active = true'(或然後list對象再次執行'sum',最終導致其他對象激活,等等。無論如何,這只是一個想法。 – Jerus 2012-07-23 09:44:31