Thanks Kevin for your answers. If I understand it correctly, using
'Dictionary' cannot be the source of non-determinism.

If anyone is interested, the full implementation is below. The
procedures still do not check if the passed argument is really an
OrderedDictionary - an example/pointer of doing it using object-oriented
standard library interface to dictionaries would be helpful.

Kevin, I had to remove "import Dictionary" from
OrderedDictionary-Simple.oz, else it gave this runtime error:

%********************** Error: module manager *******************
%**
%** Could not link module
%**
%** Could not load functor at URL: ...omitted.../Dictionary.ozf
%**--------------------------------------------------------------

And if I had "import Dictionary at 'x-oz://system/adt/Dictionary'"
instead, the runtime error was:

%***************** Error: illegal field selection ***************
%**
%** In statement: 'export'(new:<P/1 New> newFromRecord:<P/2
NewFromRecord>) . is = _<optimized>
%** In statement: {<P/3 Value.'.'> 'export'(new:<P/1 New>
newFromRecord:<P/2 NewFromRecord>) is _}
%**
%** Call Stack:
%** procedure 'Default' in file "./OrderedDictionary.oz", line 61,
column 3, PC = 11203084
%** procedure in file "./OrderedDictionary.oz", line 13, column 0, PC =
11203224
%** procedure 'RootManager,Pickle/fast' in file
"d:/cygwin/home/bruni/Projects/Mozart/Sources/share
/lib/init/Module.oz", line 244, column 6, PC = 10976392
%** procedure in file
"/home/kost/compile/build-1.3.0/updates/mozart/share/lib/base/Base.oz",
line
92, column 7, PC = 11035332
%**--------------------------------------------------------------

Is this version safe when same OrderedDictionary is modified/accessed
from both parent and child spaces?


----------------Full implementation below------------------------------

functor

export
   New            
   Is                            
   IsEmpty        
   Put            
   Exchange       
   CondExchange   
   Get            
   CondGet        
   Keys           
   Entries        
   Items          
   Remove         
   RemoveAll      
   Clone          
   Member         
   ToRecord       
   Weak           

define
   Counter = {NewCell 0}

   fun {ByEntry _#(_#Count1) _#(_#Count2)}
      Count1 < Count2
   end

   fun {Default P}
      Dictionary.P
   end

   New         = {Default new}            
   Is          = {Default is}  
   IsEmpty     = {Default isEmpty}
   Remove      = {Default remove}
   RemoveAll   = {Default removeAll}
   Clone       = {Default clone}
   Member      = {Default member}
   Weak        = {Default weak}

   proc {Put D Key Item}
      NewSeq 
      _#OldSeq = {Dictionary.condExchange D Key _#unit $ Item#NewSeq}
   in
      if OldSeq == unit then NewCounter in
         %% Key is new, so set a new incremented value
         %% or, NewSeq = Counter := NewCounter
         {Cell.exchange Counter NewSeq NewCounter}
         NewCounter = NewSeq + 1
      else
         NewSeq = OldSeq
      end
   end

   fun {Get D Key}
      case {Dictionary.get D Key} of Item#_ then Item end
   end

   fun {Keys D}
      %% Return elements in insertion order
      {Map {Entries D} fun {$ Key#_} Key end}
   end

   fun {Items D}
      %% Return items in insertion order
      {Map {Entries D} fun {$ _#Item} Item end}
   end

   fun {Entries D}
      %% Return elements without the sequence #, but sorted by the
sequence #
      {Map {Sort {Dictionary.entries D} ByEntry}
                  fun {$ Key#(Element#_)} Key#Element end}
   end

   proc {Exchange D Key OldVal NewVal}
      case {Dictionary.get D Key}
      of OldVal#OldCount then
         {Dictionary.put D Key NewVal#OldCount}
      end
   end

   proc {CondExchange D Key DefVal OldVal NewVal}
      if {Dictionary.member D Key} == false then
         OldVal = DefVal
      else
         {Exchange D Key OldVal NewVal}
      end
   end

   fun {CondGet D Key DefVal}
      if {Dictionary.member D Key} == false then
         DefVal
      else
         {Get D Key}
      end
   end

   fun {ToRecord Label D}
      RecordWithCounts = {Dictionary.toRecord Label D}
   in
      {Record.map RecordWithCounts fun {$ Element#_} Element end}
   end

end

----------------Full implementation above------------------------------


--
Himanshu.



_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to