Guest User

Untitled

a guest
May 26th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Index: gmpy_mpz.c
  2. ===================================================================
  3. --- gmpy_mpz.c (revision 311)
  4. +++ gmpy_mpz.c (working copy)
  5. @@ -400,72 +400,99 @@
  6. return result;
  7. }
  8.  
  9. +static int gmpy_mpz_bit_set_iterable(PyObject *self, PyObject *other) {
  10. + long bit_index;
  11. + PyObject *iterator, *item;
  12. +
  13. + iterator = PyObject_GetIter(other);
  14. + if (!iterator) {
  15. + TYPE_ERROR("bit_set() failed with iterator");
  16. + return -1;
  17. + }
  18. +
  19. + while ((item = PyIter_Next(iterator))) {
  20. +
  21. + bit_index = clong_From_Integer(item);
  22. + if(bit_index == -1 && PyErr_Occurred()) {
  23. + TYPE_ERROR("bit_set() requires 'mpz','int' arguments");
  24. + goto err;
  25. + }
  26. +
  27. + if(bit_index < 0) {
  28. + VALUE_ERROR("bit_index must be >= 0");
  29. + goto err;
  30. + }
  31. +
  32. + mpz_setbit(Pympz_AS_MPZ(self), bit_index);
  33. + Py_DECREF(item);
  34. + }
  35. + Py_DECREF(iterator);
  36. + return 0;
  37. + err:
  38. + Py_DECREF(item);
  39. + Py_DECREF(iterator);
  40. + return -1;
  41. +}
  42. +
  43. +static int gmpy_mpz_bit_setall(PyObject *self, PyObject *other) {
  44. + Py_ssize_t i, cur, length, start, stop, step, slicelength;
  45. +
  46. + if (!PySlice_Check(other))
  47. + return gmpy_mpz_bit_set_iterable(self, other);
  48. +
  49. + /* slice */
  50. + length = mpz_sizeinbase(Pympz_AS_MPZ(self), 2);
  51. + if (PySlice_GetIndicesEx((PySliceObject*)other, length,
  52. + &start, &stop, &step, &slicelength) < 0) {
  53. + TYPE_ERROR("bit_set() failed with slice");
  54. + return -1;
  55. + }
  56. +
  57. + for (cur = start, i = 0; i < slicelength;
  58. + cur += step, i++) {
  59. + mpz_setbit(Pympz_AS_MPZ(self), cur);
  60. + }
  61. + return 0;
  62. +}
  63. +
  64. static PyObject *
  65. Pympz_bit_set(PyObject *self, PyObject *other)
  66. {
  67. long bit_index;
  68. - PyObject *result, *iterator, *item;
  69. - int temp;
  70. - Py_ssize_t i, length, start, stop, step, slicelength;
  71. + PyObject *result = NULL;
  72. + int is_mpz = 0;
  73.  
  74. - if((Pyxmpz_Check(self) && (PyIter_Check(other)))) {
  75. - iterator = PyObject_GetIter(other);
  76. - if(!iterator) {
  77. - TYPE_ERROR("bit_set() failed with iterator");
  78. - return NULL;
  79. - }
  80. - while((item = PyIter_Next(iterator))) {
  81. - bit_index = clong_From_Integer(item);
  82. - if(bit_index == -1 && PyErr_Occurred()) {
  83. - TYPE_ERROR("bit_set() requires 'mpz','int' arguments");
  84. - Py_DECREF(item);
  85. - Py_DECREF(iterator);
  86. - return NULL;
  87. - }
  88. - if(bit_index < 0) {
  89. - VALUE_ERROR("bit_index must be >= 0");
  90. - Py_DECREF(item);
  91. - Py_DECREF(iterator);
  92. - return NULL;
  93. - }
  94. - mpz_setbit(Pympz_AS_MPZ(self), bit_index);
  95. - Py_DECREF(item);
  96. - }
  97. - Py_DECREF(iterator);
  98. - Py_RETURN_NONE;
  99. - } else if((Pyxmpz_Check(self) && (PySlice_Check(other)))) {
  100. - length = mpz_sizeinbase(Pympz_AS_MPZ(self), 2);
  101. - temp = PySlice_GetIndicesEx((PySliceObject*)other, length, &start, &stop, &step, &slicelength);
  102. - if(temp == -1) {
  103. - TYPE_ERROR("bit_set() failed with slice");
  104. - return NULL;
  105. - }
  106. - for(i=start;i<stop;i+=step) {
  107. - mpz_setbit(Pympz_AS_MPZ(self), i);
  108. - }
  109. - Py_RETURN_NONE;
  110. - } else {
  111. - bit_index = clong_From_Integer(other);
  112. - if(bit_index == -1 && PyErr_Occurred()) {
  113. - TYPE_ERROR("bit_set() requires 'mpz','int' arguments");
  114. - return NULL;
  115. - }
  116. + if (!Pyxmpz_Check(self)) {
  117. + is_mpz = 1;
  118. + CREATE0_ONE_MPZANY(result);
  119. + mpz_set(Pympz_AS_MPZ(result), Pympz_AS_MPZ(self));
  120. + self = result;
  121. + }
  122.  
  123. - if(bit_index < 0) {
  124. - VALUE_ERROR("bit_index must be >= 0");
  125. - return NULL;
  126. - }
  127. + bit_index = clong_From_Integer(other);
  128. + if (bit_index < 0) {
  129. + if (bit_index == -1 && PyErr_Occurred()) {
  130. + /* integer index failed */
  131. + PyErr_Clear();
  132. + if (gmpy_mpz_bit_setall(self, other) == -1)
  133. + goto err;
  134. + goto ret;
  135. + }
  136. + VALUE_ERROR("bit_index must be >= 0");
  137. + goto err;
  138. + }
  139.  
  140. - if(Pyxmpz_Check(self)) {
  141. - mpz_setbit(Pympz_AS_MPZ(self), bit_index);
  142. - Py_RETURN_NONE;
  143. - } else {
  144. - CREATE0_ONE_MPZANY(result);
  145. - mpz_set(Pympz_AS_MPZ(result), Pympz_AS_MPZ(self));
  146. - mpz_setbit(Pympz_AS_MPZ(result), bit_index);
  147. - return result;
  148. - }
  149. - }
  150. + mpz_setbit(Pympz_AS_MPZ(self), bit_index);
  151. + goto ret;
  152. +
  153. + err:
  154. + if (is_mpz)
  155. + Py_DECREF(result);
  156. + return NULL;
  157. + ret:
  158. + if (is_mpz)
  159. + return result; /* return new reference */
  160. + Py_RETURN_NONE;
  161. }
  162.  
  163. static char doc_bit_flipm[]="\
Add Comment
Please, Sign In to add comment