Monday, September 7, 2015

Dynamic arrays



http://www.algolist.net/Data_structures/Dynamic_array
public class DynamicArray {
      private int[] storage;
      private int size;

      public DynamicArray() {
            storage = new int[10];
            size = 0;
      }

      public DynamicArray(int capacity) {
            storage = new int[capacity];
            size = 0;
      }
}
Capacity management: Ensure Capacity, Pack
Pack
When items are removed, amount of the free space increases. If there are too few values in the dynamic array, unused storage become just a waste of space. For the purpose of saving space, we develop a mechanism to reduce capacity, when it is excessive.
  • check, if size is less or equal, than half of the capacity;
  • calculate new capacity by the formula: newCapacity = (size * 3) / 2 + 1. Algorithm leaves exact the amount of space, as if storage capacity had been trimmed to the size and then method to ensure capacity was called.
Lower boundary for size, after which packing is done, may vary. In the current example it is 0.5 of the capacity value. Commonly, pack is a private method, which is called after removal. Also, dynamic array interface provides a trim method, which reduces capacity to fit exact amount of items in the array. It is done from the outside of the implementation, when you are sure, that no more values to be added (for instance, input from user is over).
      public void ensureCapacity(int minCapacity) {
            int capacity = storage.length;
            if (minCapacity > capacity) {
                  int newCapacity = (capacity * 3) / 2 + 1;
                  if (newCapacity < minCapacity)
                        newCapacity = minCapacity;
                  storage = Arrays.copyOf(storage, newCapacity);
            }
      }

      private void pack() {
            int capacity = storage.length;
            if (size <= capacity / 2) {
                  int newCapacity = (size * 3) / 2 + 1;
                  storage = Arrays.copyOf(storage, newCapacity);
            }
      }

      public void trim() {
            int newCapacity = size;
            storage = Arrays.copyOf(storage, newCapacity);
      }

http://www.algolist.net/Data_structures/Dynamic_array/Access_functions
InsertAt
This operation may require array expanding, so algorithm invokes ensure capacity method first, which should ensure size + 1 minimal capacity. Then shift all elements from i to size - 1, where i is the insertion position, one element right. Note, that if new element is inserted after the last element in the array, then no shifting required. After shifting, put the value to i-th element and increase size by one.

RemoveAt
Shift all elements from i to size - 1, where i is the removal position, one element left. Then decrease size by 1 and invoke pack opeartion. Packing is done, if there are too few elements left after removal.

      private void rangeCheck(int index) {
            if (index < 0 || index >= size)
                  throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
                             + size);
      }
     
      public void set(int index, int value)
      {
            rangeCheck(index);
            storage[index] = value;
      }
     
      public int get(int index)
      {
            rangeCheck(index);
            return storage[index];
      }

      public void removeAt(int index) {
            rangeCheck(index);
            int moveCount = size - index - 1;
            if (moveCount > 0)
                  System.arraycopy(storage, index + 1, storage, index, moveCount);
            size--;
            pack();
      }

      public void insertAt(int index, int value) {
            if (index < 0 || index > size)
                  throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
                             + size);
            ensureCapacity(size + 1);
            int moveCount = size - index;
            if (moveCount > 0)
                  System.arraycopy(storage, index, storage, index + 1, moveCount);
            storage[index] = value;
            size++;
      }

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts