Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1652046
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: LINUX

2010-09-10 12:47:18

class MTNumbelApply
{
public:
    MTNumbelApply( char m_max=192,char m_min=100,char t_max=192,char t_min=1):MaxMcuNum(m_max),MinMcuNum(m_min),
        MaxTerminalNumber(t_max),MinTerminalNumber(t_min)                            
    {
        if( (m_max>192) || ( m_max< 1 ) )
            MaxMcuNum=192;

        if( (m_min>192) || ( m_min< 1 ) )
            MinMcuNum=1;

        if( (t_max>192) || ( t_max< 1 ) )
            MaxTerminalNumber=192;

        if( (t_min>192) || ( t_min< 1 ) )
            MinTerminalNumber=1;            

    }


    ~MTNumbelApply()
    {
        set<char>* Ptr;
        map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();

        for(iter_map=McuTerminalSet.begin(); iter_map!=McuTerminalSet.end(); iter_map++)
        {
            Ptr= iter_map->second;
            if( Ptr!=NULL)
                delete Ptr;
        }

    }   

    char ApplyMcuNumber(void)
    {
        set<char>::iterator iter=McuSet.end();

        for( char i=MinMcuNum;i< MaxMcuNum;i++)
        {
            //McuSet.find( McuSet.begin(),McuSet.end(),i )


            iter=McuSet.find( i );

            if( iter==McuSet.end() )
            {
                McuSet.insert( i );

                TerminalSetPtr= new set<char>;          

                if( NULL)
                {
                    TerminalSetPtr->clear();
                    McuTerminalSet.insert(pair<char,set<char>*>(i,TerminalSetPtr) );
                    TerminalSetPtr=NULL;
                    return i;
                }
                else
                    return -1;                
            }            

        }
        return -1;
    }   

    char RegisterMcuNumber( char Mcu )
    {
        if( isMcuValid( Mcu ) ==false )
            return -1;

        set<char>::iterator iter=McuSet.end();

        iter=McuSet.find( Mcu );

        if( iter==McuSet.end() )
        {
            TerminalSetPtr= new set<char>;

            if( NULL)
            {
                TerminalSetPtr->clear();
                McuTerminalSet.insert(pair<char,set<char>*>(Mcu,TerminalSetPtr) );
                TerminalSetPtr=NULL;
                return Mcu;
            }
            else
                return -1;        
        }
        else
        {
            return -1;
        }
    }

    char RelaseMcuNumber( char Mcu)
    {
        if( isMcuValid( Mcu ) ==false )
            return -1;

        set<char>::iterator iterator_set=McuSet.end();
        map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();

        iterator_set=McuSet.find( Mcu );

        if( iterator_set !=McuSet.end() )
        {
            McuSet.erase( iterator_set );
            iter_map=McuTerminalSet.find(Mcu);
            if( iter_map!=McuTerminalSet.end() )
            {                
                TerminalSetPtr=iter_map->second;
                if( TerminalSetPtr!=NULL)
                    delete TerminalSetPtr;
                McuTerminalSet.erase(Mcu);

            }
            return Mcu;
        }
        else
        {
            return -1;
        }

    }    
    char ApplyTerminalNumber( char Mcu)
    {
        if( isMcuValid( Mcu ) ==false )
            return -1;

     if(     McuExist(Mcu)==false )
         return -1;

        set<char>::iterator iter;
        set<char>* Ptr;
        map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();

        iter_map=McuTerminalSet.find( Mcu );

        if( iter_map!=McuTerminalSet.end() )
        {
             Ptr=iter_map->second;
             if( Ptr==NULL )
                 return -1;

             for( char i=MinTerminalNumber;i< MaxTerminalNumber;i++)
             {
                 iter= Ptr->end();
                 iter= Ptr->find(i);

                 if( iter == Ptr->end() )
                 {
                     Ptr->insert( i );
                     return i;
                 }

             }
        }
        return -1;
        
    } 

    char RegisterTerminalNumber( char Mcu, char TermiNumber)
    {        
        if( isMcuValid( Mcu ) ==false )
            return -1;

        if( isTerminalValid( TermiNumber ) ==false )
            return -1;

        if(     McuExist(Mcu)==false )
            return -1;

        set<char>::iterator iter;
        set<char>* Ptr;
        map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();

        iter_map=McuTerminalSet.find( Mcu );

        if( iter_map!=McuTerminalSet.end() )
        {
            Ptr=iter_map->second;

            if( Ptr==NULL)
                return -1;

            iter= Ptr->end();
            iter= Ptr->find( TermiNumber );

            if( iter==Ptr->end() )
            {
                Ptr->insert( TermiNumber );
                return TermiNumber;
            }
            else
            {
                return -1;
            }
        }        
            return -1;        
    }

    char RelaseTerminalNumber( char Mcu, char TermiNumber)
    {
        if( isMcuValid( Mcu ) ==false )
            return -1;

        if( isTerminalValid( TermiNumber ) ==false )
            return -1;        

        if(     McuExist(Mcu)==false )
            return -1;

        set<char>::iterator iter;
        set<char>* Ptr;
        map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();

        iter_map=McuTerminalSet.find( Mcu );

        if( iter_map!=McuTerminalSet.end() )
        {
            Ptr=iter_map->second;

            if( Ptr==NULL)
                return -1;

            iter= Ptr->end();
            iter= Ptr->find( TermiNumber );
            if( iter!=Ptr->end())
            {
                Ptr->erase(iter);
                return TermiNumber;
            }
        }
        return -1;
    
    }

private:

    int MaxMcuNum;
    int MinMcuNum;

    int MaxTerminalNumber;        
    int MinTerminalNumber;

    set<char> McuSet;
    
    map<char,set<char>* > McuTerminalSet;
    set<char>* TerminalSetPtr;

    bool McuExist( char Mcu)
    {
        set<char>::iterator iter=McuSet.end();

        iter=McuSet.find( Mcu );
        if( iter==McuSet.end() )
            return false;        
        else
            return true;
    }

    bool isMcuValid( char Mcu )
    {
        if( (Mcu>MaxMcuNum) || ( Mcu<MinMcuNum ) )
            return false;
        else
            return true;
    }

    bool isTerminalValid( char Terminal )
    {
        if( (Terminal>MaxTerminalNumber) || ( Terminal<MinTerminalNumber ) )
            return false;
        else
            return true;
    }
};


阅读(3148) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~