Ejemplo n.º 1
0
/*
 * A generic algorithm - utilizes generic types and thereby allows
 * a single, value type independent implemenetation.
 *
 * Generic methods are implemented purely in terms of generic types and
 * generic type independent instance types, such as int and error. All
 * generic type instance dependent operations are externalized.
 *
 * Generic functions are not supported because the receiver type cannot be
 * an interface.
 */
func Accumulate_R(rcv R, r Q) Q {
	vals := generic.ValueOf(r)
	for i := 0; i < vals.Len(); i++ {
		val := vals.Index(i).Interface()
		rcv.Add_R(val)
	}
	return rcv.Count_R()
}
Ejemplo n.º 2
0
func Contains_L(list L, val V) bool {
	ks := generic.ValueOf(list.Keys_L())
	for i := 0; i < ks.Len(); i++ {
		key := ks.Index(i).Interface()
		data := list.Get_L(key)
		if val == data {
			return true
		}
	}
	return false
}
Ejemplo n.º 3
0
func RemoveByV_L(list L, v V) {
	keys := generic.MakeSlice(list.TypeK_L()).Interface()
	vs := generic.ValueOf(v)
	for i := 0; i < vs.Len(); i++ {
		val := vs.Index(i).Interface()
		key := KeyOf_L(list, val)
		if key != nil {
			keys = generic.Append(keys, key).Interface()
		}
	}
	list.Remove_L(keys)
}
Ejemplo n.º 4
0
func KeyOf_L(list L, val V) K {
	ks := generic.ValueOf(list.Keys_L())
	for i := 0; i < ks.Len(); i++ {
		key := ks.Index(i).Interface()
		data := list.Get_L(key)
		if val == data {
			return key
		}
	}

	// need to think about this
	switch generic.TypeOf(list.TypeK_L()).Kind() {
	case reflect.Int, reflect.Int64:
		return -1
	case reflect.String:
		return ""
	default:
		return nil
	}
}
Ejemplo n.º 5
0
func Len_L(list L) int {
	return generic.ValueOf(list.Values_L()).Len()
}