Blame src/xdiff/xhistogram.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) 2010, Google Inc.
Packit Service 20376f
 * and other copyright owners as documented in JGit's IP log.
Packit Service 20376f
 *
Packit Service 20376f
 * This program and the accompanying materials are made available
Packit Service 20376f
 * under the terms of the Eclipse Distribution License v1.0 which
Packit Service 20376f
 * accompanies this distribution, is reproduced below, and is
Packit Service 20376f
 * available at http://www.eclipse.org/org/documents/edl-v10.php
Packit Service 20376f
 *
Packit Service 20376f
 * All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * Redistribution and use in source and binary forms, with or
Packit Service 20376f
 * without modification, are permitted provided that the following
Packit Service 20376f
 * conditions are met:
Packit Service 20376f
 *
Packit Service 20376f
 * - Redistributions of source code must retain the above copyright
Packit Service 20376f
 *   notice, this list of conditions and the following disclaimer.
Packit Service 20376f
 *
Packit Service 20376f
 * - Redistributions in binary form must reproduce the above
Packit Service 20376f
 *   copyright notice, this list of conditions and the following
Packit Service 20376f
 *   disclaimer in the documentation and/or other materials provided
Packit Service 20376f
 *   with the distribution.
Packit Service 20376f
 *
Packit Service 20376f
 * - Neither the name of the Eclipse Foundation, Inc. nor the
Packit Service 20376f
 *   names of its contributors may be used to endorse or promote
Packit Service 20376f
 *   products derived from this software without specific prior
Packit Service 20376f
 *   written permission.
Packit Service 20376f
 *
Packit Service 20376f
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
Packit Service 20376f
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
Packit Service 20376f
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 20376f
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service 20376f
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit Service 20376f
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 20376f
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 20376f
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Packit Service 20376f
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit Service 20376f
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit Service 20376f
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit Service 20376f
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit Service 20376f
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "xinclude.h"
Packit Service 20376f
#include "xtypes.h"
Packit Service 20376f
#include "xdiff.h"
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
#define MAX_PTR	UINT_MAX
Packit Service 20376f
#define MAX_CNT	UINT_MAX
Packit Service 20376f
Packit Service 20376f
#define LINE_END(n) (line##n + count##n - 1)
Packit Service 20376f
#define LINE_END_PTR(n) (*line##n + *count##n - 1)
Packit Service 20376f
Packit Service 20376f
struct histindex {
Packit Service 20376f
	struct record {
Packit Service 20376f
		unsigned int ptr, cnt;
Packit Service 20376f
		struct record *next;
Packit Service 20376f
	} **records, /* an occurrence */
Packit Service 20376f
	  **line_map; /* map of line to record chain */
Packit Service 20376f
	chastore_t rcha;
Packit Service 20376f
	unsigned int *next_ptrs;
Packit Service 20376f
	unsigned int table_bits,
Packit Service 20376f
		     records_size,
Packit Service 20376f
		     line_map_size;
Packit Service 20376f
Packit Service 20376f
	unsigned int max_chain_length,
Packit Service 20376f
		     key_shift,
Packit Service 20376f
		     ptr_shift;
Packit Service 20376f
Packit Service 20376f
	unsigned int cnt,
Packit Service 20376f
		     has_common;
Packit Service 20376f
Packit Service 20376f
	xdfenv_t *env;
Packit Service 20376f
	xpparam_t const *xpp;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct region {
Packit Service 20376f
	unsigned int begin1, end1;
Packit Service 20376f
	unsigned int begin2, end2;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
#define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift])
Packit Service 20376f
Packit Service 20376f
#define NEXT_PTR(index, ptr) \
Packit Service 20376f
	(index->next_ptrs[(ptr) - index->ptr_shift])
Packit Service 20376f
Packit Service 20376f
#define CNT(index, ptr) \
Packit Service 20376f
	((LINE_MAP(index, ptr))->cnt)
Packit Service 20376f
Packit Service 20376f
#define REC(env, s, l) \
Packit Service 20376f
	(env->xdf##s.recs[l - 1])
Packit Service 20376f
Packit Service 20376f
static int cmp_recs(xpparam_t const *xpp,
Packit Service 20376f
	xrecord_t *r1, xrecord_t *r2)
Packit Service 20376f
{
Packit Service 20376f
	return r1->ha == r2->ha &&
Packit Service 20376f
		xdl_recmatch(r1->ptr, r1->size, r2->ptr, r2->size,
Packit Service 20376f
			    xpp->flags);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define CMP_ENV(xpp, env, s1, l1, s2, l2) \
Packit Service 20376f
	(cmp_recs(xpp, REC(env, s1, l1), REC(env, s2, l2)))
Packit Service 20376f
Packit Service 20376f
#define CMP(i, s1, l1, s2, l2) \
Packit Service 20376f
	(cmp_recs(i->xpp, REC(i->env, s1, l1), REC(i->env, s2, l2)))
Packit Service 20376f
Packit Service 20376f
#define TABLE_HASH(index, side, line) \
Packit Service 20376f
	XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits)
Packit Service 20376f
Packit Service 20376f
static int scanA(struct histindex *index, unsigned int line1, unsigned int count1)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int ptr;
Packit Service 20376f
	unsigned int tbl_idx;
Packit Service 20376f
	unsigned int chain_len;
Packit Service 20376f
	struct record **rec_chain, *rec;
Packit Service 20376f
Packit Service 20376f
	for (ptr = LINE_END(1); line1 <= ptr; ptr--) {
Packit Service 20376f
		tbl_idx = TABLE_HASH(index, 1, ptr);
Packit Service 20376f
		rec_chain = index->records + tbl_idx;
Packit Service 20376f
		rec = *rec_chain;
Packit Service 20376f
Packit Service 20376f
		chain_len = 0;
Packit Service 20376f
		while (rec) {
Packit Service 20376f
			if (CMP(index, 1, rec->ptr, 1, ptr)) {
Packit Service 20376f
				/*
Packit Service 20376f
				 * ptr is identical to another element. Insert
Packit Service 20376f
				 * it onto the front of the existing element
Packit Service 20376f
				 * chain.
Packit Service 20376f
				 */
Packit Service 20376f
				NEXT_PTR(index, ptr) = rec->ptr;
Packit Service 20376f
				rec->ptr = ptr;
Packit Service 20376f
				/* cap rec->cnt at MAX_CNT */
Packit Service 20376f
				rec->cnt = XDL_MIN(MAX_CNT, rec->cnt + 1);
Packit Service 20376f
				LINE_MAP(index, ptr) = rec;
Packit Service 20376f
				goto continue_scan;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			rec = rec->next;
Packit Service 20376f
			chain_len++;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (chain_len == index->max_chain_length)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * This is the first time we have ever seen this particular
Packit Service 20376f
		 * element in the sequence. Construct a new chain for it.
Packit Service 20376f
		 */
Packit Service 20376f
		if (!(rec = xdl_cha_alloc(&index->rcha)))
Packit Service 20376f
			return -1;
Packit Service 20376f
		rec->ptr = ptr;
Packit Service 20376f
		rec->cnt = 1;
Packit Service 20376f
		rec->next = *rec_chain;
Packit Service 20376f
		*rec_chain = rec;
Packit Service 20376f
		LINE_MAP(index, ptr) = rec;
Packit Service 20376f
Packit Service 20376f
continue_scan:
Packit Service 20376f
		; /* no op */
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int try_lcs(
Packit Service 20376f
	struct histindex *index, struct region *lcs, unsigned int b_ptr,
Packit Service 20376f
	unsigned int line1, unsigned int count1,
Packit Service 20376f
	unsigned int line2, unsigned int count2)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int b_next = b_ptr + 1;
Packit Service 20376f
	struct record *rec = index->records[TABLE_HASH(index, 2, b_ptr)];
Packit Service 20376f
	unsigned int as, ae, bs, be, np, rc;
Packit Service 20376f
	int should_break;
Packit Service 20376f
Packit Service 20376f
	for (; rec; rec = rec->next) {
Packit Service 20376f
		if (rec->cnt > index->cnt) {
Packit Service 20376f
			if (!index->has_common)
Packit Service 20376f
				index->has_common = CMP(index, 1, rec->ptr, 2, b_ptr);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		as = rec->ptr;
Packit Service 20376f
		if (!CMP(index, 1, as, 2, b_ptr))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		index->has_common = 1;
Packit Service 20376f
		for (;;) {
Packit Service 20376f
			should_break = 0;
Packit Service 20376f
			np = NEXT_PTR(index, as);
Packit Service 20376f
			bs = b_ptr;
Packit Service 20376f
			ae = as;
Packit Service 20376f
			be = bs;
Packit Service 20376f
			rc = rec->cnt;
Packit Service 20376f
Packit Service 20376f
			while (line1 < as && line2 < bs
Packit Service 20376f
				&& CMP(index, 1, as - 1, 2, bs - 1)) {
Packit Service 20376f
				as--;
Packit Service 20376f
				bs--;
Packit Service 20376f
				if (1 < rc)
Packit Service 20376f
					rc = XDL_MIN(rc, CNT(index, as));
Packit Service 20376f
			}
Packit Service 20376f
			while (ae < LINE_END(1) && be < LINE_END(2)
Packit Service 20376f
				&& CMP(index, 1, ae + 1, 2, be + 1)) {
Packit Service 20376f
				ae++;
Packit Service 20376f
				be++;
Packit Service 20376f
				if (1 < rc)
Packit Service 20376f
					rc = XDL_MIN(rc, CNT(index, ae));
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if (b_next <= be)
Packit Service 20376f
				b_next = be + 1;
Packit Service 20376f
			if (lcs->end1 - lcs->begin1 < ae - as || rc < index->cnt) {
Packit Service 20376f
				lcs->begin1 = as;
Packit Service 20376f
				lcs->begin2 = bs;
Packit Service 20376f
				lcs->end1 = ae;
Packit Service 20376f
				lcs->end2 = be;
Packit Service 20376f
				index->cnt = rc;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if (np == 0)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			while (np <= ae) {
Packit Service 20376f
				np = NEXT_PTR(index, np);
Packit Service 20376f
				if (np == 0) {
Packit Service 20376f
					should_break = 1;
Packit Service 20376f
					break;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if (should_break)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			as = np;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	return b_next;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int find_lcs(
Packit Service 20376f
	struct histindex *index, struct region *lcs,
Packit Service 20376f
	unsigned int line1, unsigned int count1,
Packit Service 20376f
	unsigned int line2, unsigned int count2)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int b_ptr;
Packit Service 20376f
Packit Service 20376f
	if (scanA(index, line1, count1))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	index->cnt = index->max_chain_length + 1;
Packit Service 20376f
Packit Service 20376f
	for (b_ptr = line2; b_ptr <= LINE_END(2); )
Packit Service 20376f
		b_ptr = try_lcs(index, lcs, b_ptr, line1, count1, line2, count2);
Packit Service 20376f
Packit Service 20376f
	return index->has_common && index->max_chain_length < index->cnt;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int fall_back_to_classic_diff(struct histindex *index,
Packit Service 20376f
		int line1, int count1, int line2, int count2)
Packit Service 20376f
{
Packit Service 20376f
	xpparam_t xpp;
Packit Service 20376f
	xpp.flags = index->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
Packit Service 20376f
Packit Service 20376f
	return xdl_fall_back_diff(index->env, &xpp,
Packit Service 20376f
				  line1, count1, line2, count2);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int histogram_diff(
Packit Service 20376f
	xpparam_t const *xpp, xdfenv_t *env,
Packit Service 20376f
	unsigned int line1, unsigned int count1,
Packit Service 20376f
	unsigned int line2, unsigned int count2)
Packit Service 20376f
{
Packit Service 20376f
	struct histindex index;
Packit Service 20376f
	struct region lcs;
Packit Service 20376f
	size_t sz;
Packit Service 20376f
	int result = -1;
Packit Service 20376f
Packit Service 20376f
	if (count1 <= 0 && count2 <= 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (LINE_END(1) >= MAX_PTR)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!count1) {
Packit Service 20376f
		while(count2--)
Packit Service 20376f
			env->xdf2.rchg[line2++ - 1] = 1;
Packit Service 20376f
		return 0;
Packit Service 20376f
	} else if (!count2) {
Packit Service 20376f
		while(count1--)
Packit Service 20376f
			env->xdf1.rchg[line1++ - 1] = 1;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	memset(&index, 0, sizeof(index));
Packit Service 20376f
Packit Service 20376f
	index.env = env;
Packit Service 20376f
	index.xpp = xpp;
Packit Service 20376f
Packit Service 20376f
	index.records = NULL;
Packit Service 20376f
	index.line_map = NULL;
Packit Service 20376f
	/* in case of early xdl_cha_free() */
Packit Service 20376f
	index.rcha.head = NULL;
Packit Service 20376f
Packit Service 20376f
	index.table_bits = xdl_hashbits(count1);
Packit Service 20376f
	sz = index.records_size = 1 << index.table_bits;
Packit Service 20376f
	GITERR_CHECK_ALLOC_MULTIPLY(&sz, sz, sizeof(struct record *));
Packit Service 20376f
Packit Service 20376f
	if (!(index.records = (struct record **) xdl_malloc(sz)))
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	memset(index.records, 0, sz);
Packit Service 20376f
Packit Service 20376f
	sz = index.line_map_size = count1;
Packit Service 20376f
	sz *= sizeof(struct record *);
Packit Service 20376f
	if (!(index.line_map = (struct record **) xdl_malloc(sz)))
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	memset(index.line_map, 0, sz);
Packit Service 20376f
Packit Service 20376f
	sz = index.line_map_size;
Packit Service 20376f
	sz *= sizeof(unsigned int);
Packit Service 20376f
	if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz)))
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	memset(index.next_ptrs, 0, sz);
Packit Service 20376f
Packit Service 20376f
	/* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
Packit Service 20376f
	if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	index.ptr_shift = line1;
Packit Service 20376f
	index.max_chain_length = 64;
Packit Service 20376f
Packit Service 20376f
	memset(&lcs, 0, sizeof(lcs));
Packit Service 20376f
	if (find_lcs(&index, &lcs, line1, count1, line2, count2))
Packit Service 20376f
		result = fall_back_to_classic_diff(&index, line1, count1, line2, count2);
Packit Service 20376f
	else {
Packit Service 20376f
		if (lcs.begin1 == 0 && lcs.begin2 == 0) {
Packit Service 20376f
			while (count1--)
Packit Service 20376f
				env->xdf1.rchg[line1++ - 1] = 1;
Packit Service 20376f
			while (count2--)
Packit Service 20376f
				env->xdf2.rchg[line2++ - 1] = 1;
Packit Service 20376f
			result = 0;
Packit Service 20376f
		} else {
Packit Service 20376f
			result = histogram_diff(xpp, env,
Packit Service 20376f
						line1, lcs.begin1 - line1,
Packit Service 20376f
						line2, lcs.begin2 - line2);
Packit Service 20376f
			if (result)
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
			result = histogram_diff(xpp, env,
Packit Service 20376f
						lcs.end1 + 1, LINE_END(1) - lcs.end1,
Packit Service 20376f
						lcs.end2 + 1, LINE_END(2) - lcs.end2);
Packit Service 20376f
			if (result)
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	xdl_free(index.records);
Packit Service 20376f
	xdl_free(index.line_map);
Packit Service 20376f
	xdl_free(index.next_ptrs);
Packit Service 20376f
	xdl_cha_free(&index.rcha);
Packit Service 20376f
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2,
Packit Service 20376f
	xpparam_t const *xpp, xdfenv_t *env)
Packit Service 20376f
{
Packit Service 20376f
	if (xdl_prepare_env(file1, file2, xpp, env) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return histogram_diff(xpp, env,
Packit Service 20376f
		env->xdf1.dstart + 1, env->xdf1.dend - env->xdf1.dstart + 1,
Packit Service 20376f
		env->xdf2.dstart + 1, env->xdf2.dend - env->xdf2.dstart + 1);
Packit Service 20376f
}