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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2012 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. #import "ZXRSSUtils.h"
  18. @implementation ZXRSSUtils
  19. /*
  20. + (NSArray *)rssWidths:(int)val n:(int)n elements:(int)elements maxWidth:(int)maxWidth noNarrow:(BOOL)noNarrow {
  21. NSMutableArray *widths = [NSMutableArray arrayWithCapacity:elements];
  22. int bar;
  23. int narrowMask = 0;
  24. for (bar = 0; bar < elements - 1; bar++) {
  25. narrowMask |= 1 << bar;
  26. int elmWidth = 1;
  27. int subVal;
  28. while (YES) {
  29. subVal = [self combins:n - elmWidth - 1 r:elements - bar - 2];
  30. if (noNarrow && (narrowMask == 0) && (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) {
  31. subVal -= [self combins:n - elmWidth - (elements - bar) r:elements - bar - 2];
  32. }
  33. if (elements - bar - 1 > 1) {
  34. int lessVal = 0;
  35. for (int mxwElement = n - elmWidth - (elements - bar - 2); mxwElement > maxWidth; mxwElement--) {
  36. lessVal += [self combins:n - elmWidth - mxwElement - 1 r:elements - bar - 3];
  37. }
  38. subVal -= lessVal * (elements - 1 - bar);
  39. } else if (n - elmWidth > maxWidth) {
  40. subVal--;
  41. }
  42. val -= subVal;
  43. if (val < 0) {
  44. break;
  45. }
  46. elmWidth++;
  47. narrowMask &= ~(1 << bar);
  48. }
  49. val += subVal;
  50. n -= elmWidth;
  51. [widths addObject:@(elmWidth)];
  52. }
  53. [widths addObject:@(n)];
  54. return widths;
  55. }
  56. */
  57. + (int)rssValue:(ZXIntArray *)widths maxWidth:(int)maxWidth noNarrow:(BOOL)noNarrow {
  58. int elements = widths.length;
  59. int n = 0;
  60. for (int i = 0; i < elements; i++) {
  61. n += widths.array[i];
  62. }
  63. int val = 0;
  64. int narrowMask = 0;
  65. for (int bar = 0; bar < elements - 1; bar++) {
  66. int elmWidth;
  67. for (elmWidth = 1, narrowMask |= 1 << bar;
  68. elmWidth < widths.array[bar];
  69. elmWidth++, narrowMask &= ~(1 << bar)) {
  70. int subVal = [self combins:n - elmWidth - 1 r:elements - bar - 2];
  71. if (noNarrow && (narrowMask == 0) &&
  72. (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) {
  73. subVal -= [self combins:n - elmWidth - (elements - bar)
  74. r:elements - bar - 2];
  75. }
  76. if (elements - bar - 1 > 1) {
  77. int lessVal = 0;
  78. for (int mxwElement = n - elmWidth - (elements - bar - 2);
  79. mxwElement > maxWidth; mxwElement--) {
  80. lessVal += [self combins:n - elmWidth - mxwElement - 1
  81. r:elements - bar - 3];
  82. }
  83. subVal -= lessVal * (elements - 1 - bar);
  84. } else if (n - elmWidth > maxWidth) {
  85. subVal--;
  86. }
  87. val += subVal;
  88. }
  89. n -= elmWidth;
  90. }
  91. return val;
  92. }
  93. + (int)combins:(int)n r:(int)r {
  94. int maxDenom;
  95. int minDenom;
  96. if (n - r > r) {
  97. minDenom = r;
  98. maxDenom = n - r;
  99. } else {
  100. minDenom = n - r;
  101. maxDenom = r;
  102. }
  103. int val = 1;
  104. int j = 1;
  105. for (int i = n; i > maxDenom; i--) {
  106. val *= i;
  107. if (j <= minDenom) {
  108. val /= j;
  109. j++;
  110. }
  111. }
  112. while (j <= minDenom) {
  113. val /= j;
  114. j++;
  115. }
  116. return val;
  117. }
  118. /*
  119. + (NSArray *)elements:(NSArray *)eDist N:(int)N K:(int)K {
  120. NSMutableArray *widths = [NSMutableArray arrayWithCapacity:[eDist count] + 2];
  121. int twoK = 2 * K;
  122. [widths addObject:@1];
  123. int i;
  124. int minEven = 10;
  125. int barSum = 1;
  126. for (i = 1; i < twoK - 2; i += 2) {
  127. [widths addObject:@([eDist[i - 1] intValue] - [widths[i - 1] intValue])];
  128. [widths addObject:@([eDist[i] intValue] - [widths[i] intValue])];
  129. barSum += [widths[i] intValue] + [widths[i + 1] intValue];
  130. if ([widths[i] intValue] < minEven) {
  131. minEven = [widths[i] intValue];
  132. }
  133. }
  134. [widths addObject:@(N - barSum)];
  135. if ([widths[twoK - 1] intValue] < minEven) {
  136. minEven = [widths[twoK - 1] intValue];
  137. }
  138. if (minEven > 1) {
  139. for (i = 0; i < twoK; i += 2) {
  140. widths[i] = @([widths[i] intValue] + minEven - 1);
  141. widths[i + 1] = @([widths[i + 1] intValue] - minEven - 1);
  142. }
  143. }
  144. return widths;
  145. }
  146. */
  147. @end