Commit 841a3fd8 authored by xx1219's avatar xx1219
Browse files

xx1219: add libs Set and Map with corresponding tests

parent 7b632445
......@@ -73,7 +73,7 @@ public class PreCompiler {
str = str.split(commentMark, 2)[0];
if (str.contains(defineRuleContext)) {
/* process and store the macros info */
String[] macro = str.split(" ");
String[] macro = str.split(" ", 3);
if (macro.length != 3 || macro[0].length() != defineRuleContext.length() - 1) {
mediateFile.delete();
System.out.println("Invalid macro at line " + lineCounter);
......@@ -158,7 +158,7 @@ public class PreCompiler {
if (str.contains(defineRuleContext)) {
/* get the lib's macro info, assume macros in stdlib are all valid */
String[] macro = str.split(" ");
String[] macro = str.split(" ", 3);
macros.add(new MacroInfo(macro[1], macro[2]));
} else {
bw.write(" " + str + "\n"); /* copy the content */
......
define a b c
begin
skip
end
\ No newline at end of file
include Map<int,string>
begin
bool equals(int a, int b) is return a == b end
Map<int,string> map = new Map<int,string>();
bool isFreshKey = call put(map, 1, "one");
isFreshKey = call put(map, 2, "two");
bool hasOne = call containsKey(map, 1);
println hasOne;
bool hasTwo = call containsKey(map, 2);
println hasTwo;
bool hasThree = call containsKey(map, 3);
println hasThree;
string one = call getOrDefault(map, 1, "not found");
string two = call getOrDefault(map, 2, "not found");
string three = call getOrDefault(map, 3, "not found");
println one;
println two;
println three;
isFreshKey = call put(map, 2, "three");
three = call getOrDefault(map, 2, "not found");
println three
end
\ No newline at end of file
include Set<int>
begin
bool equals(int a, int b) is return a == b end
Set<int> set = new Set<int>();
int size = call size(set);
println size;
bool output = false;
for (int i = 1; i <= 5 ; i = i + 1) do
output = call add(set, i)
done;
size = call size(set);
println size;
for (int i = 1; i <= 5 ; i = i + 1) do
output = call add(set, i)
done;
size = call size(set);
println size;
output = call remove(set, 6);
size = call size(set);
println size;
for (int i = 1; i <= 5 ; i = i + 1) do
output = call remove(set, i)
done;
size = call size(set);
println size
end
\ No newline at end of file
define PRINT println "This is a test"
begin
PRINT
end
\ No newline at end of file
......@@ -32,6 +32,7 @@ bool add(List<E> list, E val, int pos) is
Node<E> next = list.root;
node.next = next;
list.root = node;
list.length = list.length + 1;
return true
else
if (list.root == empty) then
......@@ -51,6 +52,7 @@ bool add(List<E> list, E val, int pos) is
done;
cur.next = node;
node.next = next;
list.length = list.length + 1;
return true
fi
fi
......@@ -64,6 +66,7 @@ bool remove(List<E> list, int pos) is
Node<E> next = list.root.next;
free list.root;
list.root = next;
list.length = list.length - 1;
return true
else
if (list.root.next == empty) then
......@@ -83,6 +86,7 @@ bool remove(List<E> list, int pos) is
done;
cur.next = next.next;
free next;
list.length = list.length - 1;
return true
fi
fi
......
define Map<E1,E2>() Map<E1,E2>{empty}
define Map<E1,E2> Map_E1_E2
define Entry<E1,E2> Entry_E1_E2
struct Map<E1,E2> is {Entry<E1,E2> root}
struct Entry<E1,E2> is {E1 key, E2 val, Entry<E1,E2> next}
bool put(Map<E1,E2> map, E1 key, E2 val) is
if (map.root == empty) then
map.root = new Entry<E1,E2>{key, val, empty};
return true
else
Entry<E1,E2> cur = map.root;
while (cur.next != empty) do
bool equal = call equals(cur.key, key);
if (equal) then
cur.val = val;
return false
else
cur = cur.next
fi
done;
bool equal = call equals(cur.key, key);
if (equal) then
cur.val = val;
return false
else
cur.next = new Entry<E1,E2>{key, val, empty};
return true
fi
fi
end
bool containsKey(Map<E1,E2> map, E1 key) is
if (map.root == empty) then
return false
else
Entry<E1,E2> e = map.root;
while (e != empty) do
bool equal = call equals(e.key, key);
if (equal) then
return true
else
e = e.next
fi
done;
return false
fi
end
E2 getOrDefault(Map<E1,E2> map, E1 key, E2 val) is
if (map.root == empty) then
return val
else
Entry<E1,E2> cur = map.root;
while (cur != empty) do
bool equal = call equals(cur.key, key);
if (equal) then
return cur.val
else
cur = cur.next
fi
done;
return val
fi
end
bool destroy(Map<E1,E2> map) is
Entry<E1,E2> cur = map.root;
while (cur != empty) do
Entry<E1,E2> next = cur.next;
free cur;
cur = next
done;
free map;
return true
end
define Set<E>() Set<E>{empty,0}
define Set<E> Set_E
define Elem<E> Elem_E
struct Set<E> is {Elem<E> root, int length}
struct Elem<E> is {E val, Elem<E> next}
int size(Set<E> set) is return set.length end
bool contains(Set<E> set, E val) is
Elem<E> cur = set.root;
while(cur != empty) do
bool equal = call equals(cur.val, val);
if (equal) then
return true
else
cur = cur.next
fi
done;
return false
end
bool add(Set<E> set, E val) is
bool contains = call contains(set, val);
if (contains) then
return false
else
Elem<E> e = new Elem<E>{val, set.root};
set.root = e;
set.length = set.length + 1;
return true
fi
end
bool remove(Set<E> set, E val) is
if (set.root == empty) then
return false
else
Elem<E> cur = set.root;
bool equal = call equals(cur.val, val);
if (equal) then
set.root = cur.next;
free cur;
set.length = set.length - 1;
return true
else
Elem<E> next = cur.next;
while (next != empty) do
equal = call equals(next.val, val);
if (equal) then
cur.next = next.next;
free next;
set.length = set.length - 1;
return true
else
cur = next;
next = next.next
fi
done;
return false
fi
fi
end
bool destroy(Set<E> set) is
Elem<E> cur = set.root;
while (cur != empty) do
Elem<E> next = cur.next;
free cur;
cur = next
done;
free set;
return true
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment