- #1
- 2,306
- 808
- TL;DR Summary
- My eldest grandchild is taking IT at the low-level high school. To check on what level he understands programming I wrote a design document for him and asked if he could turn it into a program. He could not. What about you guys?
Design dokument for ”Ringbuffer”
The basic design object is
Ringbuffer: Object {
private
char buf[];
int buf_size, put_on, take_off;
bool empty, full;
int init(int size);
int next(int ix);
public
int put(char ch);
int get(void);
}
Additional design info
The design goal for this object is a "pipe" between two threads. One thread puts characters into the buffer and the other removes characters from the buffer. There is no synchronization between the threads, assume that put can be interrupted by get at any time and vice versa.
Design
init
return (ix+1) if ix<buf_size, else 0
put
If full is FALSE
If empty is FALSE
The basic design object is
Ringbuffer: Object {
private
char buf[];
int buf_size, put_on, take_off;
bool empty, full;
int init(int size);
int next(int ix);
public
int put(char ch);
int get(void);
}
Additional design info
The design goal for this object is a "pipe" between two threads. One thread puts characters into the buffer and the other removes characters from the buffer. There is no synchronization between the threads, assume that put can be interrupted by get at any time and vice versa.
Design
init
- (Allocate space for buf[])
- Set buf_size to size
- Set put_on and take_off to 0
- Set empty to TRUE and full to FALSE
return (ix+1) if ix<buf_size, else 0
put
If full is FALSE
- Copy ch to buf[put_on]
- Update put_on
- Set empty to FALSE
- If put_on equals take_off, set full to TRUE
- Return 0
- Return -1
If empty is FALSE
- Copy buf[take_off] to local temp
- Update take_off
- Set full to FALSE
- If put_on equals take_off set empty to TRUE
- Return temp
- Return -2