// MapOverRows passes each row map into a function.
// First argument is a list of AttributeSpec in the order
// they're needed in for the function. The second is the function
// to call on each row.
553015
func (inst *DenseInstances) MapOverRows(asv []AttributeSpec, mapFunc func([][]byte, int) (bool, error)) error {
	rowBuf := make([][]byte, len(asv))
	for i := 0; i < inst.maxRow; i++ {
		for j, as := range asv {
			p := inst.ags[as.pond]
			rowBuf[j] = p.get(as.position, i)
		}
		ok, err := mapFunc(rowBuf, i)
		if err != nil {
			return err
		}
		if !ok {
			break
		}
	}
	return nil
}
348571
func getResultSliceByRows(rows *core.Rows, erre error) (resultsSlice []map[string][]byte, err error) {
	resultsSlice = make([]map[string][]byte, 0)
	if rows != nil {
		if erre == nil {
			resultsSlice, err = rows2maps(rows)
		}
		rows.Close()
	}
	return
}
1153512
func Funcs(ctx *Context) template.FuncMap {
	result := make(map[string]interface{})
	for k, v := range FuncGens {
		result[k] = v(ctx)
	}
	if ctx != nil {
		for k, v := range ctx.Funcs {
			result[k] = v
		}
	}

	return template.FuncMap(result)
}
590953
func (s *cScreen) mapStyle(style Style) uint16 {
	f, b, a := style.Decompose()
	fa := s.oscreen.attrs & 0xf
	ba := (s.oscreen.attrs) >> 4 & 0xf
	if f != ColorDefault {
		fa = mapColor2RGB(f)
	}
	if b != ColorDefault {
		ba = mapColor2RGB(b)
	}
	var attr uint16
	// We simulate reverse by doing the color swap ourselves.
	// Apparently windows cannot really do this except in DBCS
	// views.
	if a&AttrReverse != 0 {
		attr = ba
		attr |= (fa << 4)
	} else {
		attr = fa
		attr |= (ba << 4)
	}
	if a&AttrBold != 0 {
		attr |= 0x8
	}
	if a&AttrDim != 0 {
		attr &^= 0x8
	}
	if a&AttrUnderline != 0 {
		// Best effort -- doesn't seem to work though.
		attr |= 0x8000
	}
	// Blink is unsupported
	return attr
}
597251
func Flatten(data map[string]interface{}) (flatmap FlatMap, err error) {
	flatmap = make(FlatMap)
	for k, raw := range data {
		err = flatten(flatmap, k, reflect.ValueOf(raw))
		if err != nil {
			return nil, err
		}
	}
	return
}
456747
func mapKeys(vs []reflect.Value) sort.Interface {
	s := mapKeySorter{vs: vs}

	// Type specialization per https://developers.google.com/protocol-buffers/docs/proto#maps.
	if len(vs) == 0 {
		return s
	}
	switch vs[0].Kind() {
	case reflect.Int32, reflect.Int64:
		s.less = func(a, b reflect.Value) bool { return a.Int() < b.Int() }
	case reflect.Uint32, reflect.Uint64:
		s.less = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() }
	case reflect.Bool:
		s.less = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } // false < true
	case reflect.String:
		s.less = func(a, b reflect.Value) bool { return a.String() < b.String() }
	default:
		panic(fmt.Sprintf("unsupported map key type: %v", vs[0].Kind()))
	}

	return s
}
212848
func mapValuesToPrimitive(k string, m map[string]interface{}, schema *Schema) error {
	elemType, err := getValueType(k, schema)
	if err != nil {
		return err
	}

	switch elemType {
	case TypeInt, TypeFloat, TypeBool:
		for k, v := range m {
			vs, ok := v.(string)
			if !ok {
				continue
			}

			v, err := stringToPrimitive(vs, false, &Schema{Type: elemType})
			if err != nil {
				return err
			}

			m[k] = v
		}
	}
	return nil
}
486561
func MapToList(envMap map[string]string) []string {
	out := []string{}
	for k, v := range envMap {
		out = append(out, k+"="+v)
	}
	return out
}
605388
func MapReusedPointers(v reflect.Value) []uintptr {
	pm := &pointerMap{
		reusedPointers: []uintptr{},
	}
	pm.consider(v)
	return pm.reusedPointers
}
466707
func Map(arr interface{}, mapFunc interface{}) interface{} {
	if !IsIteratee(arr) {
		panic("First parameter must be an iteratee")
	}

	if !IsFunction(mapFunc) {
		panic("Second argument must be function")
	}

	var (
		funcValue = reflect.ValueOf(mapFunc)
		arrValue  = reflect.ValueOf(arr)
		arrType   = arrValue.Type()
	)

	kind := arrType.Kind()

	if kind == reflect.Slice || kind == reflect.Array {
		return mapSlice(arrValue, funcValue)
	}

	if kind == reflect.Map {
		return mapMap(arrValue, funcValue)
	}

	panic(fmt.Sprintf("Type %s is not supported by Map", arrType.String()))
}
605044
func convertMap(in map[interface{}]interface{}) map[string]interface{} {
  rtn := make(map[string]interface{})
  for key, val := range in {
    // if the value is a map of interface interfaces
    // make sure to convert them as well    
    var newValue interface{}
    switch val.(type) {
      case []interface{}:
        newValue = convertArray(val.([]interface{}))
      case map[interface{}]interface{}:
        newValue = convertMap(val.(map[interface{}]interface{}))
      default:
        newValue = val
    }

    switch key.(type) {
    case string:
      rtn[key.(string)] = newValue
    }
  }

  return rtn  
}
567408
func compareRows(pk *dosa.PrimaryKey, v1 map[string]dosa.FieldValue, v2 map[string]dosa.FieldValue) (cmp int8) {
	keys := pk.ClusteringKeys
	for _, key := range keys {
		d1 := v1[key.Name]
		d2 := v2[key.Name]
		cmp = compareType(d1, d2)
		if key.Descending {
			cmp = -cmp
		}
		if cmp != 0 {
			return cmp
		}
	}
	return cmp
}
59916
func (r *Rows) ScanMap(a NullStringMap) error {
	cols, _ := r.Columns()
	var refs []interface{}
	for i := 0; i < len(cols); i++ {
		var t sql.NullString
		refs = append(refs, &t)
	}
	if err := r.Scan(refs...); err != nil {
		return err
	}

	for i, col := range cols {
		a[col] = *refs[i].(*sql.NullString)
	}

	return nil
}
347401
func (rs *Rows) ScanMap(dest interface{}) error {
	vv := reflect.ValueOf(dest)
	if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
		return errors.New("dest should be a map's pointer")
	}

	cols, err := rs.Columns()
	if err != nil {
		return err
	}

	newDest := make([]interface{}, len(cols))
	vvv := vv.Elem()

	for i, _ := range cols {
		newDest[i] = ReflectNew(vvv.Type().Elem()).Interface()
		//v := reflect.New(vvv.Type().Elem())
		//newDest[i] = v.Interface()
	}

	err = rs.Rows.Scan(newDest...)
	if err != nil {
		return err
	}

	for i, name := range cols {
		vname := reflect.ValueOf(name)
		vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
	}

	return nil
}
1142052
func KeyRangesOverlap(first, second *topodatapb.KeyRange) (*topodatapb.KeyRange, error) {
	if !KeyRangesIntersect(first, second) {
		return nil, fmt.Errorf("KeyRanges %v and %v don't overlap", first, second)
	}
	if first == nil {
		return second, nil
	}
	if second == nil {
		return first, nil
	}
	// compute max(c,a) and min(b,d)
	// start with (a,b)
	result := *first
	// if c > a, then use c
	if bytes.Compare(second.Start, first.Start) > 0 {
		result.Start = second.Start
	}
	// if b is maxed out, or
	// (d is not maxed out and d < b)
	//                           ^ valid test as neither b nor d are max
	// then use d
	if len(first.End) == 0 || (len(second.End) != 0 && bytes.Compare(second.End, first.End) < 0) {
		result.End = second.End
	}
	return &result, nil
}
677118
func mapFieldKeys(m map[*descriptor.FieldDescriptorProto]string) []*descriptor.FieldDescriptorProto {
	keys := make([]*descriptor.FieldDescriptorProto, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Sort(byTypeName(keys))
	return keys
}
1191227
func (e *Encoder) marshalMap(m reflect.Value, child bool) (string, bool) {
	var ok bool
	if ok = supportedBaseKind(m); !ok {
		return "", false
	}
	// get the kind of the map value
	var row string
	var sv stringValues = m.MapKeys()
	// sort the map keys first
	// TODO have this work on things other than string?
	sort.Sort(sv)
	for i, key := range sv {
		val := m.MapIndex(key)
		kk, ok := e.marshal(key, true)
		if !ok {
			return "", false
		}
		var kval, vval string
		for j, tmp := range kk {
			if j > 0 && j < len(kk) {
				kval += ","
			}
			kval += tmp
		}
		if len(kk) > 1 {
			kval = fmt.Sprintf("%s%s%s", e.sepBeg, kval, e.sepEnd)
		}
		vv, ok := e.marshal(val, true)
		if !ok {
			return "", false
		}
		for j, tmp := range vv {
			if j > 0 && j < len(vv) {
				vval += ","
			}
			vval += tmp
		}
		if len(vv) > 1 {
			vval = fmt.Sprintf("%s%s%s", e.sepBeg, vval, e.sepEnd)
		}
		vval = fmt.Sprintf("%s:%s", kval, vval)
		if i == 0 {
			row = vval
		} else {
			row = fmt.Sprintf("%s,%s", row, vval)
		}
	}
	if child {
		row = fmt.Sprintf("%s%s%s", e.sepBeg, row, e.sepEnd)
	}
	return row, true
}