Skip to content

Commit 82ef795

Browse files
author
khalil2535
committed
Remove space from Data Structures package name
1 parent 520ee69 commit 82ef795

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+30540
-0
lines changed

DataStructures/Bags/Bag.java

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package Bags;
2+
3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
5+
6+
/**
7+
* Collection which does not allow removing elements (only collect and iterate)
8+
*
9+
* @param <Element> - the generic type of an element in this bag
10+
*/
11+
public class Bag<Element> implements Iterable<Element> {
12+
13+
private Node<Element> firstElement; // first element of the bag
14+
private int size; // size of bag
15+
16+
private static class Node<Element> {
17+
private Element content;
18+
private Node<Element> nextElement;
19+
}
20+
21+
/**
22+
* Create an empty bag
23+
*/
24+
public Bag() {
25+
firstElement = null;
26+
size = 0;
27+
}
28+
29+
/**
30+
* @return true if this bag is empty, false otherwise
31+
*/
32+
public boolean isEmpty() {
33+
return firstElement == null;
34+
}
35+
36+
/**
37+
* @return the number of elements
38+
*/
39+
public int size() {
40+
return size;
41+
}
42+
43+
/**
44+
* @param element - the element to add
45+
*/
46+
public void add(Element element) {
47+
Node<Element> oldfirst = firstElement;
48+
firstElement = new Node<>();
49+
firstElement.content = element;
50+
firstElement.nextElement = oldfirst;
51+
size++;
52+
}
53+
54+
/**
55+
* Checks if the bag contains a specific element
56+
*
57+
* @param element which you want to look for
58+
* @return true if bag contains element, otherwise false
59+
*/
60+
public boolean contains(Element element) {
61+
Iterator<Element> iterator = this.iterator();
62+
while(iterator.hasNext()) {
63+
if (iterator.next().equals(element)) {
64+
return true;
65+
}
66+
}
67+
return false;
68+
}
69+
70+
/**
71+
* @return an iterator that iterates over the elements in this bag in arbitrary order
72+
*/
73+
public Iterator<Element> iterator() {
74+
return new ListIterator<>(firstElement);
75+
}
76+
77+
@SuppressWarnings("hiding")
78+
private class ListIterator<Element> implements Iterator<Element> {
79+
private Node<Element> currentElement;
80+
81+
public ListIterator(Node<Element> firstElement) {
82+
currentElement = firstElement;
83+
}
84+
85+
public boolean hasNext() {
86+
return currentElement != null;
87+
}
88+
89+
/**
90+
* remove is not allowed in a bag
91+
*/
92+
@Override
93+
public void remove() {
94+
throw new UnsupportedOperationException();
95+
}
96+
97+
public Element next() {
98+
if (!hasNext())
99+
throw new NoSuchElementException();
100+
Element element = currentElement.content;
101+
currentElement = currentElement.nextElement;
102+
return element;
103+
}
104+
}
105+
106+
/**
107+
* main-method for testing
108+
*/
109+
public static void main(String[] args) {
110+
Bag<String> bag = new Bag<>();
111+
112+
bag.add("1");
113+
bag.add("1");
114+
bag.add("2");
115+
116+
System.out.println("size of bag = " + bag.size());
117+
for (String s : bag) {
118+
System.out.println(s);
119+
}
120+
121+
System.out.println(bag.contains(null));
122+
System.out.println(bag.contains("1"));
123+
System.out.println(bag.contains("3"));
124+
}
125+
126+
}
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.util.Random;
2+
import java.util.concurrent.atomic.AtomicInteger;
3+
4+
public class CircularBuffer {
5+
private char[] _buffer;
6+
public final int _buffer_size;
7+
private int _write_index = 0;
8+
private int _read_index = 0;
9+
private AtomicInteger _readable_data = new AtomicInteger(0);
10+
11+
public CircularBuffer(int buffer_size) {
12+
if(!IsPowerOfTwo(buffer_size)) {
13+
throw new IllegalArgumentException();
14+
}
15+
this._buffer_size = buffer_size;
16+
_buffer = new char[buffer_size];
17+
}
18+
19+
private boolean IsPowerOfTwo(int i) {
20+
return (i & (i - 1)) == 0;
21+
}
22+
23+
private int getTrueIndex(int i) {
24+
return i % _buffer_size;
25+
}
26+
27+
public Character readOutChar() {
28+
Character result = null;
29+
30+
//if we have data to read
31+
if(_readable_data.get() > 0) {
32+
result = new Character(_buffer[getTrueIndex(_read_index)]);
33+
_readable_data.decrementAndGet();
34+
_read_index++;
35+
}
36+
37+
return result;
38+
}
39+
40+
public boolean writeToCharBuffer(char c) {
41+
boolean result = false;
42+
43+
//if we can write to the buffer
44+
if(_readable_data.get() < _buffer_size) {
45+
//write to buffer
46+
_buffer[getTrueIndex(_write_index)] = c;
47+
_readable_data.incrementAndGet();
48+
_write_index++;
49+
result = true;
50+
}
51+
52+
return result;
53+
}
54+
55+
private static class TestWriteWorker implements Runnable {
56+
String _alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
57+
Random _random = new Random();
58+
CircularBuffer _buffer;
59+
public TestWriteWorker(CircularBuffer cb) {
60+
this._buffer = cb;
61+
}
62+
63+
private char getRandomChar() {
64+
return _alphabet.charAt(_random.nextInt(_alphabet.length()));
65+
}
66+
67+
public void run() {
68+
while(!Thread.interrupted()) {
69+
if(!_buffer.writeToCharBuffer(getRandomChar())){
70+
Thread.yield();
71+
try{
72+
Thread.sleep(10);
73+
} catch (InterruptedException e) {
74+
return;
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
private static class TestReadWorker implements Runnable {
82+
CircularBuffer _buffer;
83+
public TestReadWorker(CircularBuffer cb) {
84+
this._buffer = cb;
85+
}
86+
87+
public void run() {
88+
System.out.println("Printing Buffer:");
89+
while(!Thread.interrupted()) {
90+
Character c = _buffer.readOutChar();
91+
if(c != null) {
92+
System.out.print(c.charValue());
93+
} else {
94+
Thread.yield();
95+
try {
96+
Thread.sleep(10);
97+
} catch (InterruptedException e) {
98+
System.out.println();
99+
return;
100+
}
101+
}
102+
}
103+
}
104+
}
105+
106+
public static void main(String[] args) throws InterruptedException {
107+
int buffer_size = 1024;
108+
//create circular buffer
109+
CircularBuffer cb = new CircularBuffer(buffer_size);
110+
111+
//create threads that read and write the buffer.
112+
Thread write_thread = new Thread(new TestWriteWorker(cb));
113+
Thread read_thread = new Thread(new TestReadWorker(cb));
114+
read_thread.start();
115+
write_thread.start();
116+
117+
//wait some amount of time
118+
Thread.sleep(10000);
119+
120+
//interrupt threads and exit
121+
write_thread.interrupt();
122+
read_thread.interrupt();
123+
}
124+
}

0 commit comments

Comments
 (0)