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

pull/155/head
KUDr 18 years ago
parent 8a588f9cf1
commit 6784e08945

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

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

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

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

Loading…
Cancel
Save