GenericTypes.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GenericTypes.h"
  7. #include <AK/BinarySearch.h>
  8. #include <AK/Error.h>
  9. #include <AK/Optional.h>
  10. namespace Audio {
  11. size_t SeekTable::size() const
  12. {
  13. return m_seek_points.size();
  14. }
  15. ReadonlySpan<SeekPoint> SeekTable::seek_points() const
  16. {
  17. return m_seek_points.span();
  18. }
  19. Vector<SeekPoint>& SeekTable::seek_points()
  20. {
  21. return m_seek_points;
  22. }
  23. Optional<SeekPoint const&> SeekTable::seek_point_before(u64 sample_index) const
  24. {
  25. if (m_seek_points.is_empty())
  26. return {};
  27. size_t nearby_seek_point_index = 0;
  28. AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) {
  29. // Subtraction with i64 cast may cause overflow.
  30. if (sample_index > seekpoint_candidate.sample_index)
  31. return 1;
  32. if (sample_index == seekpoint_candidate.sample_index)
  33. return 0;
  34. return -1;
  35. });
  36. // Binary search will always give us a close index, but it may be too large or too small.
  37. // By doing the index adjustment in this order, we will always find a seek point before the given sample.
  38. while (nearby_seek_point_index < m_seek_points.size() - 1 && m_seek_points[nearby_seek_point_index].sample_index < sample_index)
  39. ++nearby_seek_point_index;
  40. while (nearby_seek_point_index > 0 && m_seek_points[nearby_seek_point_index].sample_index > sample_index)
  41. --nearby_seek_point_index;
  42. if (m_seek_points[nearby_seek_point_index].sample_index > sample_index)
  43. return {};
  44. return m_seek_points[nearby_seek_point_index];
  45. }
  46. Optional<u64> SeekTable::seek_point_sample_distance_around(u64 sample_index) const
  47. {
  48. if (m_seek_points.is_empty())
  49. return {};
  50. size_t nearby_seek_point_index = 0;
  51. AK::binary_search(m_seek_points, sample_index, &nearby_seek_point_index, [](auto const& sample_index, auto const& seekpoint_candidate) {
  52. // Subtraction with i64 cast may cause overflow.
  53. if (sample_index > seekpoint_candidate.sample_index)
  54. return 1;
  55. if (sample_index == seekpoint_candidate.sample_index)
  56. return 0;
  57. return -1;
  58. });
  59. while (nearby_seek_point_index < m_seek_points.size() && m_seek_points[nearby_seek_point_index].sample_index <= sample_index)
  60. ++nearby_seek_point_index;
  61. // There is no seek point beyond the sample index.
  62. if (nearby_seek_point_index >= m_seek_points.size())
  63. return {};
  64. auto upper_seek_point_index = nearby_seek_point_index;
  65. while (nearby_seek_point_index > 0 && m_seek_points[nearby_seek_point_index].sample_index > sample_index)
  66. --nearby_seek_point_index;
  67. auto lower_seek_point_index = nearby_seek_point_index;
  68. VERIFY(upper_seek_point_index >= lower_seek_point_index);
  69. return m_seek_points[upper_seek_point_index].sample_index - m_seek_points[lower_seek_point_index].sample_index;
  70. }
  71. ErrorOr<void> SeekTable::insert_seek_point(SeekPoint seek_point)
  72. {
  73. if (auto previous_seek_point = seek_point_before(seek_point.sample_index); previous_seek_point.has_value() && previous_seek_point->sample_index == seek_point.sample_index) {
  74. // Do not insert a duplicate seek point.
  75. return {};
  76. }
  77. // FIXME: This could be even faster if we used binary search while finding the insertion point.
  78. return m_seek_points.try_insert_before_matching(seek_point, [&](auto const& other_seek_point) {
  79. return seek_point.sample_index < other_seek_point.sample_index;
  80. });
  81. }
  82. }