RandomAccessTraversal.rst 6.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. .. Copyright David Abrahams 2006. Distributed under the Boost
  2. .. Software License, Version 1.0. (See accompanying
  3. .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. Random Access Traversal Concept
  5. ...............................
  6. A class or built-in type ``X`` models the *Random Access Traversal*
  7. concept if the following expressions are valid and respect the stated
  8. semantics. In the table below, ``Distance`` is
  9. ``iterator_traits<X>::difference_type`` and ``n`` represents a
  10. constant object of type ``Distance``.
  11. +------------------------------------------------------------------------------------------------------------------+
  12. |Random Access Traversal Iterator Requirements (in addition to Bidirectional Traversal) |
  13. +-------------------------------+---------------------------------+-------------------------+----------------------+
  14. |Expression |Return Type |Operational Semantics |Assertion/ |
  15. | | | |Precondition |
  16. +===============================+=================================+=========================+======================+
  17. |``r += n`` |``X&`` |:: | |
  18. | | | | |
  19. | | | { | |
  20. | | | Distance m = n; | |
  21. | | | if (m >= 0) | |
  22. | | | while (m--) | |
  23. | | | ++r; | |
  24. | | | else | |
  25. | | | while (m++) | |
  26. | | | --r; | |
  27. | | | return r; | |
  28. | | | } | |
  29. +-------------------------------+---------------------------------+-------------------------+----------------------+
  30. |``a + n``, ``n + a`` |``X`` |``{ X tmp = a; return tmp| |
  31. | | |+= n; }`` | |
  32. | | | | |
  33. +-------------------------------+---------------------------------+-------------------------+----------------------+
  34. |``r -= n`` |``X&`` |``return r += -n`` | |
  35. +-------------------------------+---------------------------------+-------------------------+----------------------+
  36. |``a - n`` |``X`` |``{ X tmp = a; return tmp| |
  37. | | |-= n; }`` | |
  38. | | | | |
  39. +-------------------------------+---------------------------------+-------------------------+----------------------+
  40. |``b - a`` |``Distance`` |``a < b ? distance(a,b) |pre: there exists a |
  41. | | |: -distance(b,a)`` |value ``n`` of |
  42. | | | |``Distance`` such that|
  43. | | | |``a + n == b``. ``b |
  44. | | | |== a + (b - a)``. |
  45. +-------------------------------+---------------------------------+-------------------------+----------------------+
  46. |``a[n]`` |convertible to T |``*(a + n)`` |pre: a is a *Readable |
  47. | | | |Iterator* |
  48. +-------------------------------+---------------------------------+-------------------------+----------------------+
  49. |``a[n] = v`` |convertible to T |``*(a + n) = v`` |pre: a is a *Writable |
  50. | | | |iterator* |
  51. +-------------------------------+---------------------------------+-------------------------+----------------------+
  52. |``a < b`` |convertible to ``bool`` |``b - a > 0`` |``<`` is a total |
  53. | | | |ordering relation |
  54. +-------------------------------+---------------------------------+-------------------------+----------------------+
  55. |``a > b`` |convertible to ``bool`` |``b < a`` |``>`` is a total |
  56. | | | |ordering relation |
  57. +-------------------------------+---------------------------------+-------------------------+----------------------+
  58. |``a >= b`` |convertible to ``bool`` |``!(a < b)`` | |
  59. +-------------------------------+---------------------------------+-------------------------+----------------------+
  60. |``a <= b`` |convertible to ``bool`` |``!(a > b)`` | |
  61. +-------------------------------+---------------------------------+-------------------------+----------------------+
  62. |``iterator_traversal<X>::type``|Convertible to | | |
  63. | |``random_access_traversal_tag`` | | |
  64. +-------------------------------+---------------------------------+-------------------------+----------------------+