最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法

 更新時(shí)間:2018年07月23日 17:13:42   投稿:mrr  
這篇文章主要介紹了iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

數(shù)組是線性結(jié)構(gòu)是容器類型,是一塊連續(xù)的內(nèi)存空間, iOS 中用 NSArray 和 NSMutableArray 集合類型,用來(lái)存放對(duì)象類型,其中 NSArray是不可變類型, NSMutableArray 是可變類型,能夠?qū)?shù)組中元素進(jìn)行增刪改查.

本文作者本著學(xué)習(xí)的態(tài)度,決定仿照NSArray和NSMutableArray 自己實(shí)現(xiàn)一個(gè)數(shù)組類型,當(dāng)然性能可能沒(méi)有 NSArray和NSMutableArray 的好,插入100000萬(wàn)條數(shù)據(jù),時(shí)間上是 NSMutableArray 的三倍左右 ,當(dāng)然平時(shí)使用過(guò)程中很少100000次這樣大的數(shù)據(jù)往數(shù)組里添加,因此性能方面可以忽略.

ArrayList.h 主要方法聲明 完全照搬 NSArray 和 NSMutableArray 的方法名稱

 

先發(fā)下測(cè)試結(jié)果

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    Person *p1 = [[Person alloc] init];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:100000];
//   ArrayList 
  *array = [ArrayList arrayWithCapacity:100000];
    CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent();
    for (int i = 0; i<100000; i++) {
      [array addObject:p1];
    }
    CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime);
    CFTimeInterval duration = linkTime * 1000.0f;
    NSLog(@"Linked in %f ms",duration);
    [self->_timeArray addObject:@(duration)];
    count++;
  });
NSMutableArray 5.081740292635832 ms
ArrayList 16.27591523257168 ms
以下是 ArrayList 的具體實(shí)現(xiàn) ,內(nèi)部是一個(gè) C語(yǔ)言的數(shù)組用來(lái)存放對(duì)象
//
// ArrayList.m
// ArrayList
//
// Created by dzb on 2018/7/19.
// Copyright © 2018 大兵布萊恩特. All rights reserved.
//
#import "ArrayList.h"
static NSInteger const defaultCapacity = 10;
typedef void * AnyObject;
@interface ArrayList ()
{
  AnyObject *_array;
  NSInteger _size;
  NSInteger _capacity;
}
@end
@implementation ArrayList
#pragma mark - init
- (instancetype)init
{
  self = [super init];
  if (self) {
    [self resetArray];
  }
  return self;
}
+ (instancetype)array {
  return [[ArrayList alloc] initWithCapacity:defaultCapacity];
}
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems {
  return [[ArrayList alloc] initWithCapacity:numItems];
}
- (instancetype)initWithCapacity:(NSUInteger)numItems {
  _capacity = numItems;
  _array = calloc(_capacity,sizeof(AnyObject));
  _size = 0;
  return self;
}
/**
 數(shù)組重置
 */
- (void) resetArray {
  _size = 0;
  if (_array != NULL)
    _array[_size] = NULL;
    free(_array);
  _capacity = defaultCapacity;
  _array = calloc(_capacity, sizeof(AnyObject));
}
#pragma makr - 增加操作
- (void)addObject:(id)anObject {
  [self insertObject:anObject atIndex:_size];
}
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
  if (!anObject) {
    @throw [NSException exceptionWithName:@"add object null." reason:@"object must be not null ." userInfo:nil];
    return;
  }
  ///判越界
  if ((index > _size)) {
    @throw [NSException exceptionWithName:@"Array is out of bounds" reason:@"out of bounds" userInfo:nil];
    return;
  }
  if (_size == _capacity-1) { ///判斷原來(lái)數(shù)組是否已經(jīng)滿了 如果滿了就需要增加數(shù)組長(zhǎng)度
    [self resize:2*_capacity];
  }
  ///交換索引位置
  if (self.count > 0 ) {
    for(NSInteger i = _size - 1 ; i >= index ; i--)
      _array[i + 1] = _array[i];
  }
  self->_array[index] = (__bridge_retained AnyObject)(anObject);
  _size++;
}
#pragma mark - 刪除操作
- (void)removeAllObjects {
  NSInteger i = _size-1;
  while (_size > 0) {
    [self removeObjectAtIndex:i];
    i--;
  }
  [self resetArray];
}
- (void)removeObjectAtIndex:(NSUInteger)index {
  ///判斷越界
  if ((index > _size)) {
    @throw [NSException exceptionWithName:@"Array is out of bounds" reason:@"out of bounds" userInfo:nil];
    return;
  }
  AnyObject object =(_array[index]);
  CFRelease(object);
  for(NSInteger i = index + 1 ; i < _size ; i ++)
    _array[i - 1] = _array[i];
  _size--;
  _array[_size] = NULL;
  ///對(duì)數(shù)組空間縮減
  if (_size == _capacity/2) {
    [self resize:_capacity/2];
  }
}
- (void)removeObject:(id)anObject {
  NSInteger index = [self indexOfObject:anObject];
  if (index == NSNotFound) return;
  [self removeObjectAtIndex:index];
}
- (void)removeLastObject {
  if ([self isEmpty]) return;
  [self removeObjectAtIndex:_size-1];
}
#pragma mark - 修改操作
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
  if (!anObject) {
    @throw [NSException exceptionWithName:@"add object null." reason:@"object must be not null ." userInfo:nil];
    return;
  }
  ///判斷越界
  if ((index > _size)) {
    @throw [NSException exceptionWithName:@"Array is out of bounds" reason:@"out of bounds" userInfo:nil];
    return;
  }
  _array[index] = (__bridge AnyObject)(anObject);
}
#pragma mark - 查詢操作
- (BOOL) isEmpty {
  return (self->_size == 0);
}
- (BOOL) isFull {
  return (self->_size == self->_capacity-1);
}
- (id)objectAtIndex:(NSUInteger)index {
  if ((index > _size)) {
    @throw [NSException exceptionWithName:@"Array is out of bounds" reason:@"out of bounds" userInfo:nil];
    return nil;
  }
  if ([self isEmpty]) { return nil; }
  AnyObject obj = _array[index];
  if (obj == NULL) return nil;
  return (__bridge id)(obj);
}
- (NSUInteger)indexOfObject:(id)anObject {
  for (int i = 0; i<_size; i++) {
    id obj = (__bridge id)(_array[i]);
    if ([anObject isEqual:obj]) return i;
  }
  return NSNotFound;
}
- (BOOL)containsObject:(id)anObject {
  for (int i = 0; i<_size; i++) {
    id obj = (__bridge id)(_array[i]);
    if ([anObject isEqual:obj]) return YES;
  }
  return NO;
}
- (id)firstObject {
  if ([self isEmpty]) return nil;
  return (__bridge id _Nullable)(_array[0]);
}
- (id)lastObject {
  if ([self isEmpty]) return nil;
  return (__bridge id _Nullable)(_array[_size]);
}
- (NSUInteger)count {
  return _size;
}
- (NSString *)description {
  NSMutableString *string = [NSMutableString stringWithFormat:@"\nArrayList %p : [ \n" ,self];
  for (int i = 0; i<_size; i++) {
    AnyObject obj = _array[i];
    [string appendFormat:@"%@",(__bridge id)obj];
    if (i<_size-1) {
      [string appendString:@" , \n"];
    }
  }
  [string appendString:@"\n]\n"];
  return string;
}
/**
 對(duì)數(shù)組擴(kuò)容
 @param capacity 新的容量
 */
- (void) resize:(NSInteger)capacity {
  AnyObject *oldArray = _array;
  AnyObject *newArray = calloc(capacity, sizeof(AnyObject));
  for (int i = 0 ; i<_size; i++) {
    newArray[i] = oldArray[i];
  }
  _array = newArray;
  _capacity = capacity;
  free(oldArray);
}
- (void)dealloc
{
  if (_array != NULL)
    [self removeAllObjects];
  free(_array);
// NSLog(@"ArrayList dealloc");
}
@end

經(jīng)過(guò)測(cè)試 數(shù)組內(nèi)部會(huì)對(duì)存入的對(duì)象 進(jìn)行 retain 操作 其引用計(jì)數(shù)+1 ,當(dāng)對(duì)象從數(shù)組中移除的時(shí)候 能夠正常的使對(duì)象內(nèi)存引用計(jì)數(shù)-1,因此不必?fù)?dān)心對(duì)象內(nèi)存管理的問(wèn)題. 數(shù)組默認(rèn)長(zhǎng)度是10 , 如果在開(kāi)發(fā)者不確定數(shù)組長(zhǎng)度時(shí)候 ,其內(nèi)部可以動(dòng)態(tài)的擴(kuò)容增加數(shù)組長(zhǎng)度,當(dāng)執(zhí)行 remove 操作時(shí)候 也會(huì)對(duì)數(shù)組內(nèi)部長(zhǎng)度 進(jìn)行相應(yīng)的縮減

實(shí)現(xiàn)了 NSArray 和 NSMutableArray 等常用API,如果不是對(duì)性能特別在意的場(chǎng)景下 ,可以使用 ArrayList 來(lái)存放一些數(shù)據(jù)

相關(guān)文章

  • IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實(shí)例

    IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實(shí)例

    本篇文章主要介紹了IOS用AFN發(fā)送字符串形式的Json數(shù)據(jù)給服務(wù)器實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • IOS 圖文混排(CoreText.framework)詳解及實(shí)例

    IOS 圖文混排(CoreText.framework)詳解及實(shí)例

    這篇文章主要介紹了IOS 圖文混排(CoreText.framework)詳解及實(shí)例的相關(guān)資料,這里對(duì)IOS 的圖文混排進(jìn)行了詳細(xì)介紹,并附代碼實(shí)例,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下
    2016-11-11
  • IOS ObjectiveC中的賦值與對(duì)象拷貝

    IOS ObjectiveC中的賦值與對(duì)象拷貝

    這篇文章主要介紹了IOS ObjectiveC中的賦值與對(duì)象拷貝的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • iOS逆向工程之Hopper中的ARM指令詳解

    iOS逆向工程之Hopper中的ARM指令詳解

    這篇文章主要介紹了iOS逆向工程之Hopper中的ARM指令的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • IOS之構(gòu)造方法與自定義構(gòu)造方法的區(qū)別與實(shí)現(xiàn)

    IOS之構(gòu)造方法與自定義構(gòu)造方法的區(qū)別與實(shí)現(xiàn)

    本篇文章主要介紹了構(gòu)造方法以及自定義構(gòu)造方法的實(shí)現(xiàn),需要的朋友可以參考下
    2015-07-07
  • 利用iOS手勢(shì)與scrollView代理實(shí)現(xiàn)圖片的放大縮小

    利用iOS手勢(shì)與scrollView代理實(shí)現(xiàn)圖片的放大縮小

    這篇文章主要介紹了利用iOS的手勢(shì)、scrollView代理來(lái)實(shí)現(xiàn)圖片放大縮小的方法,文中通過(guò)示例代碼介紹的很詳細(xì),相信對(duì)各位iOS開(kāi)發(fā)者們來(lái)說(shuō)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • 一個(gè)方法搞定iOS下拉放大及上推縮小

    一個(gè)方法搞定iOS下拉放大及上推縮小

    在很多的APP中,我們可以看到一個(gè)列表頂部的圖片會(huì)隨著下拉會(huì)放大,隨著上推縮小。這樣的效果沒(méi)能給定一個(gè)固有名詞,現(xiàn)在本文介紹使用代碼實(shí)現(xiàn)這樣的效果,代碼量很少,容易理解。當(dāng)然實(shí)現(xiàn)效果是很好的。
    2016-07-07
  • 一步一步實(shí)現(xiàn)iOS主題皮膚切換效果

    一步一步實(shí)現(xiàn)iOS主題皮膚切換效果

    這篇文章主要為大家詳細(xì)介紹了一步一步實(shí)現(xiàn)iOS主題皮膚切換效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 分享一些iOS開(kāi)發(fā)實(shí)用的小技巧

    分享一些iOS開(kāi)發(fā)實(shí)用的小技巧

    這篇文章主要給大家分享了一些iOS開(kāi)發(fā)實(shí)用的小技巧,這些小技巧在大家開(kāi)發(fā)iOS的時(shí)候還是相當(dāng)實(shí)用,有需要的朋友們下面來(lái)一起看看吧。
    2016-09-09
  • iOS中的集合該如何弱引用對(duì)象示例詳解

    iOS中的集合該如何弱引用對(duì)象示例詳解

    這篇文章主要給大家介紹了關(guān)于在iOS中的集合該如何弱引用對(duì)象的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)依稀學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09

最新評(píng)論

文登市| 绥宁县| 民和| 侯马市| 罗田县| 开远市| 和硕县| 克东县| 江山市| 临泽县| 富川| 崇仁县| 旬邑县| 威海市| 南汇区| 齐齐哈尔市| 和龙市| 新安县| 梁山县| 都安| 凤阳县| 长垣县| 三明市| 阿城市| 封开县| 桓台县| 独山县| 阳信县| 湖北省| 封丘县| 通海县| 内江市| 铁岭市| 安岳县| 岫岩| 台江县| 西充县| 铜鼓县| 德江县| 黄石市| 贡嘎县|