~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_dirstate_helpers_py.py

  • Committer: John Arbash Meinel
  • Date: 2007-07-12 16:34:02 UTC
  • mto: This revision was merged to the branch mainline in revision 2643.
  • Revision ID: john@arbash-meinel.com-20070712163402-lp91q157w5etslrj
Some restructuring.
Move bisect_path_* to private functions
Move cmp_path_by_dirblock to a private function,
since it is only used by the bisect_path functions.
Add tests that the compiled versions are actually used.
This catches cases when the import fails for the wrong reason.
Move some code around to make it closer to sorted by name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.dirstate import DirState
24
24
 
25
25
 
26
 
def bisect_path_left_py(paths, path):
 
26
def _bisect_path_left_py(paths, path):
27
27
    """Return the index where to insert path into paths.
28
28
 
29
29
    This uses the dirblock sorting. So all children in a directory come before
62
62
        mid = (lo + hi) // 2
63
63
        # Grab the dirname for the current dirblock
64
64
        cur = paths[mid]
65
 
        if cmp_path_by_dirblock_py(cur, path) < 0:
 
65
        if _cmp_path_by_dirblock_py(cur, path) < 0:
66
66
            lo = mid + 1
67
67
        else:
68
68
            hi = mid
69
69
    return lo
70
70
 
71
71
 
72
 
def bisect_path_right_py(paths, path):
 
72
def _bisect_path_right_py(paths, path):
73
73
    """Return the index where to insert path into paths.
74
74
 
75
75
    This uses a path-wise comparison so we get::
93
93
        mid = (lo+hi)//2
94
94
        # Grab the dirname for the current dirblock
95
95
        cur = paths[mid]
96
 
        if cmp_path_by_dirblock_py(path, cur) < 0:
 
96
        if _cmp_path_by_dirblock_py(path, cur) < 0:
97
97
            hi = mid
98
98
        else:
99
99
            lo = mid + 1
131
131
    return lo
132
132
 
133
133
 
 
134
def cmp_by_dirs_py(path1, path2):
 
135
    """Compare two paths directory by directory.
 
136
 
 
137
    This is equivalent to doing::
 
138
 
 
139
       cmp(path1.split('/'), path2.split('/'))
 
140
 
 
141
    The idea is that you should compare path components separately. This
 
142
    differs from plain ``cmp(path1, path2)`` for paths like ``'a-b'`` and
 
143
    ``a/b``. "a-b" comes after "a" but would come before "a/b" lexically.
 
144
 
 
145
    :param path1: first path
 
146
    :param path2: second path
 
147
    :return: positive number if ``path1`` comes first,
 
148
        0 if paths are equal,
 
149
        and negative number if ``path2`` sorts first
 
150
    """
 
151
    return cmp(path1.split('/'), path2.split('/'))
 
152
 
 
153
 
 
154
def _cmp_path_by_dirblock_py(path1, path2):
 
155
    """Compare two paths based on what directory they are in.
 
156
 
 
157
    This generates a sort order, such that all children of a directory are
 
158
    sorted together, and grandchildren are in the same order as the
 
159
    children appear. But all grandchildren come after all children.
 
160
 
 
161
    :param path1: first path
 
162
    :param path2: the second path
 
163
    :return: positive number if ``path1`` comes first,
 
164
        0 if paths are equal
 
165
        and a negative number if ``path2`` sorts first
 
166
    """
 
167
    dirname1, basename1 = os.path.split(path1)
 
168
    key1 = (dirname1.split('/'), basename1)
 
169
    dirname2, basename2 = os.path.split(path2)
 
170
    key2 = (dirname2.split('/'), basename2)
 
171
    return cmp(key1, key2)
 
172
 
 
173
 
134
174
def _read_dirblocks_py(state):
135
175
    """Read in the dirblocks for the given DirState object.
136
176
 
233
273
    # state._dirblocks = sorted(state._dirblocks)
234
274
    state._dirblock_state = DirState.IN_MEMORY_UNMODIFIED
235
275
 
236
 
 
237
 
def cmp_by_dirs_py(path1, path2):
238
 
    """Compare two paths directory by directory.
239
 
 
240
 
    This is equivalent to doing::
241
 
 
242
 
       cmp(path1.split('/'), path2.split('/'))
243
 
 
244
 
    The idea is that you should compare path components separately. This
245
 
    differs from plain ``cmp(path1, path2)`` for paths like ``'a-b'`` and
246
 
    ``a/b``. "a-b" comes after "a" but would come before "a/b" lexically.
247
 
 
248
 
    :param path1: first path
249
 
    :param path2: second path
250
 
    :return: positive number if ``path1`` comes first,
251
 
        0 if paths are equal,
252
 
        and negative number if ``path2`` sorts first
253
 
    """
254
 
    return cmp(path1.split('/'), path2.split('/'))
255
 
 
256
 
 
257
 
def cmp_path_by_dirblock_py(path1, path2):
258
 
    """Compare two paths based on what directory they are in.
259
 
 
260
 
    This generates a sort order, such that all children of a directory are
261
 
    sorted together, and grandchildren are in the same order as the
262
 
    children appear. But all grandchildren come after all children.
263
 
 
264
 
    :param path1: first path
265
 
    :param path2: the second path
266
 
    :return: positive number if ``path1`` comes first,
267
 
        0 if paths are equal
268
 
        and a negative number if ``path2`` sorts first
269
 
    """
270
 
    dirname1, basename1 = os.path.split(path1)
271
 
    key1 = (dirname1.split('/'), basename1)
272
 
    dirname2, basename2 = os.path.split(path2)
273
 
    key2 = (dirname2.split('/'), basename2)
274
 
    return cmp(key1, key2)