GithubHelp home page GithubHelp logo

eugene1982 / easyhash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iarovyi/easyhash

0.0 1.0 0.0 111 KB

Easy way to autogenerate GetHashCode function

License: MIT License

Batchfile 0.06% C# 92.39% PowerShell 7.55%

easyhash's Introduction

EasyHash

Build status Join the chat at https://gitter.im/knockout/knockout NuGet

Easy way to autogenerate GetHashCode and Equals functions. Very often we have to write straightforward repeating and easy code that can be autogenerated and as result it eliminate errors introduced during manual typing.

EasyHash generate GetHashCode function with performance close to classic implementation because it doesn't use reflection. Don't use EasyHash if performance is your primary goal because there is a slight decrease of performance and if not then it will prevent you from making mistakes.

Benchmark

Code like this:

public sealed class HashedManually
{
    ...

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 2166136261;
            hash = hash * 16780669 ^ (field1 == null ? 0 : field1.GetHashCode());
            hash = hash * 16780669 ^ (field2 == 0 ? 0 : field2.GetHashCode());
            hash = hash * 16780669 ^ (field3 == null ? 0 : field3.GetHashCode());
            hash = hash * 16780669 ^ (field4 == 0 ? 0 : field4.GetHashCode());
            hash = hash * 16780669 ^ (field5 == null ? 0 : field5.GetHashCode());
            hash = hash * 16780669 ^ (field6 == 0 ? 0 : field6.GetHashCode());
            hash = hash * 16780669 ^ (field7 == null ? 0 : field7.GetHashCode());
            var col1Hash = 486187739;
            if (col1 != null){
                for(var item in col1) {
                    col1Hash = col1Hash * 486190561 ^ (item == null ? 0 : item.GetHashCode());
                }
            }
            hash = hash * 16780669 ^ col1Hash;
            return hash;
        }
    }

    private bool Equals(HashedManually other)
    {
        return field1 == other.field1
        && field2 == other.field2
        && string.Equals(field3, other.field3)
        && field4 == other.field4
        && field5 == other.field5
        && field6 == other.field6
        && field7 == other.field7;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        return obj is HashedManually && Equals((HashedManually) obj);
    }
}

can be replaced with just few lines of code:

public sealed class HashedWithEasyHash
{
    ...

    public override int GetHashCode() => EasyHash<HashedWithEasyHash>.GetHashCode(this);
    public override bool Equals(object obj) => EasyHash<HashedWithEasyHash>.Equals(this, obj);
}

Hashing can be customized with fluent syntaxis:

public sealed class HashedWithComplexRegistration
{
	...

	static HashedWithComplexRegistration()
	{
		EasyHash<HashedWithComplexRegistration>
			.Register(r => r
				.WithPrimes(17, 23)
				.Skip(f => f.Number)
				.For(f => f.Numbers, (ob, hash) => ob.Numbers.OrderBy(x=>x).Aggregate(hash, (i, i1) => (i * 23) ^ i1.GetHashCode()))
				.For(f => f.Text, (ob, hash) => (hash * 23) ^ ob.Text.GetHashCode())
				.ExcludeCollectionItems());
	}

	public override int GetHashCode() => EasyHash<HashedWithComplexRegistration>.GetHashCode(this);
	public override bool Equals(object obj) => EasyHash<HashedWithComplexRegistration>.Equals(this, obj);
}

Build Script

From a powershell prompt

Command Description
build.ps1 Builds the entire solution and run tests.

From a bash prompt

Command Description
build.bat Builds the entire solution and run tests.

Build targets

Target Description
Build Build solution with semantic version and run tests.
Package Create nuget package based on semantic version.
Publish Publish nuget package to nuget.org. (require ApiKey present as environment variable)

Example: .\build.ps1 -target Package

Contributors

Please read CONTRIBUTING.md

easyhash's People

Contributors

iarovyi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.