| 1 | %%% Copyright (C) Dominic Williams |
|---|
| 2 | %%% All rights reserved. |
|---|
| 3 | %%% See file COPYING. |
|---|
| 4 | |
|---|
| 5 | -module (adlib). |
|---|
| 6 | -export ([compare_dict/2]). |
|---|
| 7 | |
|---|
| 8 | compare_dict (Modified, Original) -> |
|---|
| 9 | Init_fun = fun (Key, Dict) -> dict: store (Key, dict: new (), Dict) end, |
|---|
| 10 | Init = lists: foldl (Init_fun, dict: new (), [lost, changed, same]), |
|---|
| 11 | {Intermediate, New} = dict: fold (fun compare_dict/3, {Init, Modified}, Original), |
|---|
| 12 | dict: store (new, New, Intermediate). |
|---|
| 13 | |
|---|
| 14 | compare_dict (Key, Value, {Result, Modified}) -> |
|---|
| 15 | compare_dict (dict: find (Key, Modified), Key, Value, Result, Modified). |
|---|
| 16 | |
|---|
| 17 | compare_dict (error, Key, Value, Result, Modified) -> |
|---|
| 18 | Lost = dict: store (Key, Value, dict: fetch (lost, Result)), |
|---|
| 19 | {dict: store (lost, Lost, Result), Modified}; |
|---|
| 20 | compare_dict ({ok, Value}, Key, Value, Result, Modified) -> |
|---|
| 21 | Same = dict: store (Key, Value, dict: fetch (same, Result)), |
|---|
| 22 | {dict: store (same, Same, Result), dict: erase (Key, Modified)}; |
|---|
| 23 | compare_dict ({ok, New_value}, Key, Value, Result, Modified) -> |
|---|
| 24 | Changed = dict: store (Key, {New_value, Value}, dict: fetch (changed, Result)), |
|---|
| 25 | {dict: store (changed, Changed, Result), dict: erase (Key, Modified)}. |
|---|