The principle slice

main Package 

/ * 
#include <stdlib.h> 
 * / 
Import "C" 
Import ( 
	"the unsafe" 
	"FMT" 
) 

type struct {the Slice 
	the Data unsafe.Pointer // universal pointer type corresponds to the C language * void 
	len // int the effective length of 
	cap int // valid capacitance 
} 

const = the TAG. 8 

/ * 
FUNC main () { 
	// definition of a slice 
	// 1, the data memory address 2, len valid data length of 3, the effective capacity of 24 words expandable cap section 
	var S [] int 

	//unsafe.Sizeof accounting calculation data type in memory byte size 
	fmt.Println (unsafe.Sizeof (S)) 
} 
* / 
/ * 
FUNC main () { 
	var interface I} { 
	I = 10 // only support ==! = 

	// type of assertion is based on the type of data conversion interface 
	//value,ok:=i.(int)
	OK IF {// 
	// fmt.Println ( "integer data:", value) 
	// fmt.Printf ( "% T \ n-", value) 
	//} 
	// get the data type of the interface reflection 
	// t: reflect.TypeOf = (I) 
	//fmt.Println(t) 

	// Get the value reflecting data interface type 
	V: = reflect.ValueOf (I) 
	fmt.Println (V) 

	I1: = 10 
	I2: = 20 is 

	IF the reflect. typeof (I1) == reflect.Typeof (I2) { 
		V1: = reflect.Valueof (I1) 
		V2: = reflect.Valueof (I2) 
		results V1 + V2 = 
	} 
} 
* / 
// the Create (capacity data length) 
FUNC ( the Slice * S) the Create (int L, C int, int the data ...) { 
	// if the data returned is empty 
	IF len (the data) == 0 { 
		return 
	} 
	// length is less than the capacity of less than 0 is greater than the capacity of the data length is greater than 0 length 
	if l <0 C || <|| L 0> C || len (the Data)> L { 
		return 
	} 
	// ULONGLONG unsigned Long Long unsigned long integer 
	// open space for storing data C language code 
	// if heap space open failure return value is NULL nil equivalent memory address number 0 of the space 
	s.Data = C.malloc (C.ulonglong (C) *. 8) 
	s.len = L 
	s.cap = C 

	// can be transformed into calculated pointer type 
	P: = UIntPtr (s.Data) 
	for _, V: the data Range = { 
		// data storage 
		* (int *) (unsafe.Pointer (P)) = V 
		// offset pointer 
		p + = TAG 
		//p+=unsafe.Sizeof(1) 
	} 
} 

// print the Print slice 
FUNC (S * the slice) the Print () { 
	IF S == nil { 
		return 
	} 

	// will turn into a universal pointer can be calculated pointer 
	p: = uintptr (s.Data) 
	for I: = 0; I <s.len; I ++ {
		// get the data memory 
		fmt.Print (* (int *) (unsafe.Pointer (P)), "") 
		P = the TAG + 
	} 

} 

// append slice 
func (s * Slice) Append ( Data ... int) { 
	IF S == nil { 
		return 
	} 
	IF len (the data) == 0 { 
		return 
	} 

	// if the added data exceeds the capacity of the 
	IF s.len + len (the data)> s.cap { 
		// capacity expansion 
		/ /C.realloc (pointer byte size), 2-fold expansion Go language. 
		= C.realloc s.Data (s.Data, C.ulonglong (s.cap) * 2 *. 8) 
		// capacity change value 
		s.cap s.cap * = 2 
	} 

	P: = UIntPtr (s.Data) 
	I for: = 0; I <s.len; I ++ { 
		// offset pointer 
		P + = the TAG 
	} 

	// add data 
	for _, v: = range data {
		* (int *) (unsafe.Pointer (P)) = V 
		P + = the TAG 
	} 
	// Update the valid data (length) 
	s.len s.len + = len (the Data) 
} 

// Get the GetData element (subscript) int return value element 
FUNC (S * the Slice) GetdData (int index) {int 
	IF == S == nil nil || s.Data { 
		return 0 
	} 
	IF index <0 || index> {. 1 s.len- 
		return 0 
	} 

	P: = UIntPtr (s.Data) 
	for I: = 0; I <index; I ++ { 
		P = the TAG + 
	} 
	return * (int *) (unsafe.Pointer (P)) 
} 

// find elements Search (element ) returns a value of the subscript int 
FUNC (S * the Slice) Search (the Data int) {int 
	IF == S == nil nil || s.Data { 
		return -1 
	} 

	P: = UIntPtr (s.Data) 
	for I: = 0; I <s.len; I ++ { 
		// find the position of the first element of the returned data appears 
		IF * (int *) (unsafe.Pointer (P)) {the Data == 
			return I 
		} 
		// Pointer offset 
		P = the TAG + 
	} 
	return -1 
} 

// remove elements delete (subscript) 
FUNC (S * the Slice) delete (int index) { 
	IF == S == nil nil || s.Data { 
		return 
	} 
	IF index <0 || index>. 1-s.len { 
		return 
	} 

	// pointer to the need to remove the indexing position 
	P: = UIntPtr (s.Data) 
	for I: = 0; I <index; I ++ { 
		P = the TAG + 
	} 

	// performed with the current value of the pointer value corresponding to a pointer corresponding to the next assignment 
	TEMP: = P 
	for I: = index; I <s.len; I ++ { 
		TEMP + = the TAG
		* (int *) (unsafe.Pointer (P)) * = (int *) (unsafe.Pointer (TEMP)) 
		P = the TAG + 
	} 

	s.len-- 
} 

// Insert Element Insert (subscript elements) 
FUNC ( the Slice * S) the insert (int index, int the data) { 
	IF == S == nil nil || s.Data { 
		return 
	} 
	IF index <0 || index>. 1-s.len { 
		return 
	} 

	// If data is inserted the last 
	// index == s.len-IF. 1 { 
	// P: = UIntPtr (s.Data) 
	// 
	// // for I: = 0; I <s.len; I ++ { 
	// // the TAG + = P 
	//} // 
	// + = P * UIntPtr the TAG (-s.len. 1) 
	// * (int *) (unsafe.Pointer (P)) = the Data 
	// s.len ++ 
	// return 
	/ /} 
	// append method call 
	if index == s.len-1 { 
		s.Append (the Data) 
		return 
	}
 
	// Get data insertion position 
	P: = UIntPtr (s.Data) 
	for I: = 0; I <index; I ++ { 
		P = the TAG + 
	} 
	// get a pointer to the location of the end 

	temp: = uintptr (s.Data) 
	TEMP + = * UIntPtr the TAG (s.len) 

	// behind the data sequentially moved backward 
	for i: = s.len; i> index; i-- { 
		// before data with current data assignment 
		* (int *) (unsafe.Pointer (TEMP)) * = (int *) (unsafe.Pointer (TEMP - the TAG)) 
		TEMP - the TAG = 
	} 

	// insert the modified data subscripts 
	* (* int) (unsafe. the Pointer (P)) = the Data 
	s.len ++ 
} 

// destruction slice 
FUNC (S * the slice) the destroy () { 
	// call the C language suitable release heap space 
	C.free (s.Data) 
	s.Data = nil 
	s.len 0 = 
	s.cap 0 = 
	S = nil 
}

 

Guess you like

Origin www.cnblogs.com/lurenq/p/12081707.html