From 371992a38d1972c9ae254c8317c8d0b8cf6ef938 Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 26 Feb 2016 21:59:27 +0300 Subject: [PATCH 1/4] task1 --- hw01/sp/Trie.java | 32 +++++++ hw01/sp/TrieImpl.java | 96 ++++++++++++++++++++ hw01/sp/TrieTest.java | 197 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 325 insertions(+) create mode 100644 hw01/sp/Trie.java create mode 100644 hw01/sp/TrieImpl.java create mode 100644 hw01/sp/TrieTest.java diff --git a/hw01/sp/Trie.java b/hw01/sp/Trie.java new file mode 100644 index 0000000..4ebfd31 --- /dev/null +++ b/hw01/sp/Trie.java @@ -0,0 +1,32 @@ +package sp; + +public interface Trie { + + /** + * Expected complexity: O(|element|) + * @return true if this set did not already contain the specified + * element + */ + boolean add(String element); + + /** + * Expected complexity: O(|element|) + */ + boolean contains(String element); + + /** + * Expected complexity: O(|element|) + * @return true if this set contained the specified element + */ + boolean remove(String element); + + /** + * Expected complexity: O(1) + */ + int size(); + + /** + * Expected complexity: O(|prefix|) + */ + int howManyStartsWithPrefix(String prefix); +} \ No newline at end of file diff --git a/hw01/sp/TrieImpl.java b/hw01/sp/TrieImpl.java new file mode 100644 index 0000000..975b358 --- /dev/null +++ b/hw01/sp/TrieImpl.java @@ -0,0 +1,96 @@ +package sp; + +import java.lang.Character; +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.TreeSet; +import java.util.Map; + +public class TrieImpl implements Trie { + + Node root = new Node(); + + public boolean add(String element) { + Node node = root; + for(char c : element.toCharArray()) + node = node.next(c); + return node.setLeaf(true); + } + + public boolean contains(String element) { + Node node = root; + for(char c : element.toCharArray()) { + if (!node.containsKey(c)) + return false; + node = node.get(c); + } + return node.isLeaf(); + } + + public boolean remove(String element) { + Node node = root; + for(char c : element.toCharArray()) { + if (!node.containsKey(c)) + return false; + node = node.get(c); + } + return node.setLeaf(false); + } + + public int size() { + return root.getSize(); + } + + public int howManyStartsWithPrefix(String prefix) { + Node node = root; + for(char c : prefix.toCharArray()) { + if (!node.containsKey(c)) + return 0; + node = node.get(c); + } + return node.getSize(); + } + + private class Node extends HashMap { + + private int wordCount = 0; + private boolean isLeaf = false; + private WeakReference parrent = null; + private Character character = null; + + public Node() {} + + public Node(Node prev, Character c) { + parrent = new WeakReference<>(prev); + character = c; + } + + public Node next(Character key) { + if (!containsKey(key)) + put(key, new Node(this, key)); + return get(key); + } + + public int getSize() { return wordCount; } + + private void updateSize(Character c, int delta) { + if (c != null && get(c).getSize() == 0) { + remove(c); + } + wordCount += delta; + if (parrent != null) + parrent.get().updateSize(this.character, delta); + } + + boolean isLeaf() { return isLeaf; } + + boolean setLeaf(boolean value) { + if (isLeaf == value) + return false; + isLeaf = value; + updateSize(null, value ? 1 : -1); + return true; + } + + } +} \ No newline at end of file diff --git a/hw01/sp/TrieTest.java b/hw01/sp/TrieTest.java new file mode 100644 index 0000000..d0012dd --- /dev/null +++ b/hw01/sp/TrieTest.java @@ -0,0 +1,197 @@ +package sp; + +import static org.junit.Assert.*; + +public class TrieTest { + + @org.junit.Test + public void testAdd() throws Exception { + Trie trie = new TrieImpl(); + + assertTrue(trie.add("")); + assertTrue(trie.add("aa")); + assertTrue(trie.add("a")); + assertTrue(trie.add("aaa")); + assertTrue(trie.add("aab")); + assertTrue(trie.add("aac")); + assertTrue(trie.add("ab")); + + assertFalse(trie.add("")); + assertFalse(trie.add("aa")); + assertFalse(trie.add("a")); + assertFalse(trie.add("aaa")); + assertFalse(trie.add("aab")); + assertFalse(trie.add("aac")); + assertFalse(trie.add("ab")); + + trie.remove(""); + trie.remove("aab"); + + assertTrue(trie.add("aaaa")); + assertFalse(trie.add("aaaa")); + + trie.remove("a"); + trie.remove("aa"); + trie.remove("aaa"); + trie.remove("aac"); + trie.remove("ab"); + } + + @org.junit.Test + public void testRemove() throws Exception { + Trie trie = new TrieImpl(); + assertFalse(trie.contains("")); + trie.add(""); + trie.add("aa"); + trie.add("a"); + trie.add("aaa"); + trie.add("aab"); + trie.add("aac"); + trie.add("ab"); + assertTrue(trie.remove("")); + assertTrue(trie.remove("aab")); + trie.add("aaaa"); + assertTrue(trie.remove("a")); + assertTrue(trie.remove("aa")); + assertTrue(trie.remove("aaa")); + assertTrue(trie.remove("aac")); + assertTrue(trie.remove("ab")); + + assertFalse(trie.remove("")); + assertFalse(trie.remove("aab")); + assertFalse(trie.remove("")); + assertFalse(trie.remove("aab")); + assertFalse(trie.remove("a")); + assertFalse(trie.remove("aa")); + assertFalse(trie.remove("aaa")); + assertFalse(trie.remove("aac")); + assertFalse(trie.remove("ab")); + } + + @org.junit.Test + public void testContains() throws Exception { + Trie trie = new TrieImpl(); + assertFalse(trie.contains("")); + trie.add(""); + trie.add("aa"); + trie.add("a"); + trie.add("aaa"); + trie.add("aab"); + trie.add("aac"); + trie.add("ab"); + + assertTrue(trie.contains("")); + assertTrue(trie.contains("aa")); + assertTrue(trie.contains("a")); + assertTrue(trie.contains("aaa")); + assertTrue(trie.contains("aab")); + assertTrue(trie.contains("aac")); + assertTrue(trie.contains("ab")); + + trie.remove(""); + trie.remove("aab"); + + assertFalse(trie.contains("")); + assertTrue(trie.contains("aa")); + assertTrue(trie.contains("a")); + assertTrue(trie.contains("aaa")); + assertFalse(trie.contains("aab")); + assertTrue(trie.contains("aac")); + assertTrue(trie.contains("ab")); + + trie.add("aaaa"); + trie.remove("a"); + trie.remove("aa"); + trie.remove("aaa"); + trie.remove("aac"); + trie.remove("ab"); + + assertFalse(trie.contains("")); + assertFalse(trie.contains("aa")); + assertFalse(trie.contains("a")); + assertFalse(trie.contains("aaa")); + assertFalse(trie.contains("aab")); + assertFalse(trie.contains("aac")); + assertFalse(trie.contains("ab")); + assertTrue(trie.contains("aaaa")); + } + + @org.junit.Test + public void testSize() throws Exception { + Trie trie = new TrieImpl(); + + assertEquals(trie.size(), 0); + trie.add(""); + assertEquals(trie.size(), 1); + trie.add("aa"); + trie.add("a"); + trie.add("aaa"); + assertEquals(trie.size(), 4); + trie.add("aab"); + trie.add("aac"); + trie.add("ab"); + assertEquals(trie.size(), 7); + + trie.remove(""); + trie.remove("aab"); + assertEquals(trie.size(), 5); + + trie.add("aaaa"); + assertEquals(trie.size(), 6); + + trie.remove("a"); + trie.remove("aa"); + trie.remove("aaa"); + assertEquals(trie.size(), 3); + trie.remove("aac"); + trie.remove("ab"); + assertEquals(trie.size(), 1); + + } + + @org.junit.Test + public void testHowManyStartsWithPrefix() throws Exception { + Trie trie = new TrieImpl(); + + assertEquals(trie.howManyStartsWithPrefix(""), 0); + assertEquals(trie.howManyStartsWithPrefix("a"), 0); + assertEquals(trie.howManyStartsWithPrefix("aa"), 0); + assertEquals(trie.howManyStartsWithPrefix("aaa"), 0); + + trie.add(""); + trie.add("aa"); + trie.add("a"); + trie.add("aaa"); + trie.add("aab"); + trie.add("aac"); + trie.add("ab"); + + assertEquals(trie.howManyStartsWithPrefix(""), 7); + assertEquals(trie.howManyStartsWithPrefix("a"), 6); + assertEquals(trie.howManyStartsWithPrefix("aa"), 4); + assertEquals(trie.howManyStartsWithPrefix("aaa"), 1); + + trie.remove(""); + trie.remove("aab"); + + trie.add("aaaa"); + + assertEquals(trie.howManyStartsWithPrefix(""), 6); + assertEquals(trie.howManyStartsWithPrefix("a"), 6); + assertEquals(trie.howManyStartsWithPrefix("aa"), 4); + assertEquals(trie.howManyStartsWithPrefix("aaa"), 2); + + trie.remove("a"); + trie.remove("aa"); + trie.remove("aaa"); + trie.remove("aac"); + trie.remove("ab"); + + assertEquals(trie.howManyStartsWithPrefix(""), 1); + assertEquals(trie.howManyStartsWithPrefix("a"), 1); + assertEquals(trie.howManyStartsWithPrefix("aa"), 1); + assertEquals(trie.howManyStartsWithPrefix("aaa"), 1); + } + + +} \ No newline at end of file From 751481e3c578b1455811561e380cc3f41e92c0da Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 6 Mar 2016 19:54:14 +0300 Subject: [PATCH 2/4] task2 --- build.gradle | 14 ++ build/reports/tests/classes/sp.TrieTest.html | 121 +++++++++++ build/reports/tests/css/base-style.css | 179 ++++++++++++++++ build/reports/tests/css/style.css | 84 ++++++++ build/reports/tests/index.html | 132 ++++++++++++ build/reports/tests/js/report.js | 194 ++++++++++++++++++ build/reports/tests/packages/sp.html | 103 ++++++++++ build/test-results/TEST-sp.TrieTest.xml | 12 ++ build/test-results/binary/test/output.bin | 0 build/test-results/binary/test/output.bin.idx | Bin 0 -> 1 bytes build/test-results/binary/test/results.bin | Bin 0 -> 179 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 164 +++++++++++++++ gradlew.bat | 90 ++++++++ hw01/sp/TrieImpl.java | 96 --------- hw02.iml | 22 ++ settings.gradle | 2 + src/main/java/sp/StreamSerializable.java | 15 ++ {hw01 => src/main/java}/sp/Trie.java | 0 src/main/java/sp/TrieImpl.java | 181 ++++++++++++++++ {hw01 => src/test/java}/sp/TrieTest.java | 37 +++- 21 files changed, 1355 insertions(+), 97 deletions(-) create mode 100644 build.gradle create mode 100644 build/reports/tests/classes/sp.TrieTest.html create mode 100644 build/reports/tests/css/base-style.css create mode 100644 build/reports/tests/css/style.css create mode 100644 build/reports/tests/index.html create mode 100644 build/reports/tests/js/report.js create mode 100644 build/reports/tests/packages/sp.html create mode 100644 build/test-results/TEST-sp.TrieTest.xml create mode 100644 build/test-results/binary/test/output.bin create mode 100644 build/test-results/binary/test/output.bin.idx create mode 100644 build/test-results/binary/test/results.bin create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat delete mode 100644 hw01/sp/TrieImpl.java create mode 100644 hw02.iml create mode 100644 settings.gradle create mode 100644 src/main/java/sp/StreamSerializable.java rename {hw01 => src/main/java}/sp/Trie.java (100%) create mode 100644 src/main/java/sp/TrieImpl.java rename {hw01 => src/test/java}/sp/TrieTest.java (87%) diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..c33546c --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +group 'org' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.jetbrains:annotations:15.0' + compile 'com.googlecode.json-simple:json-simple:1.1' + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/build/reports/tests/classes/sp.TrieTest.html b/build/reports/tests/classes/sp.TrieTest.html new file mode 100644 index 0000000..f99c623 --- /dev/null +++ b/build/reports/tests/classes/sp.TrieTest.html @@ -0,0 +1,121 @@ + + + + + +Test results - Class sp.TrieTest + + + + + +
+

Class sp.TrieTest

+ +
+ + + + + +
+
+ + + + + + + +
+
+
6
+

tests

+
+
+
+
0
+

failures

+
+
+
+
0
+

ignored

+
+
+
+
0.013s
+

duration

+
+
+
+
+
+
100%
+

successful

+
+
+
+
+ +
+

Tests

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TestDurationResult
testAdd0.003spassed
testContains0spassed
testHowManyStartsWithPrefix0spassed
testRemove0spassed
testSerialize0.010spassed
testSize0spassed
+
+
+ +
+ + diff --git a/build/reports/tests/css/base-style.css b/build/reports/tests/css/base-style.css new file mode 100644 index 0000000..4afa73e --- /dev/null +++ b/build/reports/tests/css/base-style.css @@ -0,0 +1,179 @@ + +body { + margin: 0; + padding: 0; + font-family: sans-serif; + font-size: 12pt; +} + +body, a, a:visited { + color: #303030; +} + +#content { + padding-left: 50px; + padding-right: 50px; + padding-top: 30px; + padding-bottom: 30px; +} + +#content h1 { + font-size: 160%; + margin-bottom: 10px; +} + +#footer { + margin-top: 100px; + font-size: 80%; + white-space: nowrap; +} + +#footer, #footer a { + color: #a0a0a0; +} + +#line-wrapping-toggle { + vertical-align: middle; +} + +#label-for-line-wrapping-toggle { + vertical-align: middle; +} + +ul { + margin-left: 0; +} + +h1, h2, h3 { + white-space: nowrap; +} + +h2 { + font-size: 120%; +} + +ul.tabLinks { + padding-left: 0; + padding-top: 10px; + padding-bottom: 10px; + overflow: auto; + min-width: 800px; + width: auto !important; + width: 800px; +} + +ul.tabLinks li { + float: left; + height: 100%; + list-style: none; + padding-left: 10px; + padding-right: 10px; + padding-top: 5px; + padding-bottom: 5px; + margin-bottom: 0; + -moz-border-radius: 7px; + border-radius: 7px; + margin-right: 25px; + border: solid 1px #d4d4d4; + background-color: #f0f0f0; +} + +ul.tabLinks li:hover { + background-color: #fafafa; +} + +ul.tabLinks li.selected { + background-color: #c5f0f5; + border-color: #c5f0f5; +} + +ul.tabLinks a { + font-size: 120%; + display: block; + outline: none; + text-decoration: none; + margin: 0; + padding: 0; +} + +ul.tabLinks li h2 { + margin: 0; + padding: 0; +} + +div.tab { +} + +div.selected { + display: block; +} + +div.deselected { + display: none; +} + +div.tab table { + min-width: 350px; + width: auto !important; + width: 350px; + border-collapse: collapse; +} + +div.tab th, div.tab table { + border-bottom: solid #d0d0d0 1px; +} + +div.tab th { + text-align: left; + white-space: nowrap; + padding-left: 6em; +} + +div.tab th:first-child { + padding-left: 0; +} + +div.tab td { + white-space: nowrap; + padding-left: 6em; + padding-top: 5px; + padding-bottom: 5px; +} + +div.tab td:first-child { + padding-left: 0; +} + +div.tab td.numeric, div.tab th.numeric { + text-align: right; +} + +span.code { + display: inline-block; + margin-top: 0em; + margin-bottom: 1em; +} + +span.code pre { + font-size: 11pt; + padding-top: 10px; + padding-bottom: 10px; + padding-left: 10px; + padding-right: 10px; + margin: 0; + background-color: #f7f7f7; + border: solid 1px #d0d0d0; + min-width: 700px; + width: auto !important; + width: 700px; +} + +span.wrapped pre { + word-wrap: break-word; + white-space: pre-wrap; + word-break: break-all; +} + +label.hidden { + display: none; +} \ No newline at end of file diff --git a/build/reports/tests/css/style.css b/build/reports/tests/css/style.css new file mode 100644 index 0000000..3dc4913 --- /dev/null +++ b/build/reports/tests/css/style.css @@ -0,0 +1,84 @@ + +#summary { + margin-top: 30px; + margin-bottom: 40px; +} + +#summary table { + border-collapse: collapse; +} + +#summary td { + vertical-align: top; +} + +.breadcrumbs, .breadcrumbs a { + color: #606060; +} + +.infoBox { + width: 110px; + padding-top: 15px; + padding-bottom: 15px; + text-align: center; +} + +.infoBox p { + margin: 0; +} + +.counter, .percent { + font-size: 120%; + font-weight: bold; + margin-bottom: 8px; +} + +#duration { + width: 125px; +} + +#successRate, .summaryGroup { + border: solid 2px #d0d0d0; + -moz-border-radius: 10px; + border-radius: 10px; +} + +#successRate { + width: 140px; + margin-left: 35px; +} + +#successRate .percent { + font-size: 180%; +} + +.success, .success a { + color: #008000; +} + +div.success, #successRate.success { + background-color: #bbd9bb; + border-color: #008000; +} + +.failures, .failures a { + color: #b60808; +} + +.skipped, .skipped a { + color: #c09853; +} + +div.failures, #successRate.failures { + background-color: #ecdada; + border-color: #b60808; +} + +ul.linkList { + padding-left: 0; +} + +ul.linkList li { + list-style: none; + margin-bottom: 5px; +} diff --git a/build/reports/tests/index.html b/build/reports/tests/index.html new file mode 100644 index 0000000..708b71b --- /dev/null +++ b/build/reports/tests/index.html @@ -0,0 +1,132 @@ + + + + + +Test results - Test Summary + + + + + +
+

Test Summary

+
+ + + + + +
+
+ + + + + + + +
+
+
6
+

tests

+
+
+
+
0
+

failures

+
+
+
+
0
+

ignored

+
+
+
+
0.013s
+

duration

+
+
+
+
+
+
100%
+

successful

+
+
+
+
+ +
+

Packages

+ + + + + + + + + + + + + + + + + + + + + +
PackageTestsFailuresIgnoredDurationSuccess rate
+sp +6000.013s100%
+
+
+

Classes

+ + + + + + + + + + + + + + + + + + + + +
ClassTestsFailuresIgnoredDurationSuccess rate
+sp.TrieTest +6000.013s100%
+
+
+ +
+ + diff --git a/build/reports/tests/js/report.js b/build/reports/tests/js/report.js new file mode 100644 index 0000000..83bab4a --- /dev/null +++ b/build/reports/tests/js/report.js @@ -0,0 +1,194 @@ +(function (window, document) { + "use strict"; + + var tabs = {}; + + function changeElementClass(element, classValue) { + if (element.getAttribute("className")) { + element.setAttribute("className", classValue); + } else { + element.setAttribute("class", classValue); + } + } + + function getClassAttribute(element) { + if (element.getAttribute("className")) { + return element.getAttribute("className"); + } else { + return element.getAttribute("class"); + } + } + + function addClass(element, classValue) { + changeElementClass(element, getClassAttribute(element) + " " + classValue); + } + + function removeClass(element, classValue) { + changeElementClass(element, getClassAttribute(element).replace(classValue, "")); + } + + function initTabs() { + var container = document.getElementById("tabs"); + + tabs.tabs = findTabs(container); + tabs.titles = findTitles(tabs.tabs); + tabs.headers = findHeaders(container); + tabs.select = select; + tabs.deselectAll = deselectAll; + tabs.select(0); + + return true; + } + + function getCheckBox() { + return document.getElementById("line-wrapping-toggle"); + } + + function getLabelForCheckBox() { + return document.getElementById("label-for-line-wrapping-toggle"); + } + + function findCodeBlocks() { + var spans = document.getElementById("tabs").getElementsByTagName("span"); + var codeBlocks = []; + for (var i = 0; i < spans.length; ++i) { + if (spans[i].className.indexOf("code") >= 0) { + codeBlocks.push(spans[i]); + } + } + return codeBlocks; + } + + function forAllCodeBlocks(operation) { + var codeBlocks = findCodeBlocks(); + + for (var i = 0; i < codeBlocks.length; ++i) { + operation(codeBlocks[i], "wrapped"); + } + } + + function toggleLineWrapping() { + var checkBox = getCheckBox(); + + if (checkBox.checked) { + forAllCodeBlocks(addClass); + } else { + forAllCodeBlocks(removeClass); + } + } + + function initControls() { + if (findCodeBlocks().length > 0) { + var checkBox = getCheckBox(); + var label = getLabelForCheckBox(); + + checkBox.onclick = toggleLineWrapping; + checkBox.checked = false; + + removeClass(label, "hidden"); + } + } + + function switchTab() { + var id = this.id.substr(1); + + for (var i = 0; i < tabs.tabs.length; i++) { + if (tabs.tabs[i].id === id) { + tabs.select(i); + break; + } + } + + return false; + } + + function select(i) { + this.deselectAll(); + + changeElementClass(this.tabs[i], "tab selected"); + changeElementClass(this.headers[i], "selected"); + + while (this.headers[i].firstChild) { + this.headers[i].removeChild(this.headers[i].firstChild); + } + + var h2 = document.createElement("H2"); + + h2.appendChild(document.createTextNode(this.titles[i])); + this.headers[i].appendChild(h2); + } + + function deselectAll() { + for (var i = 0; i < this.tabs.length; i++) { + changeElementClass(this.tabs[i], "tab deselected"); + changeElementClass(this.headers[i], "deselected"); + + while (this.headers[i].firstChild) { + this.headers[i].removeChild(this.headers[i].firstChild); + } + + var a = document.createElement("A"); + + a.setAttribute("id", "ltab" + i); + a.setAttribute("href", "#tab" + i); + a.onclick = switchTab; + a.appendChild(document.createTextNode(this.titles[i])); + + this.headers[i].appendChild(a); + } + } + + function findTabs(container) { + return findChildElements(container, "DIV", "tab"); + } + + function findHeaders(container) { + var owner = findChildElements(container, "UL", "tabLinks"); + return findChildElements(owner[0], "LI", null); + } + + function findTitles(tabs) { + var titles = []; + + for (var i = 0; i < tabs.length; i++) { + var tab = tabs[i]; + var header = findChildElements(tab, "H2", null)[0]; + + header.parentNode.removeChild(header); + + if (header.innerText) { + titles.push(header.innerText); + } else { + titles.push(header.textContent); + } + } + + return titles; + } + + function findChildElements(container, name, targetClass) { + var elements = []; + var children = container.childNodes; + + for (var i = 0; i < children.length; i++) { + var child = children.item(i); + + if (child.nodeType === 1 && child.nodeName === name) { + if (targetClass && child.className.indexOf(targetClass) < 0) { + continue; + } + + elements.push(child); + } + } + + return elements; + } + + // Entry point. + + window.onload = function() { + initTabs(); + initControls(); + }; +} (window, window.document)); \ No newline at end of file diff --git a/build/reports/tests/packages/sp.html b/build/reports/tests/packages/sp.html new file mode 100644 index 0000000..0c2b5a5 --- /dev/null +++ b/build/reports/tests/packages/sp.html @@ -0,0 +1,103 @@ + + + + + +Test results - Package sp + + + + + +
+

Package sp

+ +
+ + + + + +
+
+ + + + + + + +
+
+
6
+

tests

+
+
+
+
0
+

failures

+
+
+
+
0
+

ignored

+
+
+
+
0.013s
+

duration

+
+
+
+
+
+
100%
+

successful

+
+
+
+
+ +
+

Classes

+ + + + + + + + + + + + + + + + + + + +
ClassTestsFailuresIgnoredDurationSuccess rate
+TrieTest +6000.013s100%
+
+
+ +
+ + diff --git a/build/test-results/TEST-sp.TrieTest.xml b/build/test-results/TEST-sp.TrieTest.xml new file mode 100644 index 0000000..5543439 --- /dev/null +++ b/build/test-results/TEST-sp.TrieTest.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/build/test-results/binary/test/output.bin b/build/test-results/binary/test/output.bin new file mode 100644 index 0000000..e69de29 diff --git a/build/test-results/binary/test/output.bin.idx b/build/test-results/binary/test/output.bin.idx new file mode 100644 index 0000000000000000000000000000000000000000..f76dd238ade08917e6712764a16a22005a50573d GIT binary patch literal 1 IcmZPo000310RR91 literal 0 HcmV?d00001 diff --git a/build/test-results/binary/test/results.bin b/build/test-results/binary/test/results.bin new file mode 100644 index 0000000000000000000000000000000000000000..a0fc6661e33afde3af540ebae980f618fafdbee0 GIT binary patch literal 179 zcmZQ(Wa=p{&)!K@vf!x%p)Xle`$%=YWJg e^2>b_^D2W&5{pWT!!t`V0*X@8GArQPy%+#}a5&=t literal 0 HcmV?d00001 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..0c19db4 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Feb 14 15:03:16 MSK 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/hw01/sp/TrieImpl.java b/hw01/sp/TrieImpl.java deleted file mode 100644 index 975b358..0000000 --- a/hw01/sp/TrieImpl.java +++ /dev/null @@ -1,96 +0,0 @@ -package sp; - -import java.lang.Character; -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.TreeSet; -import java.util.Map; - -public class TrieImpl implements Trie { - - Node root = new Node(); - - public boolean add(String element) { - Node node = root; - for(char c : element.toCharArray()) - node = node.next(c); - return node.setLeaf(true); - } - - public boolean contains(String element) { - Node node = root; - for(char c : element.toCharArray()) { - if (!node.containsKey(c)) - return false; - node = node.get(c); - } - return node.isLeaf(); - } - - public boolean remove(String element) { - Node node = root; - for(char c : element.toCharArray()) { - if (!node.containsKey(c)) - return false; - node = node.get(c); - } - return node.setLeaf(false); - } - - public int size() { - return root.getSize(); - } - - public int howManyStartsWithPrefix(String prefix) { - Node node = root; - for(char c : prefix.toCharArray()) { - if (!node.containsKey(c)) - return 0; - node = node.get(c); - } - return node.getSize(); - } - - private class Node extends HashMap { - - private int wordCount = 0; - private boolean isLeaf = false; - private WeakReference parrent = null; - private Character character = null; - - public Node() {} - - public Node(Node prev, Character c) { - parrent = new WeakReference<>(prev); - character = c; - } - - public Node next(Character key) { - if (!containsKey(key)) - put(key, new Node(this, key)); - return get(key); - } - - public int getSize() { return wordCount; } - - private void updateSize(Character c, int delta) { - if (c != null && get(c).getSize() == 0) { - remove(c); - } - wordCount += delta; - if (parrent != null) - parrent.get().updateSize(this.character, delta); - } - - boolean isLeaf() { return isLeaf; } - - boolean setLeaf(boolean value) { - if (isLeaf == value) - return false; - isLeaf = value; - updateSize(null, value ? 1 : -1); - return true; - } - - } -} \ No newline at end of file diff --git a/hw02.iml b/hw02.iml new file mode 100644 index 0000000..ca7e3c0 --- /dev/null +++ b/hw02.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..bd29227 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'hw02' + diff --git a/src/main/java/sp/StreamSerializable.java b/src/main/java/sp/StreamSerializable.java new file mode 100644 index 0000000..4ea7dcb --- /dev/null +++ b/src/main/java/sp/StreamSerializable.java @@ -0,0 +1,15 @@ +package sp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public interface StreamSerializable { + + void serialize(OutputStream out) throws IOException; + + /** + * Replace current state with data from input stream + */ + void deserialize(InputStream in) throws IOException; +} \ No newline at end of file diff --git a/hw01/sp/Trie.java b/src/main/java/sp/Trie.java similarity index 100% rename from hw01/sp/Trie.java rename to src/main/java/sp/Trie.java diff --git a/src/main/java/sp/TrieImpl.java b/src/main/java/sp/TrieImpl.java new file mode 100644 index 0000000..aa85631 --- /dev/null +++ b/src/main/java/sp/TrieImpl.java @@ -0,0 +1,181 @@ +package sp; + +import java.io.*; +import java.lang.Character; +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +public class TrieImpl implements Trie, StreamSerializable { + + private final Node root = new Node(); + + @Override + public boolean add(String element) { + Node node = root; + for(char c : element.toCharArray()) { + node = node.next(c); + } + return changeLeafState(node, true); + } + + @Override + public boolean contains(String element) { + Node node = findNode(element); + return node != null && node.isLeaf(); + } + + @Override + public boolean remove(String element) { + Node node = findNode(element); + return node != null && changeLeafState(node, false); + } + + @Override + public int size() { + return root.getSize(); + } + + @Override + public int howManyStartsWithPrefix(String prefix) { + Node node = findNode(prefix); + return node == null ? 0 : node.getSize(); + } + + private Node findNode(String element) { + Node node = root; + for(char c : element.toCharArray()) { + if (!node.containsKey(c)) return null; + node = node.get(c); + } + return node; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof TrieImpl && root.equals(((TrieImpl) obj).root); + } + + @Override + public void serialize(OutputStream out) throws IOException { + JSONObject obj = root.toJson(); + out.write(obj.toJSONString().getBytes()); + } + + @Override + public void deserialize(InputStream in) throws IOException { + try { + JSONObject obj = (JSONObject)JSONValue.parseWithException(new InputStreamReader(in)); + root.fromJson(obj, null); + } catch (Exception e) { + e.printStackTrace(); + throw new IOException("can't parse json"); + } + } + + private boolean changeLeafState(Node node, boolean value) { + if (node.isLeaf() == value) return false; + node.setLeaf(value); + + int delta = value ? 1 : -1; + for (Node prev = null; node != null; node = node.getParent()) { + if (prev != null && prev.getSize() == 0) { + node.remove(prev.character); + } + node.wordCount += delta; + prev = node; + } + + return true; + } + + private static class Node { + + private int wordCount = 0; + private boolean isLeaf = false; + private Character character = null; + private WeakReference parent = null; + private Map map = new HashMap<>(); + + public static final String WORD_COUNT = "wordCount"; + public static final String CHARACTER = "character"; + public static final String IS_LEAF = "isLeaf"; + public static final String OTHER = "other"; + + public Node() {} + + public Node(Node prev, Character c) { + parent = new WeakReference<>(prev); + character = c; + } + + public Node get(Character key) { return map.get(key); } + + public Node remove(Character key) { return map.remove(key); } + + public boolean containsKey(Character key) { return map.containsKey(key); } + + public void put(Character key, Node value) { map.put(key, value); } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Node)) return false; + Node other = (Node) o; + if (wordCount != other.wordCount) return false; + if (isLeaf != other.isLeaf) return false; + if (character != other.character) return false; + if (!map.keySet().equals(other.map.keySet())) return false; + for (Character c : map.keySet()) { + if (!get(c).equals(other.get(c))) return false; + } + return true; + } + + public void fromJson(JSONObject obj, Node prev) throws Exception { + parent = prev == null ? null : new WeakReference<>(prev); + wordCount = ((Long) obj.get(WORD_COUNT)).intValue(); + isLeaf = (Boolean) obj.get(IS_LEAF); + character = obj.get(CHARACTER) == null ? null : ((String) obj.get(CHARACTER)).charAt(0); + JSONObject other = (JSONObject) obj.get(OTHER); + map.clear(); + for (Object key : other.keySet()) { + Node node = new Node(); + node.fromJson((JSONObject) other.get(key), this); + put(((String)key).charAt(0), node); + } + } + + @SuppressWarnings("unchecked") + public JSONObject toJson() { + JSONObject obj = new JSONObject(); + obj.put(WORD_COUNT, wordCount); + obj.put(IS_LEAF, isLeaf); + obj.put(CHARACTER, character == null ? null : Character.toString(character)); + JSONObject other = new JSONObject(); + for (Character c : map.keySet()) { + other.put(c, get(c).toJson()); + } + obj.put(OTHER, other); + return obj; + } + + public Node next(Character key) { + if (!containsKey(key)) { + put(key, new Node(this, key)); + } + return get(key); + } + + public int getSize() { return wordCount; } + + public Node getParent() { return parent == null ? null : parent.get(); } + + public boolean isLeaf() { return isLeaf; } + + public void setLeaf(boolean value) { isLeaf = value; } + + } +} \ No newline at end of file diff --git a/hw01/sp/TrieTest.java b/src/test/java/sp/TrieTest.java similarity index 87% rename from hw01/sp/TrieTest.java rename to src/test/java/sp/TrieTest.java index d0012dd..78f1749 100644 --- a/hw01/sp/TrieTest.java +++ b/src/test/java/sp/TrieTest.java @@ -1,9 +1,44 @@ package sp; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + import static org.junit.Assert.*; public class TrieTest { - + + @org.junit.Test + public void testSerialize() throws Exception { + TrieImpl trie = new TrieImpl(); + TrieImpl other = new TrieImpl(); + + other.add(""); + other.add("aa"); + + trie.add(""); + trie.add("aa"); + + assertTrue(trie.equals(other)); + + trie.add("a"); + trie.add("aaa"); + trie.add("aab"); + trie.add("aac"); + trie.add("ab"); + + assertFalse(trie.equals(other)); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + trie.serialize(out); + InputStream in = new ByteArrayInputStream(out.toByteArray()); + other.deserialize(in); + + + assertTrue(trie.equals(other)); + + } + @org.junit.Test public void testAdd() throws Exception { Trie trie = new TrieImpl(); From 08114f223216e1a42aa097f8f02ab19a3dc4a909 Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 20 Mar 2016 16:50:34 +0300 Subject: [PATCH 3/4] fix task2 --- .gitignore | 3 + build/reports/tests/classes/sp.TrieTest.html | 121 ----------- build/reports/tests/css/base-style.css | 179 ---------------- build/reports/tests/css/style.css | 84 -------- build/reports/tests/index.html | 132 ------------ build/reports/tests/js/report.js | 194 ------------------ build/reports/tests/packages/sp.html | 103 ---------- build/test-results/TEST-sp.TrieTest.xml | 12 -- build/test-results/binary/test/output.bin | 0 build/test-results/binary/test/output.bin.idx | Bin 1 -> 0 bytes build/test-results/binary/test/results.bin | Bin 179 -> 0 bytes src/main/java/sp/TrieImpl.java | 113 ++++++---- 12 files changed, 73 insertions(+), 868 deletions(-) delete mode 100644 build/reports/tests/classes/sp.TrieTest.html delete mode 100644 build/reports/tests/css/base-style.css delete mode 100644 build/reports/tests/css/style.css delete mode 100644 build/reports/tests/index.html delete mode 100644 build/reports/tests/js/report.js delete mode 100644 build/reports/tests/packages/sp.html delete mode 100644 build/test-results/TEST-sp.TrieTest.xml delete mode 100644 build/test-results/binary/test/output.bin delete mode 100644 build/test-results/binary/test/output.bin.idx delete mode 100644 build/test-results/binary/test/results.bin diff --git a/.gitignore b/.gitignore index 32858aa..b3c9197 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ # Mobile Tools for Java (J2ME) .mtj.tmp/ +build/* +.gradle/* + # Package Files # *.jar *.war diff --git a/build/reports/tests/classes/sp.TrieTest.html b/build/reports/tests/classes/sp.TrieTest.html deleted file mode 100644 index f99c623..0000000 --- a/build/reports/tests/classes/sp.TrieTest.html +++ /dev/null @@ -1,121 +0,0 @@ - - - - - -Test results - Class sp.TrieTest - - - - - -
-

Class sp.TrieTest

- -
- - - - - -
-
- - - - - - - -
-
-
6
-

tests

-
-
-
-
0
-

failures

-
-
-
-
0
-

ignored

-
-
-
-
0.013s
-

duration

-
-
-
-
-
-
100%
-

successful

-
-
-
-
- -
-

Tests

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TestDurationResult
testAdd0.003spassed
testContains0spassed
testHowManyStartsWithPrefix0spassed
testRemove0spassed
testSerialize0.010spassed
testSize0spassed
-
-
- -
- - diff --git a/build/reports/tests/css/base-style.css b/build/reports/tests/css/base-style.css deleted file mode 100644 index 4afa73e..0000000 --- a/build/reports/tests/css/base-style.css +++ /dev/null @@ -1,179 +0,0 @@ - -body { - margin: 0; - padding: 0; - font-family: sans-serif; - font-size: 12pt; -} - -body, a, a:visited { - color: #303030; -} - -#content { - padding-left: 50px; - padding-right: 50px; - padding-top: 30px; - padding-bottom: 30px; -} - -#content h1 { - font-size: 160%; - margin-bottom: 10px; -} - -#footer { - margin-top: 100px; - font-size: 80%; - white-space: nowrap; -} - -#footer, #footer a { - color: #a0a0a0; -} - -#line-wrapping-toggle { - vertical-align: middle; -} - -#label-for-line-wrapping-toggle { - vertical-align: middle; -} - -ul { - margin-left: 0; -} - -h1, h2, h3 { - white-space: nowrap; -} - -h2 { - font-size: 120%; -} - -ul.tabLinks { - padding-left: 0; - padding-top: 10px; - padding-bottom: 10px; - overflow: auto; - min-width: 800px; - width: auto !important; - width: 800px; -} - -ul.tabLinks li { - float: left; - height: 100%; - list-style: none; - padding-left: 10px; - padding-right: 10px; - padding-top: 5px; - padding-bottom: 5px; - margin-bottom: 0; - -moz-border-radius: 7px; - border-radius: 7px; - margin-right: 25px; - border: solid 1px #d4d4d4; - background-color: #f0f0f0; -} - -ul.tabLinks li:hover { - background-color: #fafafa; -} - -ul.tabLinks li.selected { - background-color: #c5f0f5; - border-color: #c5f0f5; -} - -ul.tabLinks a { - font-size: 120%; - display: block; - outline: none; - text-decoration: none; - margin: 0; - padding: 0; -} - -ul.tabLinks li h2 { - margin: 0; - padding: 0; -} - -div.tab { -} - -div.selected { - display: block; -} - -div.deselected { - display: none; -} - -div.tab table { - min-width: 350px; - width: auto !important; - width: 350px; - border-collapse: collapse; -} - -div.tab th, div.tab table { - border-bottom: solid #d0d0d0 1px; -} - -div.tab th { - text-align: left; - white-space: nowrap; - padding-left: 6em; -} - -div.tab th:first-child { - padding-left: 0; -} - -div.tab td { - white-space: nowrap; - padding-left: 6em; - padding-top: 5px; - padding-bottom: 5px; -} - -div.tab td:first-child { - padding-left: 0; -} - -div.tab td.numeric, div.tab th.numeric { - text-align: right; -} - -span.code { - display: inline-block; - margin-top: 0em; - margin-bottom: 1em; -} - -span.code pre { - font-size: 11pt; - padding-top: 10px; - padding-bottom: 10px; - padding-left: 10px; - padding-right: 10px; - margin: 0; - background-color: #f7f7f7; - border: solid 1px #d0d0d0; - min-width: 700px; - width: auto !important; - width: 700px; -} - -span.wrapped pre { - word-wrap: break-word; - white-space: pre-wrap; - word-break: break-all; -} - -label.hidden { - display: none; -} \ No newline at end of file diff --git a/build/reports/tests/css/style.css b/build/reports/tests/css/style.css deleted file mode 100644 index 3dc4913..0000000 --- a/build/reports/tests/css/style.css +++ /dev/null @@ -1,84 +0,0 @@ - -#summary { - margin-top: 30px; - margin-bottom: 40px; -} - -#summary table { - border-collapse: collapse; -} - -#summary td { - vertical-align: top; -} - -.breadcrumbs, .breadcrumbs a { - color: #606060; -} - -.infoBox { - width: 110px; - padding-top: 15px; - padding-bottom: 15px; - text-align: center; -} - -.infoBox p { - margin: 0; -} - -.counter, .percent { - font-size: 120%; - font-weight: bold; - margin-bottom: 8px; -} - -#duration { - width: 125px; -} - -#successRate, .summaryGroup { - border: solid 2px #d0d0d0; - -moz-border-radius: 10px; - border-radius: 10px; -} - -#successRate { - width: 140px; - margin-left: 35px; -} - -#successRate .percent { - font-size: 180%; -} - -.success, .success a { - color: #008000; -} - -div.success, #successRate.success { - background-color: #bbd9bb; - border-color: #008000; -} - -.failures, .failures a { - color: #b60808; -} - -.skipped, .skipped a { - color: #c09853; -} - -div.failures, #successRate.failures { - background-color: #ecdada; - border-color: #b60808; -} - -ul.linkList { - padding-left: 0; -} - -ul.linkList li { - list-style: none; - margin-bottom: 5px; -} diff --git a/build/reports/tests/index.html b/build/reports/tests/index.html deleted file mode 100644 index 708b71b..0000000 --- a/build/reports/tests/index.html +++ /dev/null @@ -1,132 +0,0 @@ - - - - - -Test results - Test Summary - - - - - -
-

Test Summary

-
- - - - - -
-
- - - - - - - -
-
-
6
-

tests

-
-
-
-
0
-

failures

-
-
-
-
0
-

ignored

-
-
-
-
0.013s
-

duration

-
-
-
-
-
-
100%
-

successful

-
-
-
-
- -
-

Packages

- - - - - - - - - - - - - - - - - - - - - -
PackageTestsFailuresIgnoredDurationSuccess rate
-sp -6000.013s100%
-
-
-

Classes

- - - - - - - - - - - - - - - - - - - - -
ClassTestsFailuresIgnoredDurationSuccess rate
-sp.TrieTest -6000.013s100%
-
-
- -
- - diff --git a/build/reports/tests/js/report.js b/build/reports/tests/js/report.js deleted file mode 100644 index 83bab4a..0000000 --- a/build/reports/tests/js/report.js +++ /dev/null @@ -1,194 +0,0 @@ -(function (window, document) { - "use strict"; - - var tabs = {}; - - function changeElementClass(element, classValue) { - if (element.getAttribute("className")) { - element.setAttribute("className", classValue); - } else { - element.setAttribute("class", classValue); - } - } - - function getClassAttribute(element) { - if (element.getAttribute("className")) { - return element.getAttribute("className"); - } else { - return element.getAttribute("class"); - } - } - - function addClass(element, classValue) { - changeElementClass(element, getClassAttribute(element) + " " + classValue); - } - - function removeClass(element, classValue) { - changeElementClass(element, getClassAttribute(element).replace(classValue, "")); - } - - function initTabs() { - var container = document.getElementById("tabs"); - - tabs.tabs = findTabs(container); - tabs.titles = findTitles(tabs.tabs); - tabs.headers = findHeaders(container); - tabs.select = select; - tabs.deselectAll = deselectAll; - tabs.select(0); - - return true; - } - - function getCheckBox() { - return document.getElementById("line-wrapping-toggle"); - } - - function getLabelForCheckBox() { - return document.getElementById("label-for-line-wrapping-toggle"); - } - - function findCodeBlocks() { - var spans = document.getElementById("tabs").getElementsByTagName("span"); - var codeBlocks = []; - for (var i = 0; i < spans.length; ++i) { - if (spans[i].className.indexOf("code") >= 0) { - codeBlocks.push(spans[i]); - } - } - return codeBlocks; - } - - function forAllCodeBlocks(operation) { - var codeBlocks = findCodeBlocks(); - - for (var i = 0; i < codeBlocks.length; ++i) { - operation(codeBlocks[i], "wrapped"); - } - } - - function toggleLineWrapping() { - var checkBox = getCheckBox(); - - if (checkBox.checked) { - forAllCodeBlocks(addClass); - } else { - forAllCodeBlocks(removeClass); - } - } - - function initControls() { - if (findCodeBlocks().length > 0) { - var checkBox = getCheckBox(); - var label = getLabelForCheckBox(); - - checkBox.onclick = toggleLineWrapping; - checkBox.checked = false; - - removeClass(label, "hidden"); - } - } - - function switchTab() { - var id = this.id.substr(1); - - for (var i = 0; i < tabs.tabs.length; i++) { - if (tabs.tabs[i].id === id) { - tabs.select(i); - break; - } - } - - return false; - } - - function select(i) { - this.deselectAll(); - - changeElementClass(this.tabs[i], "tab selected"); - changeElementClass(this.headers[i], "selected"); - - while (this.headers[i].firstChild) { - this.headers[i].removeChild(this.headers[i].firstChild); - } - - var h2 = document.createElement("H2"); - - h2.appendChild(document.createTextNode(this.titles[i])); - this.headers[i].appendChild(h2); - } - - function deselectAll() { - for (var i = 0; i < this.tabs.length; i++) { - changeElementClass(this.tabs[i], "tab deselected"); - changeElementClass(this.headers[i], "deselected"); - - while (this.headers[i].firstChild) { - this.headers[i].removeChild(this.headers[i].firstChild); - } - - var a = document.createElement("A"); - - a.setAttribute("id", "ltab" + i); - a.setAttribute("href", "#tab" + i); - a.onclick = switchTab; - a.appendChild(document.createTextNode(this.titles[i])); - - this.headers[i].appendChild(a); - } - } - - function findTabs(container) { - return findChildElements(container, "DIV", "tab"); - } - - function findHeaders(container) { - var owner = findChildElements(container, "UL", "tabLinks"); - return findChildElements(owner[0], "LI", null); - } - - function findTitles(tabs) { - var titles = []; - - for (var i = 0; i < tabs.length; i++) { - var tab = tabs[i]; - var header = findChildElements(tab, "H2", null)[0]; - - header.parentNode.removeChild(header); - - if (header.innerText) { - titles.push(header.innerText); - } else { - titles.push(header.textContent); - } - } - - return titles; - } - - function findChildElements(container, name, targetClass) { - var elements = []; - var children = container.childNodes; - - for (var i = 0; i < children.length; i++) { - var child = children.item(i); - - if (child.nodeType === 1 && child.nodeName === name) { - if (targetClass && child.className.indexOf(targetClass) < 0) { - continue; - } - - elements.push(child); - } - } - - return elements; - } - - // Entry point. - - window.onload = function() { - initTabs(); - initControls(); - }; -} (window, window.document)); \ No newline at end of file diff --git a/build/reports/tests/packages/sp.html b/build/reports/tests/packages/sp.html deleted file mode 100644 index 0c2b5a5..0000000 --- a/build/reports/tests/packages/sp.html +++ /dev/null @@ -1,103 +0,0 @@ - - - - - -Test results - Package sp - - - - - -
-

Package sp

- -
- - - - - -
-
- - - - - - - -
-
-
6
-

tests

-
-
-
-
0
-

failures

-
-
-
-
0
-

ignored

-
-
-
-
0.013s
-

duration

-
-
-
-
-
-
100%
-

successful

-
-
-
-
- -
-

Classes

- - - - - - - - - - - - - - - - - - - -
ClassTestsFailuresIgnoredDurationSuccess rate
-TrieTest -6000.013s100%
-
-
- -
- - diff --git a/build/test-results/TEST-sp.TrieTest.xml b/build/test-results/TEST-sp.TrieTest.xml deleted file mode 100644 index 5543439..0000000 --- a/build/test-results/TEST-sp.TrieTest.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/build/test-results/binary/test/output.bin b/build/test-results/binary/test/output.bin deleted file mode 100644 index e69de29..0000000 diff --git a/build/test-results/binary/test/output.bin.idx b/build/test-results/binary/test/output.bin.idx deleted file mode 100644 index f76dd238ade08917e6712764a16a22005a50573d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1 IcmZPo000310RR91 diff --git a/build/test-results/binary/test/results.bin b/build/test-results/binary/test/results.bin deleted file mode 100644 index a0fc6661e33afde3af540ebae980f618fafdbee0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 179 zcmZQ(Wa=p{&)!K@vf!x%p)Xle`$%=YWJg e^2>b_^D2W&5{pWT!!t`V0*X@8GArQPy%+#}a5&=t diff --git a/src/main/java/sp/TrieImpl.java b/src/main/java/sp/TrieImpl.java index aa85631..9ea972d 100644 --- a/src/main/java/sp/TrieImpl.java +++ b/src/main/java/sp/TrieImpl.java @@ -16,8 +16,8 @@ public class TrieImpl implements Trie, StreamSerializable { @Override public boolean add(String element) { Node node = root; - for(char c : element.toCharArray()) { - node = node.next(c); + for (char c : element.toCharArray()) { + node = node.getNextNodeOrCreateIfNotExists(c); } return changeLeafState(node, true); } @@ -45,20 +45,16 @@ public int howManyStartsWithPrefix(String prefix) { return node == null ? 0 : node.getSize(); } - private Node findNode(String element) { - Node node = root; - for(char c : element.toCharArray()) { - if (!node.containsKey(c)) return null; - node = node.get(c); - } - return node; - } - @Override public boolean equals(Object obj) { return obj instanceof TrieImpl && root.equals(((TrieImpl) obj).root); } + @Override + public int hashCode() { + return root.hashCode(); + } + @Override public void serialize(OutputStream out) throws IOException { JSONObject obj = root.toJson(); @@ -68,12 +64,20 @@ public void serialize(OutputStream out) throws IOException { @Override public void deserialize(InputStream in) throws IOException { try { - JSONObject obj = (JSONObject)JSONValue.parseWithException(new InputStreamReader(in)); + JSONObject obj = (JSONObject) JSONValue.parseWithException(new InputStreamReader(in)); root.fromJson(obj, null); } catch (Exception e) { - e.printStackTrace(); - throw new IOException("can't parse json"); + throw new IOException("can't parse json", e); + } + } + + private Node findNode(String element) { + Node node = root; + for (char c : element.toCharArray()) { + if (!node.containsKey(c)) return null; + node = node.get(c); } + return node; } private boolean changeLeafState(Node node, boolean value) { @@ -85,7 +89,7 @@ private boolean changeLeafState(Node node, boolean value) { if (prev != null && prev.getSize() == 0) { node.remove(prev.character); } - node.wordCount += delta; + node.setSize(node.getSize() + delta); prev = node; } @@ -96,55 +100,66 @@ private static class Node { private int wordCount = 0; private boolean isLeaf = false; - private Character character = null; + private char character = '\n'; private WeakReference parent = null; - private Map map = new HashMap<>(); + private Map nextNodes = new HashMap<>(); public static final String WORD_COUNT = "wordCount"; public static final String CHARACTER = "character"; public static final String IS_LEAF = "isLeaf"; public static final String OTHER = "other"; - public Node() {} + public Node() { + } - public Node(Node prev, Character c) { + private Node(Node prev, char c) { parent = new WeakReference<>(prev); character = c; } - public Node get(Character key) { return map.get(key); } + public Node get(char key) { + return nextNodes.get(key); + } - public Node remove(Character key) { return map.remove(key); } + public Node remove(char key) { + return nextNodes.remove(key); + } - public boolean containsKey(Character key) { return map.containsKey(key); } + public boolean containsKey(char key) { + return nextNodes.containsKey(key); + } - public void put(Character key, Node value) { map.put(key, value); } + public void put(char key, Node value) { + nextNodes.put(key, value); + } @Override public boolean equals(Object o) { - if (!(o instanceof Node)) return false; + if (!(o instanceof Node)) return false; Node other = (Node) o; - if (wordCount != other.wordCount) return false; - if (isLeaf != other.isLeaf) return false; - if (character != other.character) return false; - if (!map.keySet().equals(other.map.keySet())) return false; - for (Character c : map.keySet()) { - if (!get(c).equals(other.get(c))) return false; - } - return true; + return wordCount == other.wordCount && + isLeaf == other.isLeaf && + character == other.character && + nextNodes.keySet().equals(other.nextNodes.keySet()) && + nextNodes.equals(other.nextNodes); + } + + @Override + public int hashCode() { + return nextNodes.hashCode() + (isLeaf ? 1 : 0); } public void fromJson(JSONObject obj, Node prev) throws Exception { parent = prev == null ? null : new WeakReference<>(prev); wordCount = ((Long) obj.get(WORD_COUNT)).intValue(); isLeaf = (Boolean) obj.get(IS_LEAF); - character = obj.get(CHARACTER) == null ? null : ((String) obj.get(CHARACTER)).charAt(0); + character = ((String) obj.get(CHARACTER)).charAt(0); JSONObject other = (JSONObject) obj.get(OTHER); - map.clear(); + nextNodes.clear(); for (Object key : other.keySet()) { Node node = new Node(); node.fromJson((JSONObject) other.get(key), this); - put(((String)key).charAt(0), node); + put(node.character, node); } } @@ -153,29 +168,41 @@ public JSONObject toJson() { JSONObject obj = new JSONObject(); obj.put(WORD_COUNT, wordCount); obj.put(IS_LEAF, isLeaf); - obj.put(CHARACTER, character == null ? null : Character.toString(character)); + obj.put(CHARACTER, Character.toString(character)); JSONObject other = new JSONObject(); - for (Character c : map.keySet()) { - other.put(c, get(c).toJson()); + for (Map.Entry entry : nextNodes.entrySet()) { + other.put(entry.getKey(), entry.getValue().toJson()); } obj.put(OTHER, other); return obj; } - public Node next(Character key) { + public Node getNextNodeOrCreateIfNotExists(char key) { if (!containsKey(key)) { put(key, new Node(this, key)); } return get(key); } - public int getSize() { return wordCount; } + public int getSize() { + return wordCount; + } + + public void setSize(int count) { + wordCount = count; + } - public Node getParent() { return parent == null ? null : parent.get(); } + public Node getParent() { + return parent == null ? null : parent.get(); + } - public boolean isLeaf() { return isLeaf; } + public boolean isLeaf() { + return isLeaf; + } - public void setLeaf(boolean value) { isLeaf = value; } + public void setLeaf(boolean value) { + isLeaf = value; + } } } \ No newline at end of file From cfe1aa95c4db2253ba09276e3189137daba525bc Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 20 Mar 2016 16:59:29 +0300 Subject: [PATCH 4/4] deleted idea files --- .gitignore | 1 + hw02.iml | 22 ---------------------- 2 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 hw02.iml diff --git a/.gitignore b/.gitignore index b3c9197..628792e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ build/* .gradle/* +*.iml # Package Files # *.jar diff --git a/hw02.iml b/hw02.iml deleted file mode 100644 index ca7e3c0..0000000 --- a/hw02.iml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file