Skip to content

tgvishnu/Vishnu.Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vishnu.Extensions

This package contains

Algorithms for (NuGet: Vishnu.Algorithm.Extensions )

  • Sorting contents.
  • Pattern searching in strings

Dotnet extensions for (NuGet:)

  • Object
  • String
  • DateTime
  • Long
  • Array

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Algorithms [ NuGet: Vishnu.Algorithm.Extensions ]

1. Sorting


Description

    
  // Perform sorting using algorithms defined in DefaultAlgorithm factory
  Algorithm.Sorting.Use{ALGORITHM_NAME}<TYPE>(<TYPE> input, IComparer<TYPE>); 
  
  // Perform sorting using algorithms defined in Custom algorithm factory
  Algorithm.Sorting.Use{ALGORITHM_NAME}<TYPE>(ISortingAlgorithmFactory sortingAlgorithmFactory, <TYPE> input,  IComparer<TYPE>);        
  
  // Peform sorting on strings using algorithms with default IComparer<char>
  string sortedString = Algorithm.Sorting.Use{ALGORITHM_NAME}(string input);
  
  // Perform sorting on string using algorithm with custom IComparer<char>
  string sortedString = Algorithm.Sorting.Use{ALGORITHM_NAME}(string input, {IComparer<char>});
 

Usage

     // sorting array of integers
     int[] data = new int[] { 5,3,8,5,1,0,8 };
     Algorithm.Sorting.UseCocktail(data, new IntegerComparer());
     
     // sorting array of string based on the length
     string[] data = new string[] { "hello", null, "i", "am", null, "good", string.Empty, " "};
     Algorithm.Sorting.UseCocktail( data, new StringLengthComparer());
     
     // sorting date time
     DateTime[] data = new DateTime[] { DateTime.Now.AddDays(3), DateTime.Now.AddSeconds(10), DateTime.Now.AddSeconds(-100), DateTime.Now.AddDays(1) };
     Algorithm.Sorting.UseCocktail(data, new DateTimeComparer());
     
     // sorting class based on the custom comparer
     List<Person> people = new List<Person>();
     Algorithm.Sorting.UseCocktail(data, new PersonAgeComparer());    
    

Available IComparer

  • AsciiValueComparer
  • DateTimeComparer
  • IntegerComparer
  • StringLengthComparer

Custom comparer must implement IComparer interface

IComparer must always

  • return 1 if first element > second element
  • return -1 if first element < second element
  • return 0 if first element == second element
    public class PersonAgeComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            if (x.Age > y.Age)
                return 1;
            if (x.Age < y.Age)
                return -1;
            return 0;
        }
    }

Supported algorithms

  • ** Algorithm.Sorting.UseCocktail**
  • ** Algorithm.Sorting.UseBitonic**
  • ** Algorithm.Sorting.UseBubble**
  • ** Algorithm.Sorting.UseComb**
  • ** Algorithm.Sorting.UseCounting**
  • ** Algorithm.Sorting.UseHeap**
  • ** Algorithm.Sorting.UseInsertion**
  • ** Algorithm.Sorting.UseMerge**
  • ** Algorithm.Sorting.UsePancake**
  • ** Algorithm.Sorting.UseQuickLastPivot**
  • ** Algorithm.Sorting.UseRadix**
  • ** Algorithm.Sorting.UseSelection**
  • ** Algorithm.Sorting.UseShell**
  • ** Algorithm.Sorting.UseTim**

Extension for adding new Sorting algorithms

New algorithms can be added by extending ** ISorting ** interface using extension methods

  namespace Vishnu.Extensions.Sorting
  {
      public static class FooAlgorithmSortExtension
      {
          public static void UseFoo<T>(this ISorting sort, T[] input, IComparer<T> comparer)
          {
              // Foo algorithm logic
          }
      }
  }
    

2. Pattern Searching


Description

  // Returns list of position of the pattern found in the text based on the algorithm used.    
  IList<int> positions = Algorithm.PatternSearch.Use{ALGORITHNM_NAME} (string text, string pattern)
   

Usage

     var text = "AABAACAADAABAAABAA";
     var pattern = "AABA";            
     var result = Algorithm.PatternSearch.UseNaive(text, pattern);  
     
     // result = { 0, 9, 13 }
     

Supported algorithms

  • ** Algorithm.PatternSearch.UseAnagram**
  • ** Algorithm.PatternSearch.UseNaive**
  • ** Algorithm.PatternSearch.UseKmp**
  • ** Algorithm.PatternSearch.UseRabinKrap**
  • ** Algorithm.PatternSearch.UseFinateAutomata**
  • ** Algorithm.PatternSearch.UseEfficientFinateAutomata**
  • ** Algorithm.PatternSearch.UseBitap**
  • ** Algorithm.PatternSearch.UseZ**

Extension for adding new Pattern searching algorithms

New algorithms can be added by extending ** IPattern ** interface using extension methods

  namespace Vishnu.Extensions.Pattern
  {
      public static class FooAlgorithmPatternExtension
      {
          public static IList<int> UseFoo(this IPattern patternSearch, string text, string pattern)
          {
              // Foo algorithm logic
          }
      }
  }

References

  1. https://www.geeksforgeeks.org
  2. https://www.programmingalgorithms.com

Dotnet Extensions [ NuGet: ]

1. String

namespace : using Vishnu.Extensions.StringType

  • string result = "hello".GetMd5Hash();
  • bool result = "one".ToTryEnum(true, out numberEnum); // ignore case
  • string result = "Hi {0} --> {1}".GetInFormat("dude", "how are you");
  • bool result = "One".In(true, "one", "two", "three");
  • bool result = "on,e".In(',','.');
  • bool result = "One".In(true, new List() { "one", "two", "three" });
  • bool result = "on>e".In(new List { ',', '.' });
  • bool result = "one".AppearInAll("one", "two one dsafa", "three one dafasf");
  • bool result = "one".AppearInAll(new List() { "one", "two one dsafa", "three one dafasf" });
  • bool result = "one".AppearInAny("", "two dsafa", "three one dafasf");
  • bool result = "one".AppearInAny(new List() { "", "two dsafa", "three one dafasf" });
  • string resString = "ab23-432-5dfg".GetDigits();
  • string result = "hello".RemoveLast(2)
  • string result = "hello".RemoveLastCharacter(2)
  • string result = "hello".RemoveFirst(2)
  • string result = "hello".RemoveFirstCharacter(2)
  • IList parts = "asdfaf asd gagasfda a ".SplitOnSize(3);
  • IList parts = "asdfaf asd gagasfda a ".SplitOnSize(new List() { 3, 5, 3 });
  • bool result = "earljon".IsPalindrome();

2. Object

namespace : using Vishnu.Extensions.ObjectType

  • this.SetLogLevel("All");
  • this.LogBegin(() => "Hellow world");
  • this.LogEnd(() => "Hellow world");
  • this.LogDebug(() => "Hellow world");
  • this.LogError(() => "Hellow world");
  • this.LogExeception(new System.Exception("Hellow world"));
  • this.LogFunctional(() => "Hellow world");
  • this.LogInformation(() => "Hellow world");
  • this.LogWarning(() => "Hellow world");

3. Long

namespace : using Vishnu.Extensions.LongType

  • string size = 342113412; var result = l.ToFileSize();

4. DateTime

namespace : using Vishnu.Extensions.DateTimeType

  • bool result = DateTime.Now.Between(DateTime.Now, DateTime.Now.AddDays(2));
  • int age = new DateTime(1983, 06, 10).Age();

4. Array

namespace : using Vishnu.Extensions.ArrayType

  var input = new byte[] { 0x0D, 0x0A, 0x7e, 0x7d };
  var stuffedContent = input.PPPByteStuff();
  var originalData = stuffedContent.PPPByteDeStuff();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages