Skip to content

Commit

Permalink
Remove space from Data Structures package name
Browse files Browse the repository at this point in the history
  • Loading branch information
khalil2535 committed Apr 14, 2018
1 parent 520ee69 commit 82ef795
Show file tree
Hide file tree
Showing 41 changed files with 30,540 additions and 0 deletions.
126 changes: 126 additions & 0 deletions DataStructures/Bags/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package Bags;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Collection which does not allow removing elements (only collect and iterate)
*
* @param <Element> - the generic type of an element in this bag
*/
public class Bag<Element> implements Iterable<Element> {

private Node<Element> firstElement; // first element of the bag
private int size; // size of bag

private static class Node<Element> {
private Element content;
private Node<Element> nextElement;
}

/**
* Create an empty bag
*/
public Bag() {
firstElement = null;
size = 0;
}

/**
* @return true if this bag is empty, false otherwise
*/
public boolean isEmpty() {
return firstElement == null;
}

/**
* @return the number of elements
*/
public int size() {
return size;
}

/**
* @param element - the element to add
*/
public void add(Element element) {
Node<Element> oldfirst = firstElement;
firstElement = new Node<>();
firstElement.content = element;
firstElement.nextElement = oldfirst;
size++;
}

/**
* Checks if the bag contains a specific element
*
* @param element which you want to look for
* @return true if bag contains element, otherwise false
*/
public boolean contains(Element element) {
Iterator<Element> iterator = this.iterator();
while(iterator.hasNext()) {
if (iterator.next().equals(element)) {
return true;
}
}
return false;
}

/**
* @return an iterator that iterates over the elements in this bag in arbitrary order
*/
public Iterator<Element> iterator() {
return new ListIterator<>(firstElement);
}

@SuppressWarnings("hiding")
private class ListIterator<Element> implements Iterator<Element> {
private Node<Element> currentElement;

public ListIterator(Node<Element> firstElement) {
currentElement = firstElement;
}

public boolean hasNext() {
return currentElement != null;
}

/**
* remove is not allowed in a bag
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}

public Element next() {
if (!hasNext())
throw new NoSuchElementException();
Element element = currentElement.content;
currentElement = currentElement.nextElement;
return element;
}
}

/**
* main-method for testing
*/
public static void main(String[] args) {
Bag<String> bag = new Bag<>();

bag.add("1");
bag.add("1");
bag.add("2");

System.out.println("size of bag = " + bag.size());
for (String s : bag) {
System.out.println(s);
}

System.out.println(bag.contains(null));
System.out.println(bag.contains("1"));
System.out.println(bag.contains("3"));
}

}
124 changes: 124 additions & 0 deletions DataStructures/Buffers/CircularBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class CircularBuffer {
private char[] _buffer;
public final int _buffer_size;
private int _write_index = 0;
private int _read_index = 0;
private AtomicInteger _readable_data = new AtomicInteger(0);

public CircularBuffer(int buffer_size) {
if(!IsPowerOfTwo(buffer_size)) {
throw new IllegalArgumentException();
}
this._buffer_size = buffer_size;
_buffer = new char[buffer_size];
}

private boolean IsPowerOfTwo(int i) {
return (i & (i - 1)) == 0;
}

private int getTrueIndex(int i) {
return i % _buffer_size;
}

public Character readOutChar() {
Character result = null;

//if we have data to read
if(_readable_data.get() > 0) {
result = new Character(_buffer[getTrueIndex(_read_index)]);
_readable_data.decrementAndGet();
_read_index++;
}

return result;
}

public boolean writeToCharBuffer(char c) {
boolean result = false;

//if we can write to the buffer
if(_readable_data.get() < _buffer_size) {
//write to buffer
_buffer[getTrueIndex(_write_index)] = c;
_readable_data.incrementAndGet();
_write_index++;
result = true;
}

return result;
}

private static class TestWriteWorker implements Runnable {
String _alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
Random _random = new Random();
CircularBuffer _buffer;
public TestWriteWorker(CircularBuffer cb) {
this._buffer = cb;
}

private char getRandomChar() {
return _alphabet.charAt(_random.nextInt(_alphabet.length()));
}

public void run() {
while(!Thread.interrupted()) {
if(!_buffer.writeToCharBuffer(getRandomChar())){
Thread.yield();
try{
Thread.sleep(10);
} catch (InterruptedException e) {
return;
}
}
}
}
}

private static class TestReadWorker implements Runnable {
CircularBuffer _buffer;
public TestReadWorker(CircularBuffer cb) {
this._buffer = cb;
}

public void run() {
System.out.println("Printing Buffer:");
while(!Thread.interrupted()) {
Character c = _buffer.readOutChar();
if(c != null) {
System.out.print(c.charValue());
} else {
Thread.yield();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println();
return;
}
}
}
}
}

public static void main(String[] args) throws InterruptedException {
int buffer_size = 1024;
//create circular buffer
CircularBuffer cb = new CircularBuffer(buffer_size);

//create threads that read and write the buffer.
Thread write_thread = new Thread(new TestWriteWorker(cb));
Thread read_thread = new Thread(new TestReadWorker(cb));
read_thread.start();
write_thread.start();

//wait some amount of time
Thread.sleep(10000);

//interrupt threads and exit
write_thread.interrupt();
read_thread.interrupt();
}
}
Loading

0 comments on commit 82ef795

Please sign in to comment.