PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Backend/Modules/_bisect.cs

https://bitbucket.org/AdamMil/boaold
C# | 76 lines | 45 code | 11 blank | 20 comment | 6 complexity | 5697ffaf81e64eaad7c7ea2d5375e63e MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using Boa.Runtime;
  21. namespace Boa.Modules
  22. {
  23. [BoaType("module")]
  24. public sealed class _bisect
  25. { _bisect() { }
  26. public static string __repr__() { return "<module 'bisect' (built-in)>"; }
  27. public static string __str__() { return __repr__(); }
  28. public static int bisect(List a, object x) { return bisect_right(a, x, 0, -1); }
  29. public static int bisect(List a, object x, int lo) { return bisect_right(a, x, lo, -1); }
  30. public static int bisect(List a, object x, int lo, int hi) { return bisect_right(a, x, lo, hi); }
  31. public static int bisect_left(List a, object x) { return bisect_left(a, x, 0, -1); }
  32. public static int bisect_left(List a, object x, int lo) { return bisect_left(a, x, lo, -1); }
  33. public static int bisect_left(List a, object x, int lo, int hi)
  34. { if(hi<0) hi=a.Count;
  35. while(lo<hi)
  36. { int mid = (lo+hi)/2;
  37. if(Ops.Compare(x, a[mid])<0) lo=mid+1;
  38. else hi=mid;
  39. }
  40. return lo;
  41. }
  42. public static int bisect_right(List a, object x) { return bisect_right(a, x, 0, -1); }
  43. public static int bisect_right(List a, object x, int lo) { return bisect_right(a, x, lo, -1); }
  44. public static int bisect_right(List a, object x, int lo, int hi)
  45. { if(hi<0) hi=a.Count;
  46. while(lo<hi)
  47. { int mid = (lo+hi)/2;
  48. if(Ops.Compare(x, a[mid])<0) hi=mid;
  49. else lo=mid+1;
  50. }
  51. return lo;
  52. }
  53. public static void insort(List a, object x) { insort_right(a, x, 0, -1); }
  54. public static void insort(List a, object x, int lo) { insort_right(a, x, lo, -1); }
  55. public static void insort(List a, object x, int lo, int hi) { insort_right(a, x, lo, hi); }
  56. public static void insort_left(List a, object x) { insort_left(a, x, 0, -1); }
  57. public static void insort_left(List a, object x, int lo) { insort_left(a, x, lo, -1); }
  58. public static void insort_left(List a, object x, int lo, int hi) { a.insert(bisect_left(a, x, lo, hi), x); }
  59. public static void insort_right(List a, object x) { insort_right(a, x, 0, -1); }
  60. public static void insort_right(List a, object x, int lo) { insort_right(a, x, lo, -1); }
  61. public static void insort_right(List a, object x, int lo, int hi) { a.insert(bisect_right(a, x, lo, hi), x); }
  62. }
  63. } // namespace Boa.Modules