HW2: done Ex1

This commit is contained in:
Claudio Maggioni 2019-11-10 17:13:34 +01:00
parent 67dbbe3f64
commit c80deb247a
21 changed files with 1208 additions and 0 deletions

View File

12
hw2/Ex1/.idea/Ex1.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

9
hw2/Ex1/.idea/misc.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_12" default="false" project-jdk-name="12.0.1" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Ex1.iml" filepath="$PROJECT_DIR$/.idea/Ex1.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
hw2/Ex1/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

383
hw2/Ex1/.idea/workspace.xml Normal file
View File

@ -0,0 +1,383 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b727764e-c290-4ab7-a944-02a848fa4a41" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/bintree/BinTreeFullSync.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../hw1/Ex1/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../hw1/Ex1/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../hw1/Ex2/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../hw1/Ex2/.idea/workspace.xml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DefaultGradleProjectSettings">
<option name="isMigrated" value="true" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/bintree/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="495">
<caret line="33" column="13" lean-forward="true" selection-start-line="33" selection-start-column="13" selection-end-line="33" selection-end-column="13" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTree.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="17" selection-start-line="2" selection-start-column="17" selection-end-line="2" selection-end-column="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeCAS.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="705">
<caret line="48" selection-start-line="30" selection-end-line="48" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="17" lean-forward="true" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSync.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="20" column="14" selection-start-line="20" selection-start-column="14" selection-end-line="20" selection-end-column="14" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeSimple.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="825">
<caret line="55" column="5" lean-forward="true" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>root</find>
<find>OCCUR</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/bintree/BinTreeSimple.java" />
<option value="$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java" />
<option value="$PROJECT_DIR$/src/bintree/BinTreeFullSync.java" />
<option value="$PROJECT_DIR$/src/bintree/BinTreeCAS.java" />
<option value="$PROJECT_DIR$/src/bintree/Tester.java" />
</list>
</option>
</component>
<component name="ProjectConfigurationFiles">
<option name="files">
<list>
<option value="$PROJECT_DIR$/.idea/Ex1.iml" />
<option value="$PROJECT_DIR$/.idea/misc.xml" />
<option value="$PROJECT_DIR$/.idea/vcs.xml" />
<option value="$PROJECT_DIR$/.idea/modules.xml" />
<option value="$PROJECT_DIR$/.idea/uiDesigner.xml" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" fullScreen="true">
<option name="width" value="1680" />
<option name="height" value="1050" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="PackagesPane" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="bintree" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="project.structure.last.edited" value="Project" />
<property name="project.structure.proportion" value="0.15" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="settings.editor.selected.configurable" value="reference.settingsdialog.IDE.editor.colors" />
</component>
<component name="RecentsManager">
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="bintree" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Tester" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="bintree.Tester" />
<module name="Ex1" />
<option name="VM_PARAMETERS" value="-Xms512M -Xmx10G" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="bintree.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Tester" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="b727764e-c290-4ab7-a944-02a848fa4a41" name="Default Changelist" comment="" />
<created>1573115638539</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1573115638539</updated>
<workItem from="1573115639742" duration="3855000" />
<workItem from="1573398260802" duration="3267000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="7122000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1680" height="1050" extended-state="0" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.22039072" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Designer" order="2" />
<window_info id="Image Layers" order="3" />
<window_info id="Capture Tool" order="4" />
<window_info id="UI Designer" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info id="Learn" order="7" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32959184" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info anchor="bottom" id="Docker" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="Event Log" order="9" side_tool="true" />
<window_info anchor="bottom" id="Messages" order="10" weight="0.32959184" />
<window_info anchor="bottom" id="Database Changes" order="11" />
<window_info anchor="bottom" id="Version Control" order="12" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Maven" order="3" />
<window_info anchor="right" id="Palette" order="4" />
<window_info anchor="right" id="Capture Analysis" order="5" />
<window_info anchor="right" id="Database" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
<window_info anchor="right" id="Theme Preview" order="8" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTree.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="17" selection-start-line="2" selection-start-column="17" selection-end-line="2" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeCAS.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="705">
<caret line="48" selection-start-line="30" selection-end-line="48" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSync.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="20" column="14" selection-start-line="20" selection-start-column="14" selection-end-line="20" selection-end-column="14" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeSimple.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="825">
<caret line="55" column="5" lean-forward="true" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="17" lean-forward="true" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="495">
<caret line="33" column="13" lean-forward="true" selection-start-line="33" selection-start-column="13" selection-end-line="33" selection-end-column="13" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<last-edited>Ex1:jar</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<last-edited>12</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>Ex1</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

@ -0,0 +1,18 @@
package bintree;
public interface BinTree {
/**
* Inserts value in this tree
* @param value
*/
void insert(int value);
/**
* Returns the number of occurrences of value in this tree
* @param value
* @return the number of occurrences of value in the tree
*/
int occurs(int value);
}

View File

@ -0,0 +1,64 @@
package bintree;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class BinTreeCAS implements BinTree {
private static class Node {
final int value;
AtomicInteger occ;
AtomicReference<Node> left, right;
private Node(int value, int initOcc) {
this.value = value;
occ = new AtomicInteger(initOcc);
left = new AtomicReference<>();
left.set(null);
right = new AtomicReference<>();
right.set(null);
}
}
private final Node root = new Node(0, 0);
public void insert(int value) {
Node n = root;
while (true) {
int nval = n.value;
if (value == nval) {
n.occ.addAndGet(1);
return;
}
else if (value < nval) {
if (n.left.compareAndSet(null, new Node(value, 1))) {
return;
} else {
n = n.left.get();
}
}
else { // value > nval
if (n.right.compareAndSet(null, new Node(value, 1))) {
return;
} else {
n = n.right.get();
}
}
}
}
public int occurs(int value) {
Node n = root;
do {
int nval = n.value;
if (value == nval) {
return n.occ.intValue();
}
n = (value < nval) ? n.left.get() : n.right.get();
} while (n != null);
// value not found
return 0;
}
}

View File

@ -0,0 +1,55 @@
package bintree;
public class BinTreeFullSync implements BinTree {
private static class Node {
final int value;
int occ;
Node left, right;
private Node(int value, int initOcc) {
this.value = value;
occ = initOcc;
}
}
private final Node root = new Node(0, 0);
public synchronized void insert(int value) {
Node n = root;
while (true) {
int nval = n.value;
if (value == nval) {
n.occ++;
return;
}
else if (value < nval) {
if (n.left == null) {
n.left = new Node(value, 1);
return;
}
n = n.left;
}
else { // value > nval
if (n.right == null) {
n.right = new Node(value, 1);
return;
}
n = n.right;
}
}
}
public synchronized int occurs(int value) {
Node n = root;
do {
int nval = n.value;
if (value == nval) {
return n.occ++;
}
n = (value < nval) ? n.left : n.right;
} while (n != null);
// value not found
return 0;
}
}

View File

@ -0,0 +1,71 @@
package bintree;
public class BinTreeFullSyncEdited implements BinTree {
private static class Node {
final int value;
int occ;
Node left, right;
private Node(int value, int initOcc) {
this.value = value;
occ = initOcc;
}
private synchronized int getOcc() {
return occ;
}
private synchronized void incrementOcc() {
occ++;
}
private synchronized Node goLeftOrNull(int value) {
if (this.left == null) {
this.left = new Node(value, 1);
return null;
}
return this.left;
}
private synchronized Node goRightOrNull(int value) {
if (this.right == null) {
this.right = new Node(value, 1);
return null;
}
return this.right;
}
}
private final Node root = new Node(0, 0);
public void insert(int value) {
Node n = root;
while (n != null) {
int nval = n.value;
if (value == nval) {
n.incrementOcc();
return;
}
else if (value < nval) {
n = n.goLeftOrNull(value);
}
else { // value > nval
n = n.goRightOrNull(value);
}
}
}
public int occurs(int value) {
Node n = root;
do {
int nval = n.value;
if (value == nval) {
return n.getOcc();
}
n = (value < nval) ? n.left : n.right;
} while (n != null);
// value not found
return 0;
}
}

View File

@ -0,0 +1,57 @@
package bintree;
import java.util.concurrent.atomic.AtomicInteger;
public class BinTreeSimple implements BinTree {
private static class Node {
final int value;
int occ;
Node left, right;
private Node(int value, int initOcc) {
this.value = value;
occ = initOcc;
}
}
private final Node root = new Node(0, 0);
public void insert(int value) {
Node n = root;
while (true) {
int nval = n.value;
if (value == nval) {
n.occ++;
return;
}
else if (value < nval) {
if (n.left == null) {
n.left = new Node(value, 1);
return;
}
n = n.left;
}
else { // value > nval
if (n.right == null) {
n.right = new Node(value, 1);
return;
}
n = n.right;
}
}
}
public int occurs(int value) {
Node n = root;
do {
int nval = n.value;
if (value == nval) {
return n.occ++;
}
n = (value < nval) ? n.left : n.right;
} while (n != null);
// value not found
return 0;
}
}

View File

@ -0,0 +1,97 @@
package bintree;
import java.util.concurrent.CountDownLatch;
public class Tester {
private static final int NUM_THREADS = 1024;
private static final int NUM_ITERATIONS = 64;
private static final int OCCUR = 32;
private final BinTree tree;
private final CountDownLatch threadsReady,
measurementStarted, threadsCompleted;
private class TesterThread extends Thread {
private final int startValue;
TesterThread(int startValue) {
this.startValue = startValue;
}
public void run() {
int iter = NUM_ITERATIONS * OCCUR;
int nextValue = startValue;
threadsReady.countDown();
try {
measurementStarted.await();
}
catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
for (int i = 0; i < iter; i++) {
tree.insert(nextValue);
nextValue = (nextValue + 1) % NUM_ITERATIONS;
}
threadsCompleted.countDown();
}
}
public Tester(BinTree tree) {
this.tree = tree;
this.threadsReady = new CountDownLatch(NUM_THREADS);
this.measurementStarted = new CountDownLatch(1);
this.threadsCompleted = new CountDownLatch(NUM_THREADS);
}
private long runMeasurement() throws InterruptedException {
for (int i = 0; i < NUM_THREADS; ++i) {
Thread t = new TesterThread( NUM_ITERATIONS / 2);
t.start();
}
threadsReady.await();
long timeBegin = System.nanoTime();
measurementStarted.countDown();
threadsCompleted.await();
long timeEnd = System.nanoTime();
return timeEnd - timeBegin;
}
private void checkTree() {
int expectedOccur = OCCUR * NUM_THREADS;
for (int i = 0; i < NUM_ITERATIONS; i++) {
int foundOccur;
if ((foundOccur = tree.occurs(i)) != expectedOccur) {
System.err.printf(" - ERROR: Expected: %d ; Found: %d\n",
expectedOccur, foundOccur);
}
}
}
private static void testTreeImplementation(BinTree implementation,
String name) throws Exception {
System.out.println("Results for implementation: " + name);
Tester tester = new Tester(implementation);
long elapsedTime = tester.runMeasurement();
tester.checkTree();
System.out.printf(" - Test completed, elapsed time: %d ms\n",
(elapsedTime / 1000000));
}
public static void main(String[] args) throws Exception {
//testTreeImplementation(new BinTreeSimple(), "Simple version");
testTreeImplementation(new BinTreeFullSyncEdited(), "Synchronized version");
testTreeImplementation(new BinTreeFullSyncEdited(), "Synchronized version (Edited)");
testTreeImplementation(new BinTreeCAS(), "CAS version");
}
}

View File

@ -0,0 +1,55 @@
package parallel;
import util.Prime;
import java.util.List;
import util.PrimeCounter;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class CallablePrimeCounter implements PrimeCounter {
private final int numThreads;
public CallablePrimeCounter(int numThreads) {
this.numThreads = numThreads;
}
public int countPrimes(int[] nums) throws Exception {
int count = 0;
List<FutureTask<Integer>> futureTasks = new ArrayList<>(numThreads);
int size = (int) Math.ceil((double) nums.length / numThreads);
for (int i = 0; i < numThreads; i++) {
FutureTask<Integer> future =
new FutureTask<>(new CountPrimes(nums, i * size, (i + 1) * size));
futureTasks.add(future);
new Thread(future).start();
}
for (FutureTask<Integer> task : futureTasks) {
count += task.get();
}
return count;
}
private static class CountPrimes implements Callable<Integer> {
private final int[] nums;
private final int low, high;
private CountPrimes(int[] nums, int low, int high) {
this.nums = nums;
this.low = low;
this.high = Math.min(high, nums.length);
}
@Override
public Integer call() throws Exception {
int count = 0;
for (int i = low; i < high; i++) {
if (Prime.isPrime(nums[i])) {
count++;
}
}
return count;
}
}
}

View File

@ -0,0 +1,62 @@
package parallel;
import util.Prime;
import util.PrimeCounter;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class ForkJoinPoolPrimeCounter implements PrimeCounter {
private static final int SEQUENTIAL_THRESHOLD = 1000;
private final int numThreads;
public ForkJoinPoolPrimeCounter(int numThreads) {
this.numThreads = numThreads;
}
public int countPrimes(int[] nums) {
int count = 0;
// TODO: Complete method countPrimes
return count;
}
private static class CountPrimes extends RecursiveTask<Integer> {
private final int low, high;
private final int[] nums;
public CountPrimes(int[] nums, int low, int high) {
this.nums = nums;
this.low = low;
this.high = high;
}
@Override
protected Integer compute() {
if (low > high) {
return 0;
}
if ((high - low) <= SEQUENTIAL_THRESHOLD) {
int count = 0;
for (int i = low; i <= high; i++) {
if (Prime.isPrime(nums[i])) {
count++;
}
}
return count;
}
else {
int right, left, rightAns, leftAns;
right = left = rightAns = leftAns = 0;
right = low + ((int) Math.floor((high - low) / 2 ) - 1);
left = low + ((int) Math.ceil((high - low) / 2 ));
// TODO: Complete method compute
return leftAns + rightAns;
}
}
}
}

View File

@ -0,0 +1,49 @@
package parallel;
import util.Prime;
import java.util.List;
import util.PrimeCounter;
import java.util.ArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ThreadPoolPrimeCounter implements PrimeCounter {
private final int numThreads;
public ThreadPoolPrimeCounter(int numThreads) {
this.numThreads = numThreads;
}
public int countPrimes(int[] nums) throws Exception {
int count = 0;
// TODO: Complete method countPrimes
return count;
}
private static class CountPrimes implements Callable<Integer> {
private final int[] nums;
private final int low, high;
private CountPrimes(int[] nums, int low, int high) {
this.nums = nums;
this.low = low;
this.high = Math.min(high, nums.length);
}
@Override
public Integer call() throws Exception {
int count = 0;
for (int i = low; i < high; i++) {
if (Prime.isPrime(nums[i])) {
count++;
}
}
return count;
}
}
}

View File

@ -0,0 +1,17 @@
package sequential;
import util.Prime;
import util.PrimeCounter;
public class SequentialPrimeCounter implements PrimeCounter {
public int countPrimes(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (Prime.isPrime(nums[i])) {
count++;
}
}
return count;
}
}

View File

@ -0,0 +1,73 @@
package test;
import util.PrimeCounter;
import java.util.stream.IntStream;
import parallel.CallablePrimeCounter;
import parallel.ThreadPoolPrimeCounter;
import sequential.SequentialPrimeCounter;
import parallel.ForkJoinPoolPrimeCounter;
public class Tester {
private static final int NUM_THREADS = 8;
public static final int LOW = 2;
public static final int HIGH = 11201609;
private PrimeCounter strategy;
private void setStrategy(PrimeCounter strategy) {
this.strategy = strategy;
}
private int countPrimes(int[] array) {
return new SequentialPrimeCounter().countPrimes(array);
}
private void testImplementation(int[] array, String name)
throws Exception {
long startTime = System.nanoTime();
int count = strategy.countPrimes(array);
long endTime = System.nanoTime();
long result = countPrimes(array);
if (count != result) {
// Informing if the implementation failed
System.err.printf("- ERROR: %s; Expected: %d; Found: %d\n",
name, result, count);
} else {
// Showing the time taken by the implementation
System.out.printf("- %s: %d prime numbers found, "
+ "elapsed time: %d ms\n" , name, count,
((endTime - startTime) / 1000000));
}
}
public static void main(String[] args) throws Exception {
//Generating an integer array with numbers in the range from LOW to HIGH
int[] numbers = IntStream.rangeClosed(LOW, HIGH).toArray();
Tester tester = new Tester();
// Testing different implementations:
// Sequential implementation
tester.setStrategy(new SequentialPrimeCounter());
tester.testImplementation(numbers,"Sequential version");
// Parallel - Callable implementation
tester.setStrategy(new CallablePrimeCounter(NUM_THREADS));
tester.testImplementation(numbers, "Parallel - Callable version");
// TODO: Uncomment to test additional implementations
/*
// Parallel - ThreadPool implementation
tester.setStrategy(new ThreadPoolPrimeCounter(NUM_THREADS));
tester.testImplementation(numbers, "Parallel - ThreadPool version");
// Parallel - ForkJoinPool implementation - Ex. 4
tester.setStrategy(new ForkJoinPoolPrimeCounter(NUM_THREADS));
tester.testImplementation(numbers, "Parallel - ForkJoinPool version");
// Parallel - Streams implementation - BONUS
//tester.setStrategy(new StreamsPrimeCounter());
//tester.testImplementation(numbers, "Parallel - Streams version");
*/
}
}

View File

@ -0,0 +1,19 @@
package util;
public class Prime {
public static boolean isPrime(int n) {
if (n <= 2) {
return n == 2;
}
if ((n&1) == 0) {
return false;
}
for (int i = 3; i*i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,6 @@
package util;
@FunctionalInterface
public interface PrimeCounter {
int countPrimes(int[] nums) throws Exception;
}

23
hw2/submission.tex Normal file
View File

@ -0,0 +1,23 @@
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=2cm]{geometry}
\title{Howework 2 -- Programming Fundamentals 3}
\author{Claudio Maggioni}
\begin{document}
\maketitle
\tableofcontents
\section{Exercise 1}
\subsection{Question 2}
Results returned by \texttt{BinTreeSimple} are wrong since the class is not thread safe. In particular, \texttt{BinTreeSimple.insert(int)} has race conditions on the integer field occ of each node, and the fields left and right on each node. To fix this, an AtomicInteger could be used for occ and access on n.left and n.right could be synchronized.
\subsection{Question 3: Note on implementation}
I have implemented the class \texttt{BinTreeFullSync}
as a thread-safe alternative of \texttt{BinTreeSimple} where the only code modification between the two were the addition of \texttt{synchronized} keywords on already existing threads. Since I was not sure how to interpret this specific question, I have implemented \texttt{BinTreeFullSyncEdited}, a thread-safe refactoring of \texttt{BinTreeSimple} where the only synchronization mechanism is method-level \texttt{synchronized} blocks, which are used on new methods created in the subclass \texttt{Node}.
\subsection{Question 5}
\texttt{BinTreeFullSync} has the worst performance of all 3 implementations since it allows only sequential access to the tree structure. \texttt{BinTreeFullSyncEdited} is slightly faster, but \texttt{BinTreeCAS} is the fastest since the first one is a \texttt{synchronized} block imitation of the second one, which is instead based on \texttt{Atomic*} objects and can use the extra speed provided by architecture-based optimization.
\end{document}