PhotoHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using MediaBrowser.Controller.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace MediaBrowser.Providers.Photos
  6. {
  7. public static class PhotoHelper
  8. {
  9. public static List<BaseItem> ShuffleList(List<BaseItem> list)
  10. {
  11. var rnd = new Random(DateTime.Now.Second);
  12. for (var i = 1; i < list.Count; i++)
  13. {
  14. var pos = rnd.Next(i + 1);
  15. var x = list[i];
  16. list[i] = list[pos];
  17. list[pos] = x;
  18. }
  19. return list;
  20. }
  21. public static string Dec2Frac(double dbl)
  22. {
  23. char neg = ' ';
  24. double dblDecimal = dbl;
  25. if (dblDecimal == (int)dblDecimal) return dblDecimal.ToString(); //return no if it's not a decimal
  26. if (dblDecimal < 0)
  27. {
  28. dblDecimal = Math.Abs(dblDecimal);
  29. neg = '-';
  30. }
  31. var whole = (int)Math.Truncate(dblDecimal);
  32. string decpart = dblDecimal.ToString().Replace(Math.Truncate(dblDecimal) + ".", "");
  33. double rN = Convert.ToDouble(decpart);
  34. double rD = Math.Pow(10, decpart.Length);
  35. string rd = Recur(decpart);
  36. int rel = Convert.ToInt32(rd);
  37. if (rel != 0)
  38. {
  39. rN = rel;
  40. rD = (int)Math.Pow(10, rd.Length) - 1;
  41. }
  42. //just a few prime factors for testing purposes
  43. var primes = new[] { 47, 43, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2 };
  44. foreach (int i in primes) ReduceNo(i, ref rD, ref rN);
  45. rN = rN + (whole * rD);
  46. return string.Format("{0}{1}/{2}", neg, rN, rD);
  47. }
  48. /// <summary>
  49. /// Finds out the recurring decimal in a specified number
  50. /// </summary>
  51. /// <param name="db">Number to check</param>
  52. /// <returns></returns>
  53. private static string Recur(string db)
  54. {
  55. if (db.Length < 13) return "0";
  56. var sb = new StringBuilder();
  57. for (int i = 0; i < 7; i++)
  58. {
  59. sb.Append(db[i]);
  60. int dlength = (db.Length / sb.ToString().Length);
  61. int occur = Occurence(sb.ToString(), db);
  62. if (dlength == occur || dlength == occur - sb.ToString().Length)
  63. {
  64. return sb.ToString();
  65. }
  66. }
  67. return "0";
  68. }
  69. /// <summary>
  70. /// Checks for number of occurence of specified no in a number
  71. /// </summary>
  72. /// <param name="s">The no to check occurence times</param>
  73. /// <param name="check">The number where to check this</param>
  74. /// <returns></returns>
  75. private static int Occurence(string s, string check)
  76. {
  77. int i = 0;
  78. int d = s.Length;
  79. string ds = check;
  80. for (int n = (ds.Length / d); n > 0; n--)
  81. {
  82. if (ds.Contains(s))
  83. {
  84. i++;
  85. ds = ds.Remove(ds.IndexOf(s, System.StringComparison.Ordinal), d);
  86. }
  87. }
  88. return i;
  89. }
  90. /// <summary>
  91. /// Reduces a fraction given the numerator and denominator
  92. /// </summary>
  93. /// <param name="i">Number to use in an attempt to reduce fraction</param>
  94. /// <param name="rD">the Denominator</param>
  95. /// <param name="rN">the Numerator</param>
  96. private static void ReduceNo(int i, ref double rD, ref double rN)
  97. {
  98. //keep reducing until divisibility ends
  99. while ((rD % i) < 1e-10 && (rN % i) < 1e-10)
  100. {
  101. rN = rN / i;
  102. rD = rD / i;
  103. }
  104. }
  105. }
  106. }