Success build TortoiseMerge.
[TortoiseGit.git] / src / TortoiseMerge / libsvn_diff / token.c
blob07ab5d16653337df650f0ec53904808cc1051a97
1 /*
2 * token.c : routines for doing diffs
4 * ====================================================================
5 * Copyright (c) 2000-2004 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
20 #include <apr.h>
21 #include <apr_pools.h>
22 #include <apr_general.h>
23 #include "svn_error.h"
24 #include "svn_version.h"
25 #include "svn_io.h"
27 #include "diff.h"
31 * Prime number to use as the size of the hash table. This number was
32 * not selected by testing of any kind and may need tweaking.
34 #define SVN_DIFF__HASH_SIZE 127
36 struct svn_diff__node_t
38 svn_diff__node_t *parent;
39 svn_diff__node_t *left;
40 svn_diff__node_t *right;
42 apr_uint32_t hash;
43 void *token;
46 struct svn_diff__tree_t
48 svn_diff__node_t *root[SVN_DIFF__HASH_SIZE];
49 apr_pool_t *pool;
54 * Support functions to build a tree of token positions
57 void
58 svn_diff__tree_create(svn_diff__tree_t **tree, apr_pool_t *pool)
60 *tree = apr_pcalloc(pool, sizeof(**tree));
61 (*tree)->pool = pool;
65 static svn_error_t *
66 svn_diff__tree_insert_token(svn_diff__node_t **node, svn_diff__tree_t *tree,
67 void *diff_baton,
68 const svn_diff_fns_t *vtable,
69 apr_uint32_t hash, void *token)
71 svn_diff__node_t *new_node;
72 svn_diff__node_t **node_ref;
73 svn_diff__node_t *parent;
74 int rv;
76 SVN_ERR_ASSERT(token);
78 parent = NULL;
79 node_ref = &tree->root[hash % SVN_DIFF__HASH_SIZE];
81 while (*node_ref != NULL)
83 parent = *node_ref;
85 rv = hash - parent->hash;
86 if (!rv)
87 SVN_ERR(vtable->token_compare(diff_baton, parent->token, token, &rv));
89 if (rv == 0)
91 /* Discard the previous token. This helps in cases where
92 * only recently read tokens are still in memory.
94 if (vtable->token_discard != NULL)
95 vtable->token_discard(diff_baton, parent->token);
97 parent->token = token;
98 *node = parent;
100 return SVN_NO_ERROR;
102 else if (rv > 0)
104 node_ref = &parent->left;
106 else
108 node_ref = &parent->right;
112 /* Create a new node */
113 new_node = apr_palloc(tree->pool, sizeof(*new_node));
114 new_node->parent = parent;
115 new_node->left = NULL;
116 new_node->right = NULL;
117 new_node->hash = hash;
118 new_node->token = token;
120 *node = *node_ref = new_node;
122 return SVN_NO_ERROR;
127 * Get all tokens from a datasource. Return the
128 * last item in the (circular) list.
130 svn_error_t *
131 svn_diff__get_tokens(svn_diff__position_t **position_list,
132 svn_diff__tree_t *tree,
133 void *diff_baton,
134 const svn_diff_fns_t *vtable,
135 svn_diff_datasource_e datasource,
136 apr_pool_t *pool)
138 svn_diff__position_t *start_position;
139 svn_diff__position_t *position = NULL;
140 svn_diff__position_t **position_ref;
141 svn_diff__node_t *node;
142 void *token;
143 apr_off_t offset;
144 apr_uint32_t hash;
146 *position_list = NULL;
149 SVN_ERR(vtable->datasource_open(diff_baton, datasource));
151 position_ref = &start_position;
152 offset = 0;
153 hash = 0; /* The callback fn doesn't need to touch it per se */
154 while (1)
156 SVN_ERR(vtable->datasource_get_next_token(&hash, &token,
157 diff_baton, datasource));
158 if (token == NULL)
159 break;
161 offset++;
162 SVN_ERR(svn_diff__tree_insert_token(&node, tree,
163 diff_baton, vtable,
164 hash, token));
166 /* Create a new position */
167 position = apr_palloc(pool, sizeof(*position));
168 position->next = NULL;
169 position->node = node;
170 position->offset = offset;
172 *position_ref = position;
173 position_ref = &position->next;
176 *position_ref = start_position;
178 SVN_ERR(vtable->datasource_close(diff_baton, datasource));
180 *position_list = position;
182 return SVN_NO_ERROR;