From 75aefdb4251e7556b2b28806992ad8a0016c2f40 Mon Sep 17 00:00:00 2001 From: Dasha Date: Sat, 28 Feb 2026 21:39:39 +0300 Subject: [PATCH 1/2] hw 2 --- .../practice/randomSet/RandomSet.java | 229 +++++++++++++++++- 1 file changed, 220 insertions(+), 9 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java index 8af477b5..63af5fdf 100644 --- a/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java +++ b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java @@ -1,21 +1,232 @@ package hse.java.lectures.lecture3.practice.randomSet; +import java.util.Random; + public class RandomSet { + private DynamicArray elements; + private SimpleHashMap indexMap; + private Random random; + + public RandomSet() { + elements = new DynamicArray<>(); + indexMap = new SimpleHashMap<>(); + random = new Random(); + } + + public boolean insert(T value) { + if (value == null) { + throw new NullPointerException("Value cannot be null"); + } + if (indexMap.containsKey(value)) { + return false; + } + int idx = elements.size(); + elements.add(value); + indexMap.put(value, idx); + return true; + } + + public boolean remove(T value) { + if (value == null) { + throw new NullPointerException("Value cannot be null"); + } + Integer idx = indexMap.get(value); + if (idx == null) { + return false; + } + + int lastIdx = elements.size() - 1; + + if (idx != lastIdx) { + T lastElement = elements.get(lastIdx); + elements.set(idx, lastElement); + indexMap.put(lastElement, idx); + } + + elements.removeLast(); + indexMap.remove(value); + + return true; + } + + public boolean contains(T value) { + if (value == null) { + throw new NullPointerException("Value cannot be null"); + } + return indexMap.containsKey(value); + } + + public T getRandom() { + if (elements.size() == 0) { + throw new EmptySetException("Cannot get random element from empty set"); + } + int idx = random.nextInt(elements.size()); + return elements.get(idx); + } + + private static class DynamicArray { + private T[] data; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 16; + + @SuppressWarnings("unchecked") + DynamicArray() { + capacity = DEFAULT_CAPACITY; + data = (T[]) new Object[capacity]; + size = 0; + } - public boolean insert(T value) { - throw new UnsupportedOperationException("Not implemented"); + int size() { return size; } + + void add(T value) { + if (size == capacity) { + resize(); + } + data[size++] = value; + } + + T get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + + ", Size: " + size); + } + return data[index]; + } + + void set(int index, T value) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + + ", Size: " + size); + } + data[index] = value; + } + + T removeLast() { + if (size == 0) { + throw new IndexOutOfBoundsException("Cannot remove from empty array"); + } + T removed = data[--size]; + data[size] = null; + return removed; + } + + @SuppressWarnings("unchecked") + private void resize() { + capacity *= 2; + T[] newData = (T[]) new Object[capacity]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + } + + private static class SimpleHashMap { + private static class Entry { + T key; + int value; + Entry next; + + Entry(T key, int value, Entry next) { + this.key = key; + this.value = value; + this.next = next; + } + } + + private Entry[] table; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 16; + private static final double LOAD_FACTOR = 0.75; + + @SuppressWarnings("unchecked") + SimpleHashMap() { + capacity = DEFAULT_CAPACITY; + table = (Entry[]) new Entry[capacity]; + size = 0; } - public boolean remove(T value) { - throw new UnsupportedOperationException("Not implemented"); + private int hash(T key) { + if (key == null) + return 0; + int h = key.hashCode(); + h ^= (h >>> 16); + return (h & 0x7fffffff) % capacity; } - public boolean contains(T value) { - throw new UnsupportedOperationException("Not implemented"); + boolean containsKey(T key) { + int idx = hash(key); + Entry entry = table[idx]; + while (entry != null) { + if (entry.key.equals(key)) { + return true; + } + entry = entry.next; + } + return false; } - public T getRandom() { - throw new UnsupportedOperationException("Not implemented"); + Integer get(T key) { + int idx = hash(key); + Entry entry = table[idx]; + while (entry != null) { + if (entry.key.equals(key)) { + return entry.value; + } + entry = entry.next; + } + return null; } -} + void put(T key, int value) { + int idx = hash(key); + Entry entry = table[idx]; + while (entry != null) { + if (entry.key.equals(key)) { + entry.value = value; + return; + } + entry = entry.next; + } + table[idx] = new Entry<>(key, value, table[idx]); + size++; + if (size > capacity * LOAD_FACTOR) { + resize(); + } + } + + void remove(T key) { + int idx = hash(key); + Entry entry = table[idx]; + Entry prev = null; + while (entry != null) { + if (entry.key.equals(key)) { + if (prev == null) { + table[idx] = entry.next; + } else { + prev.next = entry.next; + } + size--; + return; + } + prev = entry; + entry = entry.next; + } + } + + @SuppressWarnings("unchecked") + private void resize() { + Entry[] oldTable = table; + capacity *= 2; + table = (Entry[]) new Entry[capacity]; + size = 0; + for (Entry head : oldTable) { + Entry entry = head; + while (entry != null) { + put(entry.key, entry.value); + entry = entry.next; + } + } + } + } +} \ No newline at end of file From d351c48e7bba05b48188cebe5b4bdae25a33b11d Mon Sep 17 00:00:00 2001 From: Dasha Date: Sat, 28 Feb 2026 21:44:26 +0300 Subject: [PATCH 2/2] randomset: commit name --- .../hse/java/lectures/lecture3/practice/randomSet/RandomSet.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java index 63af5fdf..07f4655e 100644 --- a/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java +++ b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/RandomSet.java @@ -6,7 +6,6 @@ public class RandomSet { private DynamicArray elements; private SimpleHashMap indexMap; private Random random; - public RandomSet() { elements = new DynamicArray<>(); indexMap = new SimpleHashMap<>();