(still working on the layer-stack)

partial-rewrite
Ben Blazak 2013-04-09 19:49:38 -07:00
parent 9ac5701e7e
commit 30ad5663d2
2 changed files with 45 additions and 30 deletions

View File

@ -11,6 +11,10 @@
*
* This file is meant to be included and used by the keyboard layout
* implemenmtation.
*
* TODO: add
* - `layer_stack__find_id()`
* - `uint8_t offset` as an argument to ...push()
*/
@ -59,7 +63,7 @@ uint8_t layer_stack__size (void);
* - `layer_number`: the layer-number of the layer to push
*
* Returns:
* - success: the offset of the element that was pushed (or pudated)
* - success: the offset of the element that was pushed (or updated)
*
* Notes:
* - If the given layer-id is not present in the stack, a new element is
@ -73,10 +77,10 @@ uint8_t layer_stack__size (void);
* Pop the given element (by ID) out from the layer stack
*
* Arguments:
* - `layer_id`: the ID of the layer to push
* - `layer_id`: the ID of the layer to pop (remove)
*
* Returns:
* - success: the offset of the element that was pushed (or pudated)
* - success: the offset of the element that was popped (removed)
*/
// === layer_stack__size ===

View File

@ -7,21 +7,21 @@
/** description
* Implements the layer-stack defined in "../layer-stack.h"
*
* TODO: implement, document, everything
* - this needs to be reorganized
* - much of this should probably be split out into a generic flexable_array
* type of some sort (unless the grow and shrink functions are really small)
* TODO:
* - reorganize (if necessary) / check everything
* - properly document (with script-readable comment blocks...)
* - see the '.h' for interface TODOs
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
// ----------------------------------------------------------------------------
#define BLOCK_SIZE 5
#define TYPE_SIZE sizeof(element_t)
#define MARGIN 3
#define BLOCK_SIZE 5 // the number of elements to (de)allocate at once
#define MARGIN 3 // the number of elements keep open (when deallocating)
// ----------------------------------------------------------------------------
@ -30,35 +30,43 @@ typedef struct {
uint8_t number;
} element_t;
typedef struct {
uint8_t allocated;
uint8_t filled;
element_t * data;
} stack_t;
uint8_t _allocated = 0;
uint8_t _filled = 0;
element_t * _stack = NULL;
// ----------------------------------------------------------------------------
stack_t _stack = {
.allocated = BLOCK_SIZE,
.filled = 0,
.data = (element_t *) malloc( TYPE_SIZE * BLOCK_SIZE ),
};
// TODO: this should not be the final function
static uint8_t _resize_stack(stack_t stack) {
uint8_t margin = stack.allocated - stack.filled;
// TODO: is this the cleanest implemenetation?
// TODO: should this be split out into a separate "lib/data-types/flex-array"
// or something?
static uint8_t _resize_stack(void) {
uint8_t margin = _allocated - _filled;
if (margin == 0 || margin > MARGIN + BLOCK_SIZE) {
uint8_t new_size = stack.allocated + TYPE_SIZE * (margin == 0)
? BLOCK_SIZE
: -BLOCK_SIZE;
element_t * temp = (element_t *) realloc(stack.data, new_size);
uint8_t change = (margin == 0) ? BLOCK_SIZE : -BLOCK_SIZE;
uint8_t new_size = sizeof(element_t) * (_allocated + change);
element_t * temp = (element_t *) realloc(_data, new_size);
if (temp) {
stack.data = temp;
stack.allocated += (margin == 0) ? BLOCK_SIZE : -BLOCK_SIZE;
_data = temp;
_allocated += change;
return 0; // success
} else {
return 1; // error
}
}
}
static uint8_t _shift_elements(uint8_t offset, bool up) {
// TODO: maybe we want this function to be the one that calls
// _resize_stack()?
// TODO: arguments:
// - offset: the element we want open, or the element we want to cover by
// shifting down
// - up: whether to shift the elements up or down
// TODO: should we be able to shift by more than one element at a time?
// probably not, but should consider it
}
// ----------------------------------------------------------------------------
@ -67,11 +75,14 @@ uint8_t layer_stack__peek (uint8_t offset) {
}
uint8_t layer_stack__push (uint8_t layer_id, uint8_t layer_number) {
// TODO: resize first, push after
}
uint8_t layer_stack__pop_id (uint8_t layer_id) {
// TODO: remove first, resize after
}
uint8_t layer_stack__size (void) {
return _filled;
}