Difference between revisions of "Sorted Dictionary Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 28: Line 28:
 
=== structure TypeHolder ===
 
=== structure TypeHolder ===
 
Change the definition of <nowiki>type (''k,'v) dictionary</nowiki> to support SortedDictionary.
 
Change the definition of <nowiki>type (''k,'v) dictionary</nowiki> to support SortedDictionary.
 +
 +
Recall that BinarySearchTree.tree has two generic types <nowiki>'a</nowiki> and <nowiki>'k</nowiki>. 
  
 
  <nowiki>structure TypeHolder = struct
 
  <nowiki>structure TypeHolder = struct
Line 33: Line 35:
 
type (''k,'v) dictionary = unit
 
type (''k,'v) dictionary = unit
 
end</nowiki>
 
end</nowiki>
 +
 +
NOTE: the sorted dictionary is mutable.
  
 
=== structure SortedHasEntries ===
 
=== structure SortedHasEntries ===
Line 47: Line 51:
 
  <nowiki>fun create(cmp : ''k compare_function) : (''k,'v) dictionary =  
 
  <nowiki>fun create(cmp : ''k compare_function) : (''k,'v) dictionary =  
 
raise Fail "NotYetImplemented"</nowiki>
 
raise Fail "NotYetImplemented"</nowiki>
 
+
==== get ====
===type <nowiki>(''k,'v)</nowiki> dictionary===  
+
A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.
We recall that BinarySearchTree.tree has two generic types <code>'a</code> and <code>'k</code>. 
+
==== put ====
 
+
A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.
<nowiki>type ('a,'k) tree</nowiki>
+
==== remove ====
 
+
A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.
<code>'a</code> represents the type of the contents of each node.  <code>'k</code> represents the type of the key for each node.
 
 
 
To implement a <nowiki>(''k,'v) SortedDictionary.dictionary</nowiki> with a BinarySearchTree.tree, what is the type of the contents at each node?
 
 
 
What is the type of the key for your BinarySearchTree.tree?
 
 
 
The answers to these questions will be instrumental in defining your type in the SortedDictionary structure:
 
 
 
<nowiki>(* TODO: replace unit with the type you decide upon *)
 
type (''k,'v) dictionary = unit</nowiki>
 
 
 
NOTE: the sorted dictionary is mutable.
 
 
 
===create===
 
<nowiki>fun create(cmp : ''k compare_function) : (''k,'v) dictionary
 
    raise NotYetImplemented</nowiki>
 
  
 
=Testing=
 
=Testing=

Revision as of 14:03, 2 March 2022

Background

ref

How to use ref

dictionary

Dictionary

Code To Use

Binary Search Tree

You will undoubtedly want to use the tree you implemented in the Binary Search Tree assignment.

HasEntriesFn

The structure SortedDictionary has been setup to re-use functor HasEntriesFn from the Chained Dictionary exercise.

Code To Implement

structure SortedDictionary

SortedDictionary must:

  • have O(lg N) expected performance for find, insert, and remove.
  • produce entries and keys in sorted order based on the provided compare_function.
  • produce values in the sorted order of each value's key based on the provided compare_function.
structure SortedDictionary :> SORTED_DICTIONARY
 <nowiki>signature SORTED_DICTIONARY = sig include DICTIONARY
    type ''k compare_function = (''k*''k) -> order
    val create : ''k compare_function -> (''k,'v) dictionary
end

structure TypeHolder

Change the definition of type (''k,'v) dictionary to support SortedDictionary.

Recall that BinarySearchTree.tree has two generic types 'a and 'k.

structure TypeHolder = struct
	(* TODO: replace unit with the type you decide upon *)
	type (''k,'v) dictionary = unit
end

NOTE: the sorted dictionary is mutable.

structure SortedHasEntries

structure SortedHasEntries = HasEntriesFn (struct
	type (''k,'v) dictionary = (''k,'v) TypeHolder.dictionary
	fun entries(dict : (''k,'v) dictionary) : (''k*'v) list =
		raise Fail "NotYetImplemented"
end)

entries

Define the entries method so we can take advantage of the keys and values functions you implemented on functor HasEntriesFn.

functions

create

fun create(cmp : ''k compare_function) : (''k,'v) dictionary = 
	raise Fail "NotYetImplemented"

get

A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.

put

A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.

remove

A function from [[Binary_Search_Tree_Assignment#Code_to_Implement|BinarySearchTree] should be useful here.

Testing

file: sml run_unit_test_sorted_dictionary.sml

in folder: src/test/sml/dictionary/sorted

Pledge, Acknowledgments, Citations

file: studio-sorted-dictionary-pledge-acknowledgments-citations.txt

More info about the Honor Pledge