• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Object-c 的TList类(翻译delphi版)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

为什么要实现一个tlist类,object-c自带nsarray和nsmutarray类,但这两个类都只能管理id类型也就是必须是nsobject或者是继承自nsobject的类型,因为在addobject和removeobject的时候,会去调用nsobject的retain和release方法对计数器加1和减1。有些时候它替我们管理,比较方便,但在某些时候就觉得它多管闲事了,感觉不通用。因此,我们更希望一个能存放任意指针类型的tlist类,下面附代码:

头文件:

#define MaxListSize (int)21474883647/6

typedef enum
{
        lnAdded,
        lnExtracted,
        lnDeleted
}TListNotifyication;

typedef void** PPointerList;

@protocol TListSortCompare
+(int)_Sort:(void*)Item1:(void*)Item2;
@end;

@interface TList : NSObject
{
        @private
        PPointerList FList;
        int FCount;
        int FCapacity;
}
//protected function
-(void)_Notify:(void*)P:(TListNotifyication)Action;
//protected function virtual
-(void)_Grow;
//public function
-(void)dealloc;
-(int)Add:(void*)Item;
-(void)Delete:(int)Index;
-(TList*)Expaned;
-(void*)Extract:(void*)Item;
-(void*)First;
-(int)IndexOf:(void*)Item;
-(void)Insert:(int)Index:(void*)Item;
-(void*)Last;
-(void)Move:(int)CurIndex:(int)NewIndex;
-(int)Remove:(void*)Item;
-(void)Pack;
-(void)Sort:(id<TListSortCompare>)Compare;
-(void)SetCapacity:(int)NewCapacity;
-(int)Capacity;
-(void)SetCount:(int)NewCount;
-(int)Count;
-(void*)Get:(int)Index;
-(void)Put:(int)Index:(void*)Item;
-(PPointerList)List;
-(void)Exchange:(int)Index1:(int)Index2;
//public function virtual
-(void)Clear;
@end

源文件:
//快速排序
void QuickSort(PPointerList List, int L, int R,
               id<TListSortCompare> SCompare)
{
    int i, j;
    void* p, *t;
    do {
        i = L;
        j = R;
        p = List[(L + R) >> 1];
        do{
            while([SCompare _Sort:List[i], p] < 0)
                ++i;
            while([SCompare _Sort:List[j], p] > 0)
                --j;
            if(i <= j)
            {
                t = List[i];
                List[i] = List[j];
                List[j] = t;
                ++i;
                --j;
            }
        }while(i < j);
        if(L < j)
            QuickSort(List, L, j, SCompare);
        L = i;
    }
    while(i <= R);
            
}


@implementation TList
-(void)dealloc
{
    [self Clear];
    [super dealloc];
};

-(void)Delete:(int)Index
{
    if((Index < 0)||(Index >= FCount))
       {
           raise(0);
           return;
       }
    void* temp = [self Get:Index];
    --FCount;
    if(Index < FCount)
        memcpy(&FList[Index], &FList[Index + 1], (FCount - Index) * sizeof(void*));
    if(temp)
        [self _Notify:temp :lnDeleted];
}

-(void)Exchange:(int)Index1 :(int)Index2
{
    if((Index1 < 0)||(Index1 >= FCount))
    {
        raise(0);
        return;
    }
    if((Index2 < 0)||(Index2 >= FCount))
    {
        raise(0);
        return;
    }
    void* item = FList[Index1];
    FList[Index1] = FList[Index2];
    FList[Index2] = item;
}

-(void*)Extract:(void *)Item
{
    void* res = nil;
    int i = [self IndexOf:Item];
    if(i >= 0)
    {
        res = Item;
        FList[i] = nil;
        [self Delete:i];
        [self _Notify:res :lnExtracted];
    }
    return res;
}

-(void)_Notify:(void *)P :(TListNotifyication)Action
{
    //nothing
}

-(int)Add:(void*)Item
{
    int res = FCount;
    if(res == FCapacity)
        [self _Grow];  
    memcpy(&FList[res], &Item, sizeof(void*));
    ++FCount;    
    if(Item)
        [self _Notify:Item:lnAdded];
    return res;
};

-(int)Capacity
{
    return FCapacity;
}

-(int)Count
{
    return FCount;
}

-(PPointerList)List
{
    return FList;
}

-(void)Clear
{
    [self SetCount:0];
    [self SetCapacity:0];
}
-(TList*)Expaned
{
  if(FCount == FCapacity)
      [self _Grow];
    return self;
}

-(void*)First
{
    return [self Get:0];
}

-(void*)Last
{
    return [self Get:FCount - 1];
}

-(void)Move:(int)CurIndex :(int)NewIndex
{
    if(CurIndex != NewIndex)
    {
        if((NewIndex < 0)||(NewIndex >= FCount))
            return;
        void* item = [self Get:CurIndex];
        FList[CurIndex] = nil;
        [self Delete:CurIndex];
        [self Insert:NewIndex :nil];
        FList[NewIndex] = item;
    }
}

-(void)Put:(int)Index :(void *)Item
{
    if((Index < 0)||(Index >= FCount))
    {
        raise(0);
        return;
    }
    if(Item != FList[Index])
    {
        void* temp = FList[Index];
        FList[Index] = Item;
        if(temp)
            [self _Notify:temp : lnDeleted];
        if(Item)
            [self _Notify:Item:lnAdded];
    }
}

-(int)Remove:(void *)Item
{
    int res = [self IndexOf:Item];
    if(res >= 0)
        [self Delete:res];
    return res;
}

-(void)Pack
{
    for(int i = FCount - 1; i >= 0; --i)
    {
        if([self Get:i] == nil)
            [self Delete:i];
    }
}

-(void)SetCapacity:(int)NewCapacity
{
    if((NewCapacity < FCount)||(NewCapacity > MaxListSize))
    {
        raise(0);
        return;
    }
    if(NewCapacity != FCapacity)
    {
        FList  = realloc(FList, NewCapacity * sizeof(void*));

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB曲线拟合函数发布时间:2022-07-18
下一篇:
Delphi2009之TBalloonHint发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap