Reformatted map classes.

BUG=
TEST=Trybots.

Review URL: https://webrtc-codereview.appspot.com/1006004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3308 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org 2012-12-18 17:18:35 +00:00
parent 61f39a3174
commit 6e0ce73741
5 changed files with 464 additions and 526 deletions

View File

@ -13,11 +13,10 @@
#include <map>
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class MapItem
{
class MapItem {
friend class MapWrapper;
public:
@ -33,8 +32,7 @@ private:
void* item_pointer_;
};
class MapWrapper
{
class MapWrapper {
public:
MapWrapper();
~MapWrapper();
@ -70,6 +68,7 @@ public:
private:
std::map<int, MapItem*> map_;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_

View File

@ -8,47 +8,41 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "map_wrapper.h"
#include "webrtc/system_wrappers/interface/map_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
MapItem::MapItem(int id, void* item) : item_id_(id), item_pointer_(item)
{
MapItem::MapItem(int id, void* item)
: item_id_(id),
item_pointer_(item) {
}
MapItem::~MapItem()
{
MapItem::~MapItem() {
}
void* MapItem::GetItem()
{
void* MapItem::GetItem() {
return item_pointer_;
}
int MapItem::GetId()
{
int MapItem::GetId() {
return item_id_;
}
unsigned int MapItem::GetUnsignedId()
{
unsigned int MapItem::GetUnsignedId() {
return static_cast<unsigned int>(item_id_);
}
void MapItem::SetItem(void* ptr)
{
void MapItem::SetItem(void* ptr) {
item_pointer_ = ptr;
}
MapWrapper::MapWrapper() : map_()
{
MapWrapper::MapWrapper() : map_() {
}
MapWrapper::~MapWrapper()
{
if (!map_.empty())
{
MapWrapper::~MapWrapper() {
if (!map_.empty()) {
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in MapWrapper");
// Remove all map items. Please note that std::map::clear() can't be
@ -60,91 +54,73 @@ MapWrapper::~MapWrapper()
}
}
int MapWrapper::Size() const
{
int MapWrapper::Size() const {
return (int)map_.size();
}
int MapWrapper::Insert(int id, void* ptr)
{
int MapWrapper::Insert(int id, void* ptr) {
map_[id] = new MapItem(id, ptr);
return 0;
}
MapItem* MapWrapper::First() const
{
MapItem* MapWrapper::First() const {
std::map<int, MapItem*>::const_iterator it = map_.begin();
if (it != map_.end())
{
if (it != map_.end()) {
return it->second;
}
return 0;
}
MapItem* MapWrapper::Last() const
{
MapItem* MapWrapper::Last() const {
std::map<int, MapItem*>::const_reverse_iterator it = map_.rbegin();
if (it != map_.rend())
{
if (it != map_.rend()) {
return it->second;
}
return 0;
}
MapItem* MapWrapper::Next(MapItem* item) const
{
if (item == 0)
{
MapItem* MapWrapper::Next(MapItem* item) const {
if (item == 0) {
return 0;
}
std::map<int, MapItem*>::const_iterator it = map_.find(item->item_id_);
if (it != map_.end())
{
if (it != map_.end()) {
it++;
if (it != map_.end())
{
if (it != map_.end()) {
return it->second;
}
}
return 0;
}
MapItem* MapWrapper::Previous(MapItem* item) const
{
if (item == 0)
{
MapItem* MapWrapper::Previous(MapItem* item) const {
if (item == 0) {
return 0;
}
std::map<int, MapItem*>::const_iterator it = map_.find(item->item_id_);
if ((it != map_.end()) &&
(it != map_.begin()))
{
(it != map_.begin())) {
--it;
return it->second;
}
return 0;
}
MapItem* MapWrapper::Find(int id) const
{
MapItem* MapWrapper::Find(int id) const {
std::map<int, MapItem*>::const_iterator it = map_.find(id);
if (it != map_.end())
{
if (it != map_.end()) {
return it->second;
}
return 0;
}
int MapWrapper::Erase(MapItem* item)
{
if (item == 0)
{
int MapWrapper::Erase(MapItem* item) {
if (item == 0) {
return -1;
}
std::map<int, MapItem*>::iterator it = map_.find(item->item_id_);
if (it != map_.end())
{
if (it != map_.end()) {
delete it->second;
map_.erase(it);
return 0;
@ -152,15 +128,14 @@ int MapWrapper::Erase(MapItem* item)
return -1;
}
int MapWrapper::Erase(const int id)
{
int MapWrapper::Erase(const int id) {
std::map<int, MapItem*>::iterator it = map_.find(id);
if (it != map_.end())
{
if (it != map_.end()) {
delete it->second;
map_.erase(it);
return 0;
}
return -1;
}
} // namespace webrtc

View File

@ -8,41 +8,36 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "map_no_stl.h"
#include "webrtc/system_wrappers/source/map_no_stl.h"
#include "critical_section_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
MapNoStlItem::MapNoStlItem(int id, void* item)
: next_(0),
prev_(0),
item_id_(id),
item_ptr_(item)
{
item_ptr_(item) {
}
MapNoStlItem::~MapNoStlItem()
{
MapNoStlItem::~MapNoStlItem() {
}
void* MapNoStlItem::GetItem()
{
void* MapNoStlItem::GetItem() {
return item_ptr_;
}
int MapNoStlItem::GetId()
{
int MapNoStlItem::GetId() {
return item_id_;
}
unsigned int MapNoStlItem::GetUnsignedId()
{
unsigned int MapNoStlItem::GetUnsignedId() {
return static_cast<unsigned int>(item_id_);
}
void MapNoStlItem::SetItem(void* ptr)
{
void MapNoStlItem::SetItem(void* ptr) {
item_ptr_ = ptr;
}
@ -50,14 +45,11 @@ MapNoStl::MapNoStl()
: critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
first_(0),
last_(0),
size_(0)
{
size_(0) {
}
MapNoStl::~MapNoStl()
{
if (First())
{
MapNoStl::~MapNoStl() {
if (First()) {
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in MapNoStl");
while (Erase(First()) == 0)
@ -66,40 +58,32 @@ MapNoStl::~MapNoStl()
delete critical_section_;
}
int MapNoStl::Size() const
{
int MapNoStl::Size() const {
return size_;
}
int MapNoStl::Insert(int id, void* ptr)
{
int MapNoStl::Insert(int id, void* ptr) {
MapNoStlItem* new_item = new MapNoStlItem(id, ptr);
CriticalSectionScoped lock(critical_section_);
MapNoStlItem* item = first_;
size_++;
if (!item)
{
if (!item) {
first_ = new_item;
last_ = new_item;
return 0;
}
while(item->next_)
{
while (item->next_) {
// Three scenarios
// 1. Item should be inserted first.
// 2. Item should be inserted between two items
// 3. Item should be inserted last
if (item->GetId() > id)
{
if (item->GetId() > id) {
new_item->next_ = item;
item->prev_ = new_item;
if (item == first_)
{
if (item == first_) {
first_ = new_item;
}
else
{
} else {
new_item->prev_ = item->prev_;
new_item->prev_->next_ = new_item;
}
@ -114,69 +98,55 @@ int MapNoStl::Insert(int id, void* ptr)
return 0;
}
MapNoStlItem* MapNoStl::First() const
{
MapNoStlItem* MapNoStl::First() const {
return first_;
}
MapNoStlItem* MapNoStl::Last() const
{
MapNoStlItem* MapNoStl::Last() const {
return last_;
}
MapNoStlItem* MapNoStl::Next(MapNoStlItem* item) const
{
if (!item)
{
MapNoStlItem* MapNoStl::Next(MapNoStlItem* item) const {
if (!item) {
return 0;
}
return item->next_;
}
MapNoStlItem* MapNoStl::Previous(MapNoStlItem* item) const
{
if (!item)
{
MapNoStlItem* MapNoStl::Previous(MapNoStlItem* item) const {
if (!item) {
return 0;
}
return item->prev_;
}
MapNoStlItem* MapNoStl::Find(int id) const
{
MapNoStlItem* MapNoStl::Find(int id) const {
CriticalSectionScoped lock(critical_section_);
MapNoStlItem* item = Locate(id);
return item;
}
int MapNoStl::Erase(MapNoStlItem* item)
{
if(!item)
{
int MapNoStl::Erase(MapNoStlItem* item) {
if (!item) {
return -1;
}
CriticalSectionScoped lock(critical_section_);
return Remove(item);
}
int MapNoStl::Erase(const int id)
{
int MapNoStl::Erase(const int id) {
CriticalSectionScoped lock(critical_section_);
MapNoStlItem* item = Locate(id);
if(!item)
{
if (!item) {
return -1;
}
return Remove(item);
}
MapNoStlItem* MapNoStl::Locate(int id) const
{
MapNoStlItem* MapNoStl::Locate(int id) const {
MapNoStlItem* item = first_;
while(item)
{
if (item->GetId() == id)
{
while (item) {
if (item->GetId() == id) {
return item;
}
item = item->next_;
@ -184,34 +154,27 @@ MapNoStlItem* MapNoStl::Locate(int id) const
return 0;
}
int MapNoStl::Remove(MapNoStlItem* item)
{
if (!item)
{
int MapNoStl::Remove(MapNoStlItem* item) {
if (!item) {
return -1;
}
size_--;
MapNoStlItem* previous_item = item->prev_;
MapNoStlItem* next_item = item->next_;
if (!previous_item)
{
if (!previous_item) {
next_item->prev_ = 0;
first_ = next_item;
}
else
{
} else {
previous_item->next_ = next_item;
}
if (!next_item)
{
if (!next_item) {
previous_item->next_ = 0;
last_ = previous_item;
}
else
{
} else {
next_item->prev_ = previous_item;
}
delete item;
return 0;
}
} // namespace webrtc

View File

@ -11,13 +11,13 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_MAP_NO_STL_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_MAP_NO_STL_H_
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class CriticalSectionWrapper;
class MapNoStlItem
{
class MapNoStlItem {
friend class Map;
public:
@ -38,8 +38,7 @@ private:
DISALLOW_COPY_AND_ASSIGN(MapNoStlItem);
};
class MapNoStl
{
class MapNoStl {
public:
MapNoStl();
virtual ~MapNoStl();
@ -65,6 +64,7 @@ private:
int size_;
DISALLOW_COPY_AND_ASSIGN(MapNoStl);
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_MAP_NO_STL_H_

View File

@ -8,9 +8,9 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "gtest/gtest.h"
#include "webrtc/system_wrappers/interface/map_wrapper.h"
#include "map_wrapper.h"
#include "gtest/gtest.h"
using ::webrtc::MapWrapper;
using ::webrtc::MapItem;
@ -170,7 +170,8 @@ protected:
TEST_F(MapWrapperTest, RemoveTest) {
// Erase using int id
{ // Create local scope to avoid accidental re-use
{
// Create local scope to avoid accidental re-use
MapItem* item_first = ascending_map_.First();
ASSERT_FALSE(item_first == NULL);
const int first_value_id = item_first->GetId();