github.com/synesissoftware/ANGoLS@v0.0.0-20190330004400-955d82dbf73b/equal_slice.go (about)

     1  /* /////////////////////////////////////////////////////////////////////////
     2   * File:        equal_slice.go
     3   *
     4   * Purpose:     EqualSlice*() functions
     5   *
     6   * Created:     1st March 2019
     7   * Updated:     11th March 2019
     8   *
     9   * Home:        http://github.com/synesissoftware/ANGOLS
    10   *
    11   * Copyright (c) 2019, Matthew Wilson and Synesis Software
    12   * All rights reserved.
    13   *
    14   * Redistribution and use in source and binary forms, with or without
    15   * modification, are permitted provided that the following conditions are
    16   * met:
    17   *
    18   * - Redistributions of source code must retain the above copyright notice,
    19   *   this list of conditions and the following disclaimer.
    20   * - Redistributions in binary form must reproduce the above copyright
    21   *   notice, this list of conditions and the following disclaimer in the
    22   *   documentation and/or other materials provided with the distribution.
    23   * - Neither the names of Matthew Wilson, Synesis Software nor
    24   *   the names of any contributors may be used to endorse or promote products
    25   *   derived from this software without specific prior written permission.
    26   *
    27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    28   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    29   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    30   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    31   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    32   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    33   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    34   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    35   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    36   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    37   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    38   *
    39   * ////////////////////////////////////////////////////////////////////// */
    40  
    41  package angols
    42  
    43  import (
    44  
    45  	"reflect"
    46  )
    47  
    48  // /////////////////////////////////////////////////////////////////////////
    49  // EqualSlice*()
    50  
    51  func EqualSliceOfInt(lhs, rhs []int) bool {
    52  
    53  	len_l	:=	len(lhs)
    54  	len_r	:=	len(rhs)
    55  
    56  	if len_l != len_r {
    57  
    58  		return false
    59  	} else {
    60  
    61  		for i := 0; len_l != i; i++ {
    62  
    63  			if lhs[i] != rhs[i] {
    64  
    65  				return false
    66  			}
    67  		}
    68  
    69  		return true
    70  	}
    71  }
    72  
    73  func EqualSliceOfUInt(lhs, rhs []uint) bool {
    74  
    75  	len_l	:=	len(lhs)
    76  	len_r	:=	len(rhs)
    77  
    78  	if len_l != len_r {
    79  
    80  		return false
    81  	} else {
    82  
    83  		for i := 0; len_l != i; i++ {
    84  
    85  			if lhs[i] != rhs[i] {
    86  
    87  				return false
    88  			}
    89  		}
    90  
    91  		return true
    92  	}
    93  }
    94  
    95  func EqualSliceOfFloat64(lhs, rhs []float64) bool {
    96  
    97  	len_l	:=	len(lhs)
    98  	len_r	:=	len(rhs)
    99  
   100  	if len_l != len_r {
   101  
   102  		return false
   103  	} else {
   104  
   105  		for i := 0; len_l != i; i++ {
   106  
   107  			if lhs[i] != rhs[i] {
   108  
   109  				return false
   110  			}
   111  		}
   112  
   113  		return true
   114  	}
   115  }
   116  
   117  func EqualSliceOfString(lhs, rhs []string) bool {
   118  
   119  	len_l	:=	len(lhs)
   120  	len_r	:=	len(rhs)
   121  
   122  	if len_l != len_r {
   123  
   124  		return false
   125  	} else {
   126  
   127  		for i := 0; len_l != i; i++ {
   128  
   129  			if lhs[i] != rhs[i] {
   130  
   131  				return false
   132  			}
   133  		}
   134  
   135  		return true
   136  	}
   137  }
   138  
   139  func EqualSlice(lhs, rhs interface{}) bool {
   140  
   141  	// Check for typed variants
   142  
   143  	switch lhs_typed := lhs.(type) {
   144  
   145  	case []int:
   146  
   147  		if rhs_typed, ok := rhs.([]int); ok {
   148  
   149  			return EqualSliceOfInt(lhs_typed, rhs_typed)
   150  		} else {
   151  
   152  			return false
   153  		}
   154  	case []float64:
   155  
   156  		if rhs_typed, ok := rhs.([]float64); ok {
   157  
   158  			return EqualSliceOfFloat64(lhs_typed, rhs_typed)
   159  		} else {
   160  
   161  			return false
   162  		}
   163  	case []string:
   164  
   165  		if rhs_typed, ok := rhs.([]string); ok {
   166  
   167  			return EqualSliceOfString(lhs_typed, rhs_typed)
   168  		} else {
   169  
   170  			return false
   171  		}
   172  	}
   173  
   174  	// Generic comparison
   175  
   176  	lhs_t	:=	reflect.TypeOf(lhs)
   177  	rhs_t	:=	reflect.TypeOf(rhs)
   178  
   179  	// check both are slices
   180  
   181  	if reflect.Slice != lhs_t.Kind() {
   182  
   183  		return false
   184  	}
   185  
   186  	if reflect.Slice != rhs_t.Kind() {
   187  
   188  		return false
   189  	}
   190  
   191  	// check element type
   192  
   193  	lhs_k	:=	lhs_t.Elem()
   194  	rhs_k	:=	rhs_t.Elem()
   195  
   196  	if lhs_k != rhs_k {
   197  
   198  		return false
   199  	} else {
   200  
   201  		lhs_v	:=	reflect.ValueOf(lhs)
   202  		rhs_v	:=	reflect.ValueOf(rhs)
   203  
   204  		lhs_n	:=	lhs_v.Len()
   205  		rhs_n	:=	rhs_v.Len()
   206  
   207  		if lhs_n != rhs_n {
   208  
   209  			return false
   210  		}
   211  
   212  		for i := 0; lhs_n != i; i++ {
   213  
   214  			if lhs_v.Index(i).Interface() != rhs_v.Index(i).Interface() {
   215  
   216  				return false
   217  			}
   218  		}
   219  
   220  		return true
   221  	}
   222  }
   223  
   224  /* ///////////////////////////// end of file //////////////////////////// */
   225  
   226