PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/historical/mergetools.py

https://bitbucket.org/lindenlab/apiary/
Python | 97 lines | 41 code | 13 blank | 43 comment | 7 complexity | e1e70932858200d2dd4a695b34302b0f MD5 | raw file
  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. """mergetools - Merge together sorted sequences
  26. Functions:
  27. imerge - Return an iterator that produces the merge of the arguments
  28. merge - Return an array that is the merge of the arguments
  29. In both cases, the arguments can be zero or more iterable objects. Collections,
  30. iterators, and any object implementing the iterator protocol are acceptable.
  31. If the sequences being merged are not already in sorted order, then the result
  32. order is not well defined.
  33. Items in the sequences are compared using the < operator. Duplicate items are
  34. allowed, and will be returned as many times as they appear. If duplicate items
  35. appear in more than one of the input sequences, then they will appear in the
  36. output in the order of the input sequences.
  37. """
  38. __all__ = [
  39. 'imerge',
  40. 'merge',
  41. ]
  42. class _Feed(object):
  43. def __init__(self, iterable):
  44. self._iteration = iter(iterable)
  45. self._head = None
  46. self.advance()
  47. def empty(self):
  48. return self._iteration is None
  49. def head(self):
  50. return self._head
  51. def advance(self):
  52. try:
  53. self._head = self._iteration.next()
  54. except StopIteration:
  55. self._iteration = None
  56. self._head = None
  57. class _IMerge(object):
  58. def __init__(self, iterables):
  59. self._feeds = map(_Feed, iterables)
  60. def __iter__(self):
  61. return self
  62. def next(self):
  63. source = None
  64. smallest = None
  65. for f in self._feeds:
  66. if f.empty():
  67. continue
  68. if source is None or f.head() < smallest:
  69. source = f
  70. smallest = f.head()
  71. if source is None:
  72. raise StopIteration
  73. source.advance()
  74. return smallest
  75. def imerge(*iterables):
  76. """Return an iterator that produces the merge of the arguments"""
  77. return _IMerge(iterables)
  78. def merge(*iterables):
  79. """Return an array that is the merge of the arguments"""
  80. return map(None, _IMerge(iterables))