final

Dependencies:   mbed FATFileSystem

Fork of KL46Z-USBHostMSD_HelloWorld by Norimasa Okamoto

Committer:
homzovam
Date:
Sat Apr 04 20:16:39 2015 +0000
Revision:
4:77d6450f34d7
prijimac-funkcni final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
homzovam 4:77d6450f34d7 1 #pragma once
homzovam 4:77d6450f34d7 2
homzovam 4:77d6450f34d7 3 template<class K,class T>
homzovam 4:77d6450f34d7 4 class mymap {
homzovam 4:77d6450f34d7 5 struct mypair {
homzovam 4:77d6450f34d7 6 K first;
homzovam 4:77d6450f34d7 7 T second;
homzovam 4:77d6450f34d7 8 };
homzovam 4:77d6450f34d7 9 public:
homzovam 4:77d6450f34d7 10 mymap() {
homzovam 4:77d6450f34d7 11 m_size = 0;
homzovam 4:77d6450f34d7 12 }
homzovam 4:77d6450f34d7 13 T& operator[](const K& key) {
homzovam 4:77d6450f34d7 14 int it;
homzovam 4:77d6450f34d7 15 if (count(key) == 0) {
homzovam 4:77d6450f34d7 16 it = insert(key, 0);
homzovam 4:77d6450f34d7 17 } else {
homzovam 4:77d6450f34d7 18 it = find(key);
homzovam 4:77d6450f34d7 19 }
homzovam 4:77d6450f34d7 20 return m_buf[it].second;
homzovam 4:77d6450f34d7 21 }
homzovam 4:77d6450f34d7 22 bool empty() { return m_size == 0 ? true : false; }
homzovam 4:77d6450f34d7 23 int size() { return m_size; }
homzovam 4:77d6450f34d7 24 void clear() { m_size = 0; }
homzovam 4:77d6450f34d7 25 int count(K key) {
homzovam 4:77d6450f34d7 26 for(int i = 0; i < m_size; i++) {
homzovam 4:77d6450f34d7 27 if (m_buf[i].first == key) {
homzovam 4:77d6450f34d7 28 return 1;
homzovam 4:77d6450f34d7 29 }
homzovam 4:77d6450f34d7 30 }
homzovam 4:77d6450f34d7 31 return 0;
homzovam 4:77d6450f34d7 32 }
homzovam 4:77d6450f34d7 33
homzovam 4:77d6450f34d7 34 private:
homzovam 4:77d6450f34d7 35 int find(K key) {
homzovam 4:77d6450f34d7 36 for(int i = 0; i < m_size; i++) {
homzovam 4:77d6450f34d7 37 if (m_buf[i].first == key) {
homzovam 4:77d6450f34d7 38 return i;
homzovam 4:77d6450f34d7 39 }
homzovam 4:77d6450f34d7 40 }
homzovam 4:77d6450f34d7 41 return -1;
homzovam 4:77d6450f34d7 42 }
homzovam 4:77d6450f34d7 43 int insert(K key, T value) {
homzovam 4:77d6450f34d7 44 int it = find(key);
homzovam 4:77d6450f34d7 45 if (it != -1) {
homzovam 4:77d6450f34d7 46 m_buf[it].second = value;
homzovam 4:77d6450f34d7 47 return it;
homzovam 4:77d6450f34d7 48 }
homzovam 4:77d6450f34d7 49 mypair* new_buf = new mypair[m_size+1];
homzovam 4:77d6450f34d7 50 if (m_size > 0) {
homzovam 4:77d6450f34d7 51 for(int i = 0; i < m_size; i++) {
homzovam 4:77d6450f34d7 52 new_buf[i] = m_buf[i];
homzovam 4:77d6450f34d7 53 }
homzovam 4:77d6450f34d7 54 delete[] m_buf;
homzovam 4:77d6450f34d7 55 }
homzovam 4:77d6450f34d7 56 m_buf = new_buf;
homzovam 4:77d6450f34d7 57 it = m_size++;
homzovam 4:77d6450f34d7 58 m_buf[it].first = key;
homzovam 4:77d6450f34d7 59 m_buf[it].second = value;
homzovam 4:77d6450f34d7 60 return it;
homzovam 4:77d6450f34d7 61 }
homzovam 4:77d6450f34d7 62
homzovam 4:77d6450f34d7 63 int m_size;
homzovam 4:77d6450f34d7 64 mypair *m_buf;
homzovam 4:77d6450f34d7 65 };