You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXIntArray.m 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2014 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXIntArray.h"
  17. @implementation ZXIntArray
  18. - (id)initWithLength:(unsigned int)length {
  19. if (self = [super init]) {
  20. if (length > 0) {
  21. _array = (int32_t *)calloc(length, sizeof(int32_t));
  22. } else {
  23. _array = NULL;
  24. }
  25. _length = length;
  26. }
  27. return self;
  28. }
  29. - (id)initWithInts:(int32_t)int1, ... {
  30. va_list args;
  31. va_start(args, int1);
  32. unsigned int length = 0;
  33. for (int32_t i = int1; i != -1; i = va_arg(args, int)) {
  34. length++;
  35. }
  36. va_end(args);
  37. if ((self = [self initWithLength:length]) && (length > 0)) {
  38. va_list args;
  39. va_start(args, int1);
  40. int i = 0;
  41. for (int32_t c = int1; c != -1; c = va_arg(args, int)) {
  42. _array[i++] = c;
  43. }
  44. va_end(args);
  45. }
  46. return self;
  47. }
  48. - (id)copyWithZone:(NSZone *)zone {
  49. ZXIntArray *copy = [[ZXIntArray allocWithZone:zone] initWithLength:self.length];
  50. memcpy(copy.array, self.array, self.length * sizeof(int32_t));
  51. return copy;
  52. }
  53. - (void)dealloc {
  54. if (_array) {
  55. free(_array);
  56. }
  57. }
  58. - (void)clear {
  59. memset(self.array, 0, self.length * sizeof(int32_t));
  60. }
  61. - (int)sum {
  62. int sum = 0;
  63. int32_t *array = self.array;
  64. for (int i = 0; i < self.length; i++) {
  65. sum += array[i];
  66. }
  67. return sum;
  68. }
  69. - (NSString *)description {
  70. NSMutableString *s = [NSMutableString stringWithFormat:@"length=%u, array=(", self.length];
  71. for (int i = 0; i < self.length; i++) {
  72. [s appendFormat:@"%d", self.array[i]];
  73. if (i < self.length - 1) {
  74. [s appendString:@", "];
  75. }
  76. }
  77. [s appendString:@")"];
  78. return s;
  79. }
  80. @end