Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simplestl std::unordered_map #5859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/simplestl.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,101 @@ inline string operator+(const string& str1, const string& str2)
return str;
}

template<typename Key, typename T>
class unordered_map
{
public:
typedef pair<Key, T> value_type;

unordered_map()
: count(0)
{
}

~unordered_map()
{
clear();
}

void insert(const value_type& value)
{
for (size_t i = 0; i < data.size(); ++i)
{
if (data[i].first == value.first)
{
data[i].second = value.second;
return;
}
}
data.push_back(value);
count++;
}

T& operator[](const Key& key)
{
for (size_t i = 0; i < data.size(); ++i)
{
if (data[i].first == key)
{
return data[i].second;
}
}
data.push_back(make_pair(key, T()));
count++;
return data[count - 1].second;
}

T& at(const Key& key)
{
for (size_t i = 0; i < data.size(); ++i)
{
if (data[i].first == key)
{
return data[i].second;
}
}
return data[0].second; //throw std::out_of_range("Key not found");
}

bool empty() const
{
return count == 0;
}

size_t size() const
{
return count;
}

void emplace(const Key& key, const T& value)
{
insert(make_pair(key, value));
}

void erase(const Key& key)
{
for (size_t i = 0; i < data.size(); ++i)
{
if (data[i].first == key)
{
data.erase(data.begin() + i);
count--;
return;
}
}
}

void clear()
{
data.clear();
count = 0;
}

private:
vector<value_type> data;
size_t count;
};

} // namespace std

#endif // NCNN_SIMPLESTL_H
Loading