Emulation of LocalFileSystem with virtual COM.

Dependencies:   USBDevice

Dependents:   KL46Z-lpc81isp lpcterm2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mymap.h Source File

mymap.h

00001 #pragma once
00002 
00003 template<class K,class T>
00004 class mymap {
00005     struct mypair {
00006         K first;
00007         T second;
00008     };
00009 public:
00010     mymap() {
00011         m_size = 0;
00012     }
00013     T& operator[](const K& key) {
00014         int it;
00015         if (count(key) == 0) {
00016             it = insert(key, 0);
00017         } else {
00018             it = find(key);
00019         }
00020         return m_buf[it].second;
00021     }
00022     bool empty() { return m_size == 0 ? true : false; }
00023     int size() { return m_size; }
00024     void clear() { m_size = 0; }
00025     int count(K key) {
00026         for(int i = 0; i < m_size; i++) {
00027             if (m_buf[i].first == key) {
00028                 return 1;
00029             }
00030         }
00031         return 0;
00032     }
00033     K getKey(int index) {
00034         return m_buf[index].first;
00035     }
00036 
00037 private:
00038     int find(K key) {
00039         for(int i = 0; i < m_size; i++) {
00040             if (m_buf[i].first == key) {
00041                 return i;
00042             }
00043         }
00044         return -1;
00045     }
00046     int insert(K key, T value) {
00047         int it = find(key);
00048         if (it != -1) {
00049             m_buf[it].second = value;
00050             return it;
00051         }
00052         mypair* new_buf = new mypair[m_size+1];
00053         if (m_size > 0) {
00054             for(int i = 0; i < m_size; i++) {
00055                 new_buf[i] = m_buf[i];
00056             }
00057             delete[] m_buf;
00058         }
00059         m_buf = new_buf;
00060         it = m_size++;
00061         m_buf[it].first = key;
00062         m_buf[it].second = value;
00063         return it;
00064     }
00065 
00066     int m_size;
00067     mypair *m_buf;
00068 };