Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions SmartList/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
16 changes: 16 additions & 0 deletions SmartList/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions SmartList/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions SmartList/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions SmartList/ds.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.0" level="project" />
</component>
</module>
41 changes: 41 additions & 0 deletions SmartList/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.spbau.mit.kazakov.SpiralMatrix</groupId>
<artifactId>SmartList</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>

</project>
192 changes: 192 additions & 0 deletions SmartList/src/main/java/ru/spbau/mit/kazakov/SmartList/SmartList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package ru.spbau.mit.kazakov.SmartList;

import org.apache.commons.collections4.iterators.EmptyIterator;
import org.apache.commons.collections4.iterators.SingletonIterator;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
* Smart list storage. Store one element using one reference, 2-5 element using an array, and more elements using ArrayList.
* @param <E> generic type
*/
public class SmartList<E> extends AbstractList<E> implements List<E> {
private int size;
private Object data;

/**
* Creates new empty list.
*/
public SmartList() {
size = 0;
data = null;
}

/**
* Creates new list containing elements from specified collection.
*
* @param collection specified collection
*/
public SmartList(@NotNull Collection<? extends E> collection) {
for (E elem : collection) {
add(elem);
}
}

/**
* Returns elements by specified index.
*
* @param i specified index
*/
@SuppressWarnings("unchecked")
@Override
public E get(int i) {
if (i >= size) {
throw new IndexOutOfBoundsException();
}

if (size == 1) {
return (E) data;
} else if (size <= 5) {
return (E) ((Object[]) data)[i];
} else {
return (E) ((ArrayList<Object>) data).get(i);
}
}

@Override
public int size() {
return size;
}

/**
* Removes element by specified index
*
* @param i specified index
* @return removed element
* @throws IndexOutOfBoundsException if there is no element with specified index
*/
@Override
@SuppressWarnings("unchecked")
public E remove(int i) throws IndexOutOfBoundsException {
if (i >= size) {
throw new IndexOutOfBoundsException();
}

E returnValue;
if (size == 1) {
returnValue = (E) data;
data = null;
} else if (size == 2) {
returnValue = (E) ((Object[]) data)[i];
data = i == 0 ? ((Object[]) data)[1] : ((Object[]) data)[0];
} else if (size <= 5) {
returnValue = (E) ((Object[]) data)[i];
} else if (size == 6) {
returnValue = (E) ((ArrayList<Object>) data).get(i);
Object[] array = new Object[5];
int j = 0;
while (j != 4) {
int cur = 0;
if (j != i) {
array[cur] = ((ArrayList<Object>) data).get(j);
cur++;
}
j++;
}
data = array;
} else {
returnValue = (E) ((ArrayList<Object>) data).remove(i);
}

size--;
return returnValue;
}

/**
* Adds specified element to list.
*
* @param elem specified element
* @return true
*/
@Override
public boolean add(@NotNull E elem) {
size++;

if (size == 1) {
data = elem;
} else if (size == 2) {
Object[] array = new Object[5];
array[0] = this.data;
array[1] = elem;
data = array;
} else if (size <= 5) {
((Object[]) data)[size - 1] = elem;
} else if (size == 6) {
ArrayList<Object> list = new ArrayList<>(Arrays.asList((Object[]) data));
list.add(elem);
data = list;
} else {
//noinspection unchecked
((ArrayList<Object>) data).add(elem);
}

return true;
}

/**
* Checks if there is specified element in list
*
* @param elem specified element
* @return true if there is such element, and false otherwise
*/
@Override
public boolean contains(@NotNull Object elem) {
if (size == 1) {
return data.equals(elem);
} else if (size <= 5) {
boolean result = false;
for (Object i : (Object[]) data) {
result |= i.equals(elem);
}
return result;
} else {
boolean result = false;
//noinspection unchecked
for (Object i : (ArrayList<Object>) data) {
result |= i.equals(elem);
}
return result;
}
}

/**
* Replaces the element at the specified index with the specified element.
*
* @param i specified position
* @param elem specified element
* @return the element previously at the specified index
*/
@Override
@SuppressWarnings("unchecked")
public E set(int i, @NotNull E elem) {
if (i >= size) {
throw new IndexOutOfBoundsException();
}

E returnValue;
if (size == 1) {
returnValue = (E) data;
data = elem;
} else if (size <= 5) {
returnValue = (E) ((Object[]) data)[i];
((Object[]) data)[i] = elem;
} else {
returnValue = (E) ((ArrayList<Object>) data).get(i);
((ArrayList<Object>) data).set(i, elem);
}

return returnValue;
}

}
Loading