几何尺寸与公差论坛

 找回密码
 注册
查看: 2468|回复: 7

node sample

[复制链接]
发表于 2007-7-22 21:17:03 | 显示全部楼层 |阅读模式
1.CSClassTypeNode.h
// CSClassTypeNode.h: interface for the CSClassTypeNode class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CSTYPEMAPHELPER_H__CE2B9209_8683_42A8_838D_E07B32A7921B__INCLUDED_)
#define AFX_CSTYPEMAPHELPER_H__CE2B9209_8683_42A8_838D_E07B32A7921B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "CSTypedef.h"
#include "CSTransient.h"
#include "CSClassTypeHandle.h"

enum EInternalType {
CS_Void,
  CS_Char,
  CS_ExtChar,
  CS_LongInt,
  CS_Bool,
  CS_Float,
  CS_LongDouble,
  CS_String,
  CS_EString,
  CS_EntryAddress,
  CS_DataAddress,
  CS_EngineHandle,
  CS_Long64,
  CS_Array
};
enum ELifeWay {
CS_IsNothing,
  CS_IsAddress,
  CS_IsTransient,
  CS_IsPersistent,
  CS_IsNotLoaded
};

class CSClassTypeNode  
{
public:
// Methods PUBLIC
//
CSClassTypeNode();
CSClassTypeNode(const CS_CString aName,const CS_Integer aLen,const ELifeWay aWayOfLife);
CSClassTypeNode(const CSClassTypeHandle& aType);
CS_EXPORT   void Copy(const CSClassTypeNode*& anOther) ;
CS_EXPORT   void AddInternalType(const EInternalType aInternalType) ;
CS_EXPORT   EInternalType InternalType() const;
CS_EXPORT   void AddMethodType(const CS_Boolean aMethodType) ;
CS_EXPORT   CS_Boolean MethodType() const;
CS_EXPORT   CS_CString Name() const;
CS_EXPORT   CS_Integer Length() const;
CS_EXPORT   CSClassTypeHandle Type() const;
CS_EXPORT   CS_Boolean SubType(const CSClassTypeNode*& pNode) const;
CS_EXPORT   void Nullify() ;
CS_EXPORT   CS_Boolean IsImported() const;
CS_EXPORT   CS_Boolean IsPrimitive() const;
CS_EXPORT   CS_Boolean IsUnKnown() const;
CS_EXPORT   CS_Boolean IsEnumeration() const;
CS_EXPORT   CS_Boolean IsClass() const;
CS_EXPORT   CS_Boolean IsPackage() const;
CS_EXPORT   void WhatIs() ;
CS_EXPORT   CS_Boolean IsPersistent() const;
CS_EXPORT   CS_Boolean IsTransient() const;
CS_EXPORT   CS_Boolean IsAddress() const;
CS_EXPORT   CS_Boolean IsNull() const;
CS_EXPORT   CS_Boolean IsNotLoaded() const;
CS_EXPORT   CS_Integer HashCode() const;
CS_EXPORT   CS_Integer Rank() const;
CS_EXPORT   void AddName(const CS_CString aName,const CS_Integer aLen) ;
CS_EXPORT   void AddHashCode(const CS_Integer aHash) ;
CS_EXPORT   void AddRank(const CS_Integer aRank) ;
CS_EXPORT   CSClassTypeNode* Next() const;
CS_EXPORT   void AddNext(const CSClassTypeNode*& pNode) ;
CS_EXPORT   CSClassTypeNode* NextContainer() const;
CS_EXPORT   void AddNextContainer(const CSClassTypeNode*& pNode) ;
CS_EXPORT   CS_Integer CaseCmp(const CSClassTypeNode*& anOther) const;
CS_EXPORT   void Sort() ;
CS_EXPORT   CS_Address InterfacePlugin() const;
CS_EXPORT   void SetInterfacePlugin(const CS_Address pInterface) ;
CS_EXPORT   void Display() const;
CS_EXPORT   void Display(CS_OStream& anOut) const;
CS_EXPORT   void ShallowDump() const;
CS_EXPORT   void ShallowDump(CS_OStream& anOut) const;



private:
CSClassTypeNode* m_pNext;
CS_Address m_pInterface;
CSClassTypeNode* m_pNextContainer;
CS_CString m_strName;
CS_Integer m_nLength;
EClassType m_eKind;
ELifeWay m_eWay;
EInternalType m_eInternalType;
CS_Integer m_nHashCode;
CS_Integer m_nRank;
CSClassTypeHandle m_hType;
CS_Boolean m_bMethodType;
};
typedef CSClassTypeNode* PCSClassTypeNode;
#endif // !defined(AFX_CSTYPEMAPHELPER_H__CE2B9209_8683_42A8_838D_E07B32A7921B__INCLUDED_)
 楼主| 发表于 2007-7-22 21:17:36 | 显示全部楼层

回复: node sample

2.CSClassTypeNode.cpp
// CSClassTypeNode.cpp: implementation of the CSClassTypeNode class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CSClassTypeNode.h"
#include "CSMapType.h"
//////////////////////////////////////////////////////////////////////
// Construction/DestructionCSClassTypeNode*
//////////////////////////////////////////////////////////////////////
CS_EXPORT CSMapType    *CSMapType();

inline CSClassTypeNode::CSClassTypeNode() :
       m_pNext( NULL ), m_pInterface( NULL ), m_pNextContainer( NULL ),
       m_strName( NULL ), m_nLength( 0 ), m_eKind( CS_IsUnKnown ),
       m_eWay( CS_IsNothing ), m_eInternalType( CS_Void ),
       m_nHashCode( 0 ), m_nRank( 0 ), m_hType( NULL ),
       m_bMethodType( CS_False )
{
// cout << "CSClassTypeNode::CSClassTypeNode 1 " ;
// Display() ;
// cout << endl ;
}
inline CSClassTypeNode::CSClassTypeNode(
                        const CS_CString aName ,
                        const CS_Integer aLen ,
                        const ELifeWay aWayOfLife) :
       m_pNext( NULL ), m_pInterface( NULL ), m_pNextContainer( NULL ),
       m_strName( aName ), m_nLength( aLen ),m_eKind( CS_IsPackage ),
       m_eWay( aWayOfLife ), m_eInternalType( CS_Void ),
       m_nHashCode( 0 ), m_nRank( 0 ), m_hType( NULL ),
       m_bMethodType( CS_False )
{
// cout << "CSClassTypeNode::CSClassTypeNode 2 " ;
// Display() ;
// cout << endl ;
}
inline void CSClassTypeNode::Copy(const CSClassTypeNode*& anOther )
{
   m_hType         = anOther->m_hType ;
   m_eKind   = anOther->m_eKind ;
   m_eWay    = anOther->m_eWay ;
   m_eInternalType = anOther->m_eInternalType ;
   m_bMethodType   = anOther->m_bMethodType ;
}
inline CS_CString CSClassTypeNode::Name() const
{
return m_strName ;
}
inline void CSClassTypeNode::AddInternalType( const EInternalType aInternalType )
{
m_eInternalType = aInternalType ;
}
inline EInternalType CSClassTypeNode::InternalType() const
{
return m_eInternalType ;
}
inline void CSClassTypeNode::AddMethodType( const CS_Boolean aMethodType )
{
m_bMethodType = aMethodType ;
}
inline CS_Boolean CSClassTypeNode::MethodType() const
{
return m_bMethodType ;
}
inline CS_Integer CSClassTypeNode:ength() const
{
return m_nLength ;
}

inline CSClassTypeHandle  CSClassTypeNode::Type() const
{
return m_hType ;
}

inline CS_Boolean CSClassTypeNode::SubType(const CSClassTypeNode*& pNode ) const
{
if ( m_eKind == CS_IsUnKnown ||
      pNode->IsUnKnown() )
   return CS_False ;
if ( m_eKind == CS_IsPrimitive ||
      pNode->IsPrimitive() )
   return CS_False ;
if ( pNode == this || pNode->Type() == m_hType )
   return CS_True ;
return m_hType->SubType( pNode->Type() ) ;
}

inline CS_Boolean CSClassTypeNode::IsImported() const
{
  return m_eKind == CS_IsImported;
}

inline CS_Boolean CSClassTypeNode::IsPrimitive() const
{
  return m_eKind == CS_IsPrimitive;
}

inline CS_Boolean CSClassTypeNode::IsUnKnown() const
{
  return m_eKind == CS_IsUnKnown;
}

inline CS_Boolean CSClassTypeNode::IsEnumeration() const
{
  return m_eKind == CS_IsEnumeration;
}

inline CS_Boolean CSClassTypeNode::IsClass() const
{
  return m_eKind == CS_IsClass ;
}

inline CS_Boolean CSClassTypeNode::IsPackage() const
{
  return m_eKind == CS_IsPackage ;
}

inline void CSClassTypeNode::WhatIs()
{
if ( m_eKind == CS_IsPackage )
   m_eWay = CS_IsNothing ;
else if ( m_hType->SubType( THEPForMapOfTypes()->TypPersistent()->Type() ) )
   m_eWay = CS_IsPersistent ;
else if ( m_hType->SubType( THEPForMapOfTypes()->TypTransient()->Type() ) )
   m_eWay = CS_IsTransient ;
else
   m_eWay = CS_IsAddress ;
}

inline CS_Boolean CSClassTypeNode::IsPersistent() const
{
  return m_eWay == CS_IsPersistent ;
}

inline CS_Boolean CSClassTypeNode::IsTransient() const
{
  return m_eWay == CS_IsTransient ;
}

inline CS_Boolean CSClassTypeNode::IsAddress() const
{
  return m_eWay == CS_IsAddress ;
}
inline CS_Boolean CSClassTypeNode::IsNotLoaded() const
{
  return m_eWay == CS_IsNotLoaded ;
}

inline CS_Integer CSClassTypeNode::HashCode() const
{
return m_nHashCode ;
}

inline CS_Integer CSClassTypeNode::Rank() const
{
return m_nRank ;
}
inline CSClassTypeNode* CSClassTypeNode::Next() const
{
return m_pNext ;
}
inline CSClassTypeNode* CSClassTypeNode::NextContainer() const
{
return m_pNextContainer ;
}
inline void CSClassTypeNode::AddName(const CS_CString aName,const CS_Integer aLen )
{
m_strName = new char[aLen + 1];
strcpy( m_strName , aName );
m_nLength = aLen ;
m_eWay = CS_IsNotLoaded ;
}

inline void CSClassTypeNode::AddHashCode(const CS_Integer aHashCode )
{
m_nHashCode = aHashCode ;
}

inline void CSClassTypeNode::AddRank(const CS_Integer aRank )
{
m_nRank = aRank ;
}
inline void CSClassTypeNode::AddNext(const CSClassTypeNode*& pNode )
{
m_pNext = pNode;
}
inline void CSClassTypeNode::AddNextContainer(const CSClassTypeNode*& pNode )
{
m_pNextContainer = pNode;
}
inline CS_Integer CSClassTypeNode::CaseCmp(const CSClassTypeNode*& pNode ) const
{
if ( m_nRank < pNode->m_nRank )
   return -1 ;
else if ( m_nRank > pNode->m_nRank )
   return 1 ;
else
   return 0 ;
}
inline void CSClassTypeNode::SetInterfacePlugin( const CS_Address pInterface )
{
  m_pInterface = pInterface;
}
inline CS_Address CSClassTypeNode::InterfacePlugin() const
{
  return m_pInterface;
}
inline void CSClassTypeNode::Nullify()
{
m_hType.Nullify() ;
}
inline CS_Boolean CSClassTypeNode::IsNull() const
{
return m_hType.IsNull() ;
}
 楼主| 发表于 2007-7-22 21:18:22 | 显示全部楼层

回复: node sample

3.CSBaseMapIterator.h
// CSBaseMapIterator.h: interface for the CSBaseMapIterator class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CSBaseMapIterator_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_)
#define AFX_CSBaseMapIterator_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSBaseMap;
struct CSMapNode
{
public:
CSMapNode(CSMapNode* n) : next(n) {}
CSMapNode* next;
};
class  CSBaseMapIterator  
{
public:

CS_EXPORT   void Reset() ;
CS_EXPORT   CS_Boolean More() const;
CS_EXPORT   void Next() ;

protected:

// Methods PROTECTED
//
CS_EXPORT CSBaseMapIterator();
CS_EXPORT CSBaseMapIterator(const CSBaseMap& M);
CS_EXPORT void Initialize(const CSBaseMap& M) ;


// Fields PROTECTED
//
CS_Address m_pNode;


private:

// Methods PRIVATE
//


// Fields PRIVATE
//
CS_Integer m_nNumBuckets;
CS_Address m_pBuckets;
CS_Integer m_nBucket;

};
#endif // !defined(AFX_CSBASICMAPITERATOR_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_)
 楼主| 发表于 2007-7-22 21:19:02 | 显示全部楼层

回复: node sample

4.CSBaseMapIterator.h
// CSBaseMapIterator.h: interface for the CSBaseMapIterator class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CSBaseMapIterator_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_)
#define AFX_CSBaseMapIterator_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSBaseMap;
struct CSMapNode
{
public:
CSMapNode(CSMapNode* n) : next(n) {}
CSMapNode* next;
};
class  CSBaseMapIterator  
{
public:

CS_EXPORT   void Reset() ;
CS_EXPORT   CS_Boolean More() const;
CS_EXPORT   void Next() ;

protected:

// Methods PROTECTED
//
CS_EXPORT CSBaseMapIterator();
CS_EXPORT CSBaseMapIterator(const CSBaseMap& M);
CS_EXPORT void Initialize(const CSBaseMap& M) ;


// Fields PROTECTED
//
CS_Address m_pNode;


private:

// Methods PRIVATE
//


// Fields PRIVATE
//
CS_Integer m_nNumBuckets;
CS_Address m_pBuckets;
CS_Integer m_nBucket;

};
#endif // !defined(AFX_CSBASICMAPITERATOR_H__214801A6_2163_4995_8245_F320FB23E519__INCLUDED_)
 楼主| 发表于 2007-7-22 21:20:12 | 显示全部楼层

回复: node sample

5.CSMapHasher.h
// CSMapHasher.h: interface for the CSMapHasher class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CSMAPHASHER_H__595FBF07_A8A6_40FC_AFB4_F8A953C29CD5__INCLUDED_)
#define AFX_CSMAPHASHER_H__595FBF07_A8A6_40FC_AFB4_F8A953C29CD5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "CSTypedef.h"
#include "CSClassTypeNode.h"
class CSClassTypeHandle;
class CSMapHasher  
{
public:
CS_EXPORT static  CS_Integer HashCode(const PCSClassTypeNode& K,const CS_Integer Upper) ;
CS_EXPORT static  CS_Integer HashCode(const CS_CString K,const CS_Integer len,const CS_Integer Upper) ;
CS_EXPORT static  CS_Integer HashCode(const CS_CString K,const CS_Integer len,const CS_Integer Upper,CS_Integer& HashCode) ;
CS_EXPORT static  CS_Boolean IsEqual(const PCSClassTypeNode& K1,const CS_CString K2,const CS_Integer len) ;
CS_EXPORT static  CS_Boolean IsEqual(const PCSClassTypeNode& K1,const PCSClassTypeNode& K2) ;
CS_EXPORT static  CS_Boolean IsEqual(const PCSClassTypeNode& K1,const CSClassTypeHandle& K2) ;

};
typedef CSMapHasher Hasher;
#endif // !defined(AFX_CSMAPHASHER_H__595FBF07_A8A6_40FC_AFB4_F8A953C29CD5__INCLUDED_)
 楼主| 发表于 2007-7-22 21:20:44 | 显示全部楼层

回复: node sample

6.CSMapHasher.cpp
// CSMapHasher.cpp: implementation of the CSMapHasher class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CSTypedef.h"
#include "CSMapHasher.h"
#include "CSClassTypeNode.h"
#include "CSClassTypeHandle.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

inline CS_Integer CSMapHasher::HashCode(const PCSClassTypeNode& K,const CS_Integer Upper)
{
//  return (Abs( K->HashCode() ) % Upper ) + 1 ;
return ::HashCode( K->HashCode() , Upper ) ;
}
inline CS_Boolean CSMapHasher::IsEqual(const PCSClassTypeNode& K1,const PCSClassTypeNode& K2)
{
CS_Boolean k_equal ;
k_equal = ( K1 == K2 );
return k_equal ;
}
inline CS_Integer CSMapHasher::HashCode(const CS_CString K,const CS_Integer len ,const CS_Integer Upper )
{
return HASHCODE( K , len , Upper );
}
inline CS_Boolean CSMapHasher::IsEqual(const PCSClassTypeNode& K1,const CS_CString  K2 ,const CS_Integer Len2)
{
return ISEQUAL( K1->Name() , K1->Length() ,K2 , Len2 ) ;
}
inline CS_Integer CSMapHasher::HashCode(const CS_CString K,const CS_Integer len ,const CS_Integer Upper ,CS_Integer& aHashCode )
{
return HASHCODE( K , len , Upper , aHashCode );
}
inline CS_Boolean CSMapHasher::IsEqual(const PCSClassTypeNode& K1,const CSClassTypeHandle&  K2)
{
return ISEQUAL( K1->Name() , K1->Length() ,
  K2->Name() , K2->Size() ) ;
}
 楼主| 发表于 2007-7-22 21:21:45 | 显示全部楼层

回复: node sample

7.STypeMap.cpp
// STypeMap.cpp: implementation of the CSClassTypeMap class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CSTypedef.h"
#include "CSBaseMapIterator.h"
#include "CSMapHasher.h"
#include "CSClassTypeMap.h"

class Node : public CSMapNode
{
public :
    Node(const PCSClassTypeNode& K1, const CS_Integer K2, Node* n1, Node* n2) :
    CSMapNode(n1),key1(K1),key2(K2),next2(n2) {}
   
    void* operator new(size_t aSize)
    {return calloc(1,aSize);}
   
    void  operator delete(void* aNode )
    {
     free(aNode);
    }
   
    PCSClassTypeNode    key1;
    CS_Integer          key2;
    Node*               next2;
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//=======================================================================
//function : CSClassTypeMap
//purpose  :
//=======================================================================
CSClassTypeMap::CSClassTypeMap(const CS_Integer NbBuckets )
{
//:CSBaseMap(NbBuckets,CS_False)
CSBaseMap(NbBuckets,CS_False,1);
}
//=======================================================================
//function : CSClassTypeMap
//purpose  :
//=======================================================================
CSClassTypeMap::CSClassTypeMap(const CS_Integer NbBuckets ,const CS_Integer maxsize ) :
CSBaseMap(NbBuckets,CS_False,maxsize)
{
}
//=======================================================================
//function : CSClassTypeMap
//purpose  :
//=======================================================================
CSClassTypeMap::CSClassTypeMap(const CSClassTypeMap& Other) :
CSBaseMap(Other.GetBucketsNum(),CS_False)
{
if (!Other.IsEmpty())
  Standard_DomainError::Raise("Standard:Copy of IndexedMap");
}
//=======================================================================
//function : Assign
//purpose  :
//=======================================================================
CSClassTypeMap& CSClassTypeMap::Assign(const CSClassTypeMap& Other)
{
// very simple implementation
// not optimal (recompute the hashcode values)

if (this == &Other) return *this;
Clear();
ReSize(Other.GetBucketsNum());
for (CS_Integer i = 1; i <= Other.Extent(); i++)
{
  Add(Other(i));
}
return *this;
}
//=======================================================================
//function : ReSize
//purpose  :
//=======================================================================
void CSClassTypeMap::ReSize(const CS_Integer N)
{
Node** pData1;
Node** pData2;
CS_Integer nBuck ;
if (BeginResize(N,nBuck,
  *(CS_Address*)&pData1,
  *(CS_Address*)&pData2))
{
  Node *p, *q;
  CS_Integer i,k1,k2;
  if (m_pData1)
  {
   Node** olddata1 = (Node**) m_pData1;
   for (i = 0; i <= GetBucketsNum(); i++)
   {
    if (olddata1)
    {
     p = olddata1;
     while (p)
     {
      k1 = Hasher::HashCode(p->key1,nBuck);
      q = (Node*) p->next;
      p->next = pData1[k1];
      pData1[k1] = p;
      if (p->key2 > 0) {
       k2 = ::HashCode(p->key2,nBuck);
       p->next2 = pData2[k2];
       pData2[k2] = p;
      }
      p = q;
     }
    }
   }
  }
  EndResize(N,nBuck,pData1,pData2);
}
}
//=======================================================================
//function : Clear
//purpose  :
//=======================================================================
void CSClassTypeMap::Clear()
{
if (!IsEmpty())
{
  CS_Integer i;
  Node** data1 = (Node**) m_pData1;
  Node *p,*q;
  for (i = 0; i <= GetBucketsNum(); i++)
  {
   p = data1;
   while (p)
   {
    q = (Node*) p->next;
    delete p;
    p = q;
   }
  }
}
CSBaseMap:estroy();
}
//=======================================================================
//function : Add
//purpose  :
//=======================================================================
CS_Integer CSClassTypeMap::Add(const PCSClassTypeNode& K1)
{
if (Resizable())  ReSize(Extent());
Node** data1 = (Node**)m_pData1;
CS_Integer k1 = Hasher::HashCode(K1,GetBucketsNum());
Node* p;
p = data1[k1];
while (p) {
  if (Hasher::IsEqual(p->key1,K1))
   return p->key2;
  p = (Node*) p->next;
}
Increment();
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(Extent(),GetBucketsNum());
p = new Node(K1,Extent(),data1[k1],data2[k2]);
data1[k1] = p;
data2[k2] = p;
return Extent();
}
//=======================================================================
//function : Substitute
//purpose  :
//=======================================================================
void CSClassTypeMap::Substitute(const CS_Integer I,const PCSClassTypeNode& K1)
{
Standard_OutOfRange_Raise_if(I < 1 || I > Extent(),
  "IndexedMap::Substitute");
Node** data1 = (Node**)m_pData1;
Node* p;

// check if K1 is not already in the map
CS_Integer k1 = Hasher::HashCode(K1,GetBucketsNum());
p = data1[k1];
while (p) {
  if (Hasher::IsEqual(p->key1,K1))
   Standard_DomainError::Raise("IndexedMap::Substitute");
  p = (Node*) p->next;
}

// Find the node for the index I
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(I,GetBucketsNum());
p = data2[k2];
while (p) {
  if (p->key2 == I)
   break;
  p = (Node*) p->next2;
}

// remove the old key
CS_Integer k = Hasher::HashCode(p->key1,GetBucketsNum());
Node* q = data1[k];
if (q == p) data1[k] = (Node*) p->next;
else {
  while(q->next != p) q = (Node*) q->next;
  q->next = p->next;
}

// update the node
p->key1 = K1;
p->next = data1[k1];
data1[k1] = p;
}
//=======================================================================
//function : RemoveLast
//purpose  :
//=======================================================================
void CSClassTypeMap::RemoveLast()
{
Standard_OutOfRange_Raise_if(Extent() == 0,
  "IndexedMap::RemoveLast");
Node** data1 = (Node**)m_pData1;
Node* p;
Node* q;

// Find the node for the last index and remove it
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(Extent(),GetBucketsNum());
p = data2[k2];
q = NULL;
while (p) {
  if (p->key2 == Extent())
   break;
  q = p;
  p = (Node*) p->next2;
}
if (q == NULL)
  data2[k2] = p->next2;
else
  q->next2 = p->next2;

// remove the key
CS_Integer k = Hasher::HashCode(p->key1,GetBucketsNum());
q = data1[k];
if (q == p) data1[k] = (Node*) p->next;
else {
  while(q->next != p) q = (Node*) q->next;
  q->next = p->next;
}

Decrement();
delete p;
}
//=======================================================================
//function : Contains
//purpose  :
//=======================================================================
CS_Boolean CSClassTypeMap::Contains(const PCSClassTypeNode& K1) const
{
if (IsEmpty()) return CS_False;
Node** data1 = (Node**)m_pData1;
CS_Integer k1 = Hasher::HashCode(K1,GetBucketsNum());
Node *p1;
p1 = data1[k1];
while (p1) {
  if (Hasher::IsEqual(p1->key1,K1)) return CS_True;
  p1 = (Node*) p1->next;
}
return CS_False;
}
//=======================================================================
//function : FindKey
//purpose  :
//=======================================================================
static void *theNULL = NULL ;
const PCSClassTypeNode& CSClassTypeMap::FindKey(const CS_Integer K2) const
{
if (K2 < 1 || K2 > Extent())
  return (const PCSClassTypeNode& ) theNULL ;
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(K2,GetBucketsNum());
Node *p2;
p2 = data2[k2];
while (p2) {
  if (p2->key2 == K2) return p2->key1;
  p2 = p2->next2;
}
return (const PCSClassTypeNode& ) theNULL ;
}
//=======================================================================
//function : FindIndex
//purpose  :
//=======================================================================
CS_Integer CSClassTypeMap::FindIndex(const PCSClassTypeNode& K1) const
{
if (IsEmpty()) return 0;
Node** data1 = (Node**)m_pData1;
CS_Integer k1 = Hasher::HashCode(K1,GetBucketsNum());
Node *p1;
p1 = data1[k1];
while (p1) {
  if (Hasher::IsEqual(p1->key1,K1)) return p1->key2;
  p1 = (Node*) p1->next;
}
return 0;
}
inline PCSClassTypeNode CSClassTypeMap::FindFromType(const CSClassTypeHandle& aType ) const
{
return CSClassTypeMap::FindFromString( aType->Name() , aType->Size() ) ;
}
inline CS_Boolean CSClassTypeMap::IsUpdated()
{
CS_Boolean ret = m_bUpdate ;
m_bUpdate =CS_False ;
return ret ;
}

void CSClassTypeMap::ChangeSize(const CS_Integer N)
{
Node** pData1 = NULL ;
Node** pData2 = NULL ;
CS_Integer nBuck ;
if (BeginResize(N,nBuck,
  *(CS_Address*)&pData1,
  *(CS_Address*)&pData2)) {
  Node *p, *q;
  CS_Integer i,k1,k2;
  if (m_pData1) {
   Node** olddata1 = (Node**) m_pData1;
   for (i = 0; i <= GetBucketsNum(); i++) {
    if (olddata1) {
     p = olddata1;
     while (p) {
      k1 = HashCode(p->key1->HashCode(),nBuck);
      q = (Node*) p->next;
      p->next = pData1[k1];
      pData1[k1] = p;
      if (p->key2 > 0) {
       k2 = ::HashCode(p->key2,nBuck);
       p->next2 = pData2[k2];
       pData2[k2] = p;
      }
      p = q;
     }
    }
   }
  }
  EndResize(N,nBuck,pData1,pData2);
}
}
CS_Integer CSClassTypeMap::AddFromForMapOfTypes(const PCSClassTypeNode & K)
{
CS_Integer aHashCode ;

if (Resizable()) ReSize(Extent());

Node** data1 = (Node**) m_pData1;
CS_Integer k1 = Hasher::HashCode(K->Name(),K->Length(),GetBucketsNum(),aHashCode);
Node* p = data1[ k1 ];
while (p) {
  if (Hasher::IsEqual( p->key1 , K )) {
   return 0 ;
  }
  p = (Node*)p->next;
}
Increment();
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(Extent(),GetBucketsNum());
p = new Node(K,Extent(),data1[k1],data2[k2]);
data1[k1] = p;
data2[k2] = p;
K->AddHashCode( aHashCode ) ;
if ( 2*Extent() > GetBucketsNum() )
  ChangeSize( int( NextPrime( 3*GetBucketsNum() ) ) ) ;
K->Sort() ;
m_bUpdate = CS_True ;
return Extent();
}
CS_Integer CSClassTypeMap::AddFromType(const CSClassTypeHandle& K)
{
CS_Integer aHashCode ;
if (Resizable()) ReSize(Extent());
Node** data1 = (Node**) m_pData1;
CS_Integer k1 = Hasher::HashCode( K->Name() ,
  K->Size() ,
  GetBucketsNum() ,
  aHashCode ) ;
Node* p = data1[ k1 ];
while (p) {
  if (Hasher::IsEqual( p->key1 , K )) {
   if ( p->key1->IsNotLoaded() ) {
    PCSClassTypeNode pNode = new CSClassTypeNode( K ) ;
    p->key1->Copy( pNode );
   }
   return 0 ;
  }
  p = (Node*)p->next;
}
PCSClassTypeNode pNode = new CSClassTypeNode( K ) ;
Increment();
data1 = (Node**) m_pData1; // May have changed !
Node** data2 = (Node**)m_pData2;
CS_Integer k2 = ::HashCode(Extent(),GetBucketsNum());
p = new Node(pNode,Extent(),data1[k1],data2[k2]);
data1[k1] = p;
data2[k2] = p;
pNode->AddHashCode( aHashCode ) ;
if ( 2*Extent() > GetBucketsNum() )
  ChangeSize( int( NextPrime( 3*GetBucketsNum() ) ) ) ;
pNode->Sort() ;
m_bUpdate = CS_True ;
return Extent();
}
PCSClassTypeNode CSClassTypeMap::FindFromString(const CS_CString K ,
            const CS_Integer Len ) const
{
if (IsEmpty()) return NULL;
Node** data = (Node**) m_pData1;
CS_Integer i = Hasher::HashCode( K , Len ,GetBucketsNum()) ;
Node* p = data[ i ] ;
while (p)
{
  if (Hasher::IsEqual( p->key1 , K , Len ))
  {
   return p->key1 ;
  }
  p = (Node*)p->next;
}
return NULL ;
}
 楼主| 发表于 2007-7-22 21:22:53 | 显示全部楼层

回复: node sample

8.csmacro.h
#ifndef _CS_Macro_H
# define _CS_Macro_H
# define   Handle(ClassName)  ClassName##Handle
# define   Oid(ClassName)     Oid_##ClassName
# define   CS_TYPE(ClassName)   ClassName##_Type()
# ifdef WNT
#  ifndef CS_EXPORT
#   define CS_EXPORT __declspec( dllexport )
// For global variables :
#   define CS_EXPORTEXTERN __declspec( dllexport ) extern
#   define CS_EXPORTEXTERNC extern "C" __declspec( dllexport )
#  endif  /* CS_EXPORT */
#  ifndef CS_IMPORT
#   define CS_IMPORT __declspec( dllimport ) extern
#   define CS_IMPORTC extern "C" __declspec( dllimport )
#  endif  /* CS_IMPORT */
# else  /* WNT */
#  ifndef CS_EXPORT
#   define CS_EXPORT
// For global variables :
#   define CS_EXPORTEXTERN extern
#   define CS_EXPORTEXTERNC extern "C"
#  endif  /* CS_EXPORT */
#  ifndef CS_IMPORT
#   define CS_IMPORT extern
#   define CS_IMPORTC extern "C"
#  endif  /* CS_IMPORT */
# endif  /* WNT */
# ifndef __CS_API
//#  ifdef WNT
#   if !defined(WNT) || defined(__CS_DLL) || defined(__FSD_DLL) || defined(__GraphDS_DLL) || defined(__GraphTools_DLL) || defined(__MMgt_DLL) || defined(__OSD_DLL) || defined(__Plugin_DLL) || defined(__Quantity_DLL) || defined(__Resource_DLL) || defined(__SortTools_DLL) || defined(__StdFail_DLL) || defined(__Storage_DLL) || defined(__TColQuantity_DLL) || defined(__TColStd_DLL) || defined(__TCollection_DLL) || defined(__TShort_DLL) || defined(__Units_DLL) || defined(__UnitsAPI_DLL) || defined(__Dynamic_DLL) || defined(__Materials_DLL) || defined(__Dico_DLL)
#    define __CS_API CS_EXPORT
#    define __CS_APIEXTERN CS_EXPORTEXTERN
#   else
#    define __CS_API CS_IMPORT
#    define __CS_APIEXTERN CS_IMPORT
#   endif  // __CS_DLL
//#  else
//#   define __CS_API
//#  endif  // WNT
# endif  // __CS_API
#endif
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 12:39 , Processed in 0.047917 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表