slice

Conversion of a slice of string into a slice of custom type

社会主义新天地 提交于 2019-12-05 10:25:29
I'm quite new to Go, so this might be obvious. The compiler does not allow the following code: ( http://play.golang.org/p/3sTLguUG3l ) package main import "fmt" type Card string type Hand []Card func NewHand(cards []Card) Hand { hand := Hand(cards) return hand } func main() { value := []string{"a", "b", "c"} firstHand := NewHand(value) fmt.Println(firstHand) } The error is: /tmp/sandbox089372356/main.go:15: cannot use value (type []string) as type []Card in argument to NewHand From the specs, it looks like []string is not the same underlying type as []Card, so the type conversion cannot occur.

How to create an array or a slice from an array unsafe.Pointer in golang?

▼魔方 西西 提交于 2019-12-05 08:42:49
A pointer to an array, let's say: p := uintptr(unsafe.Pointer(&array)) size := 5 I can't access to the variable array , the code above is used to make it more clear. Also, I know the size of the array, but the size is not constant, it changes according to the runtime. Now, I want to initialize a slice or an array with the known pointer, size and of course the data type. I come up with the following code: data := make([]byte, size) stepSize := unsafe.Sizeof(data[0]) for i := 0; i < size; i++ { data[i] = *(*byte)(unsafe.Pointer(p)) p += stepSize } fmt.println(data) but this method does memory

How to raise an IndexError when slice indices are out of range?

。_饼干妹妹 提交于 2019-12-05 08:18:54
The Python Documentation states that slice indices are silently truncated to fall in the allowed range and therefor no IndexErrors are risen when slicing a list, regardless what start or stop parameters are used: >>> egg = [1, "foo", list()] >>> egg[5:10] [] Since the list egg does not contain any indices greater then 2 , a egg[5] or egg[10] call would raise an IndexError : >> egg[5] Traceback (most recent call last): IndexError: list index out of range The question is now, how can we raise an IndexError , when both given slice indices are out of range? In Python 2 you can override __getslice_

Lab6:进程的调度

雨燕双飞 提交于 2019-12-05 07:30:58
CPU调度 从就绪队列中挑选下一个占用CPU运行的进程,从多个可用CPU中挑选就绪进程可使用的CPU资源 调度策略 比较调度算法的准则 CPU使用率 吞吐量 周转时间 就绪等待时间 响应时间 吞吐量与延迟 低延迟:喝水的时候想要一打开水龙头水就流出来 高带宽:给游泳池充水时希望从水龙头里同时流出大量的水,并且不介意是否存在延迟 处理机调度策略的响应时间目标 减少响应时间 减少平均响应时间的波动 增加吞吐量 减少等待时间 调度算法 先来先服务算法(First Come First Served, FCFS) 依据进程进入就绪状态的先后顺序排列,进程进入等待或结束状态时,就绪队列中的下一个进程占用CPU 但是FCFS的平均等待时间波动较大,I/O资源和CPU资源的利用率较低 短进程优先算法(SPN) 选择就绪队列中执行时间最短进程占用CPU进入运行状态,用历史的执行时间来预估未来的执行时间,短进程优先算法具有最优平均周转时间 但是连续的短进程流会使长进程无法获得CPU资源 最高响应比优先算法(HRRN) 选择就绪队列中响应比R值最高的进程 R=(w+s)/s w: 等待时间(waiting time) s: 执行时间(service time) 时间片轮转算法(RR, Round-Robin) 利用时间片作为分配处理机资源的基本时间单元,时间片结束时,按FCFS算法切换到下一个就绪进程

How to slice a dataframe by selecting a range of columns and rows based on names and not indexes?

醉酒当歌 提交于 2019-12-05 06:34:50
This is a follow-up question of the question I asked here . There I learned a) how to do this for columns (see below) and b) that the selection of rows and columns seems to be quite differently handled in R which means that I cannot use the same approach for rows. So suppose I have a pandas dataframe like this: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(10, size=(6, 6)), columns=['c' + str(i) for i in range(6)], index=["r" + str(i) for i in range(6)]) c0 c1 c2 c3 c4 c5 r0 4 2 3 9 9 0 r1 9 0 8 1 7 5 r2 2 6 7 5 4 7 r3 6 9 9 1 3 4 r4 1 1 1 3 0 3 r5 0 8 5 8 2 9 then

How do you reference Array.prototype.slice.call()?

随声附和 提交于 2019-12-05 05:45:06
I am writing a script in which I need to clone arrays in many different places. For this reason, I would like to do the following to emulate a cloning function: var clone = [].slice.call; var arr1 = [1,2,3,4,5,6,7,8,9,10]; var arr2 = clone(arr1, 0); Unfortunately, the above code results in: TypeError: object is not a function . I realize there are many functions out there to do deep cloning and shallow copies but I just want to use the built in method. Interestingly enough, the following does work: var clone = [].slice; var arr1 = [1,2,3,4,5,6,7,8,9,10]; var arr2 = clone.call(arr1, 0); Does

Appending to slice bad performance.. why?

孤街醉人 提交于 2019-12-05 05:43:22
I'm currently creating a game using GoLang. I'm measuring the FPS. I'm noticing about a 7 fps loss using a for loop to append to a slice like so: vertexInfo := Opengl.OpenGLVertexInfo{} for i := 0; i < 4; i = i + 1 { vertexInfo.Translations = append(vertexInfo.Translations, float32(s.x), float32(s.y), 0) vertexInfo.Rotations = append(vertexInfo.Rotations, 0, 0, 1, s.rot) vertexInfo.Scales = append(vertexInfo.Scales, s.xS, s.yS, 0) vertexInfo.Colors = append(vertexInfo.Colors, s.r, s.g, s.b, s.a) } I'm doing this for every sprite, every draw. The question is why do I get such a huge performance

How to slice/cut an image into pieces

人盡茶涼 提交于 2019-12-05 05:35:50
问题 So I have a function that receives OpenCV image and turns it into gray-scale. void UseLSD(IplImage* destination) { IplImage *destinationForGS = cvCreateImage(cvSize(destination->width, destination->height),IPL_DEPTH_8U,1); cvCvtColor(destination,destinationForGS,CV_RGB2GRAY); } How now to cut that image into images of size 10x10 pixels and iterate true them? (width and height may not divide on 10 but if there would be some loss (like loss from 1*h to 9*h+9*h pixels per image) it would be OK

Why does slice [:-0] return empty list in Python

…衆ロ難τιáo~ 提交于 2019-12-05 04:31:11
Stumbled upon something slightly perplexing today while writing some unittests: blah = ['a', 'b', 'c'] blah[:-3] # [] blah[:-2] # ['a'] blah[:-1] # ['a', 'b'] blah[:-0] # [] Can't for the life of me figure out why blah[:-0] # [] should be the case, the pattern definitely seems to suggest that it should be ['a', 'b', 'c'] . Can anybody help to shed some light on why that is the case? Haven't been able to find mention in the docs as to why that is the case. -0 is 0 , and a slice that goes from the beginning of a list inclusive to index 0 non-inclusive is an empty list . Python doesn't treat -0

pandas mix position and label indexing without chaining

早过忘川 提交于 2019-12-05 04:27:41
Since .ix has been deprecated as of Pandas 0.20 , I wonder what is the proper way to mix lable-based, boolean-based and position-based indexing in Pandas? I need to assign values to a slice of dataframe that can be best referenced with label or boolean on the index and position on the columns. For example (using .loc as placeholder for the desired slicing method): df.loc[df['a'] == 'x', -12:-1] = 3 obviously this doesn't work, with which I get: TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [-12] of <class 'int'> If I use .iloc , I get: