|
1 | 1 | package com.thealgorithms.datastructures.buffers; |
2 | 2 |
|
3 | | -import java.util.Random; |
4 | 3 | import java.util.concurrent.atomic.AtomicInteger; |
5 | 4 |
|
6 | | -public class CircularBuffer { |
| 5 | +public class CircularBuffer<Item> { |
| 6 | + private final Item[] buffer; |
| 7 | + private final CircularPointer putPointer; |
| 8 | + private final CircularPointer getPointer; |
| 9 | + private final AtomicInteger size = new AtomicInteger(0); |
7 | 10 |
|
8 | | - private char[] _buffer; |
9 | | - public final int _buffer_size; |
10 | | - private int _write_index = 0; |
11 | | - private int _read_index = 0; |
12 | | - private AtomicInteger _readable_data = new AtomicInteger(0); |
13 | | - |
14 | | - public CircularBuffer(int buffer_size) { |
15 | | - if (!IsPowerOfTwo(buffer_size)) { |
16 | | - throw new IllegalArgumentException(); |
17 | | - } |
18 | | - this._buffer_size = buffer_size; |
19 | | - _buffer = new char[buffer_size]; |
| 11 | + public CircularBuffer(int size) { |
| 12 | + //noinspection unchecked |
| 13 | + this.buffer = (Item[]) new Object[size]; |
| 14 | + this.putPointer = new CircularPointer(0, size); |
| 15 | + this.getPointer = new CircularPointer(0, size); |
20 | 16 | } |
21 | 17 |
|
22 | | - private boolean IsPowerOfTwo(int i) { |
23 | | - return (i & (i - 1)) == 0; |
| 18 | + public boolean isEmpty() { |
| 19 | + return size.get() == 0; |
24 | 20 | } |
25 | 21 |
|
26 | | - private int getTrueIndex(int i) { |
27 | | - return i % _buffer_size; |
| 22 | + public boolean isFull() { |
| 23 | + return size.get() == buffer.length; |
28 | 24 | } |
29 | 25 |
|
30 | | - public Character readOutChar() { |
31 | | - Character result = null; |
32 | | - |
33 | | - // if we have data to read |
34 | | - if (_readable_data.get() > 0) { |
35 | | - result = Character.valueOf(_buffer[getTrueIndex(_read_index)]); |
36 | | - _readable_data.decrementAndGet(); |
37 | | - _read_index++; |
38 | | - } |
| 26 | + public Item get() { |
| 27 | + if (isEmpty()) |
| 28 | + return null; |
39 | 29 |
|
40 | | - return result; |
| 30 | + Item item = buffer[getPointer.getAndIncrement()]; |
| 31 | + size.decrementAndGet(); |
| 32 | + return item; |
41 | 33 | } |
42 | 34 |
|
43 | | - public boolean writeToCharBuffer(char c) { |
44 | | - boolean result = false; |
45 | | - |
46 | | - // if we can write to the buffer |
47 | | - if (_readable_data.get() < _buffer_size) { |
48 | | - // write to buffer |
49 | | - _buffer[getTrueIndex(_write_index)] = c; |
50 | | - _readable_data.incrementAndGet(); |
51 | | - _write_index++; |
52 | | - result = true; |
53 | | - } |
| 35 | + public boolean put(Item item) { |
| 36 | + if (isFull()) |
| 37 | + return false; |
54 | 38 |
|
55 | | - return result; |
| 39 | + buffer[putPointer.getAndIncrement()] = item; |
| 40 | + size.incrementAndGet(); |
| 41 | + return true; |
56 | 42 | } |
57 | 43 |
|
58 | | - private static class TestWriteWorker implements Runnable { |
| 44 | + private static class CircularPointer { |
| 45 | + private int pointer; |
| 46 | + private final int max; |
59 | 47 |
|
60 | | - String _alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; |
61 | | - Random _random = new Random(); |
62 | | - CircularBuffer _buffer; |
63 | | - |
64 | | - public TestWriteWorker(CircularBuffer cb) { |
65 | | - this._buffer = cb; |
66 | | - } |
67 | | - |
68 | | - private char getRandomChar() { |
69 | | - return _alphabet.charAt(_random.nextInt(_alphabet.length())); |
| 48 | + public CircularPointer(int pointer, int max) { |
| 49 | + this.pointer = pointer; |
| 50 | + this.max = max; |
70 | 51 | } |
71 | 52 |
|
72 | | - public void run() { |
73 | | - while (!Thread.interrupted()) { |
74 | | - if (!_buffer.writeToCharBuffer(getRandomChar())) { |
75 | | - Thread.yield(); |
76 | | - try { |
77 | | - Thread.sleep(10); |
78 | | - } catch (InterruptedException e) { |
79 | | - return; |
80 | | - } |
81 | | - } |
82 | | - } |
| 53 | + public int getAndIncrement() { |
| 54 | + if (pointer == max) |
| 55 | + pointer = 0; |
| 56 | + int tmp = pointer; |
| 57 | + pointer++; |
| 58 | + return tmp; |
83 | 59 | } |
84 | 60 | } |
85 | | - |
86 | | - private static class TestReadWorker implements Runnable { |
87 | | - |
88 | | - CircularBuffer _buffer; |
89 | | - |
90 | | - public TestReadWorker(CircularBuffer cb) { |
91 | | - this._buffer = cb; |
92 | | - } |
93 | | - |
94 | | - @Override |
95 | | - public void run() { |
96 | | - System.out.println("Printing Buffer:"); |
97 | | - while (!Thread.interrupted()) { |
98 | | - Character c = _buffer.readOutChar(); |
99 | | - if (c != null) { |
100 | | - System.out.print(c.charValue()); |
101 | | - } else { |
102 | | - Thread.yield(); |
103 | | - try { |
104 | | - Thread.sleep(10); |
105 | | - } catch (InterruptedException e) { |
106 | | - System.out.println(); |
107 | | - return; |
108 | | - } |
109 | | - } |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - public static void main(String[] args) throws InterruptedException { |
115 | | - int buffer_size = 1024; |
116 | | - // create circular buffer |
117 | | - CircularBuffer cb = new CircularBuffer(buffer_size); |
118 | | - |
119 | | - // create threads that read and write the buffer. |
120 | | - Thread write_thread = new Thread(new TestWriteWorker(cb)); |
121 | | - Thread read_thread = new Thread(new TestReadWorker(cb)); |
122 | | - read_thread.start(); |
123 | | - write_thread.start(); |
124 | | - |
125 | | - // wait some amount of time |
126 | | - Thread.sleep(10000); |
127 | | - |
128 | | - // interrupt threads and exit |
129 | | - write_thread.interrupt(); |
130 | | - read_thread.interrupt(); |
131 | | - } |
132 | 61 | } |
0 commit comments