(svn r7147) -CodeChange: Don't use references if they can refer to NULL (Tron)

This commit is contained in:
KUDr 2006-11-14 13:42:50 +00:00
parent ecccf79c07
commit ddda3c8b2f
4 changed files with 52 additions and 54 deletions

View File

@ -13,27 +13,27 @@ struct CHashTableSlotT
CHashTableSlotT() : m_pFirst(NULL) {}
/** hash table slot helper - linear search for item with given key through the given blob - const version */
FORCEINLINE const Titem_& Find(const Key& key) const
FORCEINLINE const Titem_* Find(const Key& key) const
{
for (const Titem_* pItem = m_pFirst; pItem != NULL; pItem = pItem->GetHashNext()) {
if (pItem->GetKey() == key) {
// we have found the item, return it
return *pItem;
return pItem;
}
}
return *(Titem_*)NULL;
return NULL;
}
/** hash table slot helper - linear search for item with given key through the given blob - non-const version */
FORCEINLINE Titem_& Find(const Key& key)
FORCEINLINE Titem_* Find(const Key& key)
{
for (Titem_* pItem = m_pFirst; pItem != NULL; pItem = pItem->GetHashNext()) {
if (pItem->GetKey() == key) {
// we have found the item, return it
return *pItem;
return pItem;
}
}
return *(Titem_*)NULL;
return NULL;
}
/** hash table slot helper - add new item to the slot */
@ -67,18 +67,18 @@ struct CHashTableSlotT
}
/** hash table slot helper - remove and return item from a slot */
FORCEINLINE Titem_& Detach(const Key& key)
FORCEINLINE Titem_* Detach(const Key& key)
{
// do we have any items?
if (m_pFirst == NULL) {
return *(Titem_*)NULL;
return NULL;
}
// is it our first item?
if (m_pFirst->GetKey() == key) {
Titem_& ret_item = *m_pFirst;
m_pFirst = m_pFirst->GetHashNext();
ret_item.SetHashNext(NULL);
return ret_item;
return &ret_item;
}
// find it in the following items
Titem_* pPrev = m_pFirst;
@ -87,10 +87,10 @@ struct CHashTableSlotT
// we have found the item, unlink and return it
pPrev->SetHashNext(pItem->GetHashNext());
pItem->SetHashNext(NULL);
return *pItem;
return pItem;
}
}
return *(Titem_*)NULL;
return NULL;
}
};
@ -163,30 +163,30 @@ public:
FORCEINLINE int Count() const {return m_num_items;}
/** const item search */
const Titem_& Find(const Tkey& key) const
const Titem_* Find(const Tkey& key) const
{
int hash = CalcHash(key);
const Slot& slot = m_slots[hash];
const Titem_& item = slot.Find(key);
const Titem_* item = slot.Find(key);
return item;
}
/** non-const item search */
Titem_& Find(const Tkey& key)
Titem_* Find(const Tkey& key)
{
int hash = CalcHash(key);
Slot& slot = m_slots[hash];
Titem_& item = slot.Find(key);
Titem_* item = slot.Find(key);
return item;
}
/** non-const item search & optional removal (if found) */
Titem_& TryPop(const Tkey& key)
Titem_* TryPop(const Tkey& key)
{
int hash = CalcHash(key);
Slot& slot = m_slots[hash];
Titem_& item = slot.Detach(key);
if (&item != NULL) {
Titem_* item = slot.Detach(key);
if (item != NULL) {
m_num_items--;
}
return item;
@ -195,9 +195,9 @@ public:
/** non-const item search & removal */
Titem_& Pop(const Tkey& key)
{
Titem_& item = TryPop(key);
assert(&item != NULL);
return item;
Titem_* item = TryPop(key);
assert(item != NULL);
return *item;
}
/** non-const item search & optional removal (if found) */
@ -225,7 +225,7 @@ public:
{
int hash = CalcHash(new_item);
Slot& slot = m_slots[hash];
assert(&slot.Find(new_item.GetKey()) == NULL);
assert(slot.Find(new_item.GetKey()) == NULL);
slot.Attach(new_item);
m_num_items++;
}

View File

@ -69,7 +69,7 @@ public:
/** insert given item as open node (into m_open and m_open_queue) */
FORCEINLINE void InsertOpenNode(Titem_& item)
{
assert(&m_closed.Find(item.GetKey()) == NULL);
assert(m_closed.Find(item.GetKey()) == NULL);
m_open.Push(item);
// TODO: check if m_open_queue is not full
assert(!m_open_queue.IsFull());
@ -78,50 +78,48 @@ public:
m_new_node = NULL;
}
/** return the best open node */
FORCEINLINE Titem_& GetBestOpenNode()
FORCEINLINE Titem_* GetBestOpenNode()
{
if (!m_open_queue.IsEmpty()) {
Titem_& item = m_open_queue.GetHead();
return item;
return &item;
}
return *(Titem_*)NULL;
return NULL;
}
/** remove and return the best open node */
FORCEINLINE Titem_& PopBestOpenNode()
FORCEINLINE Titem_* PopBestOpenNode()
{
if (!m_open_queue.IsEmpty()) {
Titem_& item = m_open_queue.PopHead();
m_open.Pop(item);
return item;
return &item;
}
return *(Titem_*)NULL;
return NULL;
}
/** return the open node specified by a key or NULL if not found */
FORCEINLINE Titem_& FindOpenNode(const Key& key)
FORCEINLINE Titem_* FindOpenNode(const Key& key)
{
Titem_& item = m_open.Find(key);
Titem_* item = m_open.Find(key);
return item;
}
/** remove and return the open node specified by a key */
FORCEINLINE Titem_& PopOpenNode(const Key& key)
{
Titem_& item = m_open.Pop(key);
if (&item != NULL) {
int idxPop = m_open_queue.FindLinear(item);
m_open_queue.RemoveByIdx(idxPop);
}
int idxPop = m_open_queue.FindLinear(item);
m_open_queue.RemoveByIdx(idxPop);
return item;
}
/** close node */
FORCEINLINE void InsertClosedNode(Titem_& item)
{
assert(&m_open.Find(item.GetKey()) == NULL);
assert(m_open.Find(item.GetKey()) == NULL);
m_closed.Push(item);
}
/** return the closed node specified by a key or NULL if not found */
FORCEINLINE Titem_& FindClosedNode(const Key& key)
FORCEINLINE Titem_* FindClosedNode(const Key& key)
{
Titem_& item = m_closed.Find(key);
Titem_* item = m_closed.Find(key);
return item;
}

View File

@ -116,18 +116,18 @@ public:
while (true) {
m_num_steps++;
Node& n = m_nodes.GetBestOpenNode();
if (&n == NULL)
Node* n = m_nodes.GetBestOpenNode();
if (n == NULL)
break;
// if the best open node was worse than the best path found, we can finish
if (m_pBestDestNode != NULL && m_pBestDestNode->GetCost() < n.GetCostEstimate())
if (m_pBestDestNode != NULL && m_pBestDestNode->GetCost() < n->GetCostEstimate())
break;
Yapf().PfFollowNode(n);
Yapf().PfFollowNode(*n);
if (m_max_search_nodes == 0 || m_nodes.ClosedCount() < m_max_search_nodes) {
m_nodes.PopOpenNode(n.GetKey());
m_nodes.InsertClosedNode(n);
m_nodes.PopOpenNode(n->GetKey());
m_nodes.InsertClosedNode(*n);
} else {
m_pBestDestNode = m_pBestIntermediateNode;
break;
@ -172,7 +172,7 @@ public:
{
Yapf().PfNodeCacheFetch(n);
// insert the new node only if it is not there
if (&m_nodes.FindOpenNode(n.m_key) == NULL) {
if (m_nodes.FindOpenNode(n.m_key) == NULL) {
m_nodes.InsertOpenNode(n);
} else {
// if we are here, it means that node is already there - how it is possible?
@ -231,27 +231,27 @@ public:
}
// check new node against open list
Node& openNode = m_nodes.FindOpenNode(n.GetKey());
if (&openNode != NULL) {
Node* openNode = m_nodes.FindOpenNode(n.GetKey());
if (openNode != NULL) {
// another node exists with the same key in the open list
// is it better than new one?
if (n.GetCostEstimate() < openNode.GetCostEstimate()) {
if (n.GetCostEstimate() < openNode->GetCostEstimate()) {
// update the old node by value from new one
m_nodes.PopOpenNode(n.GetKey());
openNode = n;
*openNode = n;
// add the updated old node back to open list
m_nodes.InsertOpenNode(openNode);
m_nodes.InsertOpenNode(*openNode);
}
return;
}
// check new node against closed list
Node& closedNode = m_nodes.FindClosedNode(n.GetKey());
if (&closedNode != NULL) {
Node* closedNode = m_nodes.FindClosedNode(n.GetKey());
if (closedNode != NULL) {
// another node exists with the same key in the closed list
// is it better than new one?
int node_est = n.GetCostEstimate();
int closed_est = closedNode.GetCostEstimate();
int closed_est = closedNode->GetCostEstimate();
if (node_est < closed_est) {
// If this assert occurs, you have probably problem in
// your Tderived::PfCalcCost() or Tderived::PfCalcEstimate().

View File

@ -106,7 +106,7 @@ struct CSegmentCostCacheT
FORCEINLINE Tsegment& Get(Key& key, bool *found)
{
Tsegment* item = &m_map.Find(key);
Tsegment* item = m_map.Find(key);
if (item == NULL) {
*found = false;
item = new (&m_heap.AddNC()) Tsegment(key);