wu

Dependencies:   mbed-rtos mbed

Fork of Bov3 by kao yi

Committer:
backman
Date:
Mon Jun 30 07:01:58 2014 +0000
Revision:
18:eb675df59c7f
task_de

Who changed what in which revision?

UserRevisionLine numberNew contents of line
backman 18:eb675df59c7f 1 /*
backman 18:eb675df59c7f 2 * mbed library for Stack
backman 18:eb675df59c7f 3 * Copyright (c) 2011 Hiroshi Suga
backman 18:eb675df59c7f 4 * Released under the MIT License: http://mbed.org/license/mit
backman 18:eb675df59c7f 5 */
backman 18:eb675df59c7f 6
backman 18:eb675df59c7f 7 /** @file Stack.cpp
backman 18:eb675df59c7f 8 * @brief Stack
backman 18:eb675df59c7f 9 */
backman 18:eb675df59c7f 10
backman 18:eb675df59c7f 11 #include "Stack.h"
backman 18:eb675df59c7f 12
backman 18:eb675df59c7f 13 template <class T>
backman 18:eb675df59c7f 14 Stack<T>::Stack (int p_size) {
backman 18:eb675df59c7f 15 size = p_size + 1;
backman 18:eb675df59c7f 16 buf = new T[size];
backman 18:eb675df59c7f 17 // buf = (T*)malloc(sizeof(T) * size);
backman 18:eb675df59c7f 18 addr = 0;
backman 18:eb675df59c7f 19 }
backman 18:eb675df59c7f 20
backman 18:eb675df59c7f 21 template <class T>
backman 18:eb675df59c7f 22 Stack<T>::~Stack () {
backman 18:eb675df59c7f 23 delete [] buf;
backman 18:eb675df59c7f 24 // free(buf);
backman 18:eb675df59c7f 25 }
backman 18:eb675df59c7f 26
backman 18:eb675df59c7f 27 template <class T>
backman 18:eb675df59c7f 28 int Stack<T>::push (T dat) {
backman 18:eb675df59c7f 29
backman 18:eb675df59c7f 30 if (addr >= size) {
backman 18:eb675df59c7f 31 return -1;
backman 18:eb675df59c7f 32 }
backman 18:eb675df59c7f 33 buf[addr] = dat;
backman 18:eb675df59c7f 34 addr ++;
backman 18:eb675df59c7f 35 return dat;
backman 18:eb675df59c7f 36 }
backman 18:eb675df59c7f 37
backman 18:eb675df59c7f 38 template <class T>
backman 18:eb675df59c7f 39 int Stack<T>::pop (T *dat) {
backman 18:eb675df59c7f 40
backman 18:eb675df59c7f 41 if (addr == 0) {
backman 18:eb675df59c7f 42 return -1;
backman 18:eb675df59c7f 43 }
backman 18:eb675df59c7f 44 addr --;
backman 18:eb675df59c7f 45 *dat = buf[addr];
backman 18:eb675df59c7f 46 return 0;
backman 18:eb675df59c7f 47 }
backman 18:eb675df59c7f 48
backman 18:eb675df59c7f 49 template <class T>
backman 18:eb675df59c7f 50 int Stack<T>::read (T *dat) {
backman 18:eb675df59c7f 51
backman 18:eb675df59c7f 52 if (addr == 0) {
backman 18:eb675df59c7f 53 return -1;
backman 18:eb675df59c7f 54 }
backman 18:eb675df59c7f 55 *dat = buf[addr];
backman 18:eb675df59c7f 56 return 0;
backman 18:eb675df59c7f 57 }
backman 18:eb675df59c7f 58
backman 18:eb675df59c7f 59 template <class T>
backman 18:eb675df59c7f 60 int Stack<T>::available () {
backman 18:eb675df59c7f 61 return size - addr;
backman 18:eb675df59c7f 62 }
backman 18:eb675df59c7f 63
backman 18:eb675df59c7f 64 template <class T>
backman 18:eb675df59c7f 65 int Stack<T>::use () {
backman 18:eb675df59c7f 66 return addr;
backman 18:eb675df59c7f 67 }
backman 18:eb675df59c7f 68
backman 18:eb675df59c7f 69 template <class T>
backman 18:eb675df59c7f 70 void Stack<T>::clear () {
backman 18:eb675df59c7f 71 addr = 0;
backman 18:eb675df59c7f 72 }
backman 18:eb675df59c7f 73
backman 18:eb675df59c7f 74 template class Stack<int>;
backman 18:eb675df59c7f 75 template class Stack<float>;