我在hashCode()为我创建的类编写方法时遇到了一些麻烦.此类旨在在TreeSet中使用,因此,它实现了Comparable.该类具有以下变量:
public class Node implements Comparable<Node> {
Matrix matrix;
int[] coordinates= new int[2];
Node father;
int depth;
int cost;
Run Code Online (Sandbox Code Playgroud)
这是该compareTo()方法的实现.我希望TreeSet按成本组织这些Node结构,因此,compareTo()返回简单减法的结果.
public int compareTo(Node nodeToCompare) {
return this.cost - nodeToCompare.cost;
}
Run Code Online (Sandbox Code Playgroud)
我还实现了一种equals()方法.
public boolean equals(Object objectToCompare) {
if(objectToCompare== this) {return true;}
if(objectToCompare== null || objectToCompare.getClass()!= this.getClass()) {return false;}
Node objectNode= (Node) objectToCompare;
return this.father.equals(objectNode.father) &&
this.depth== objectNode.depth &&
this.cost== objectNode.cost &&
this.matrix.equals(objectNode.matrix) &&
Arrays.equals(this.coordinates, objectNode.coordinates);
}
Run Code Online (Sandbox Code Playgroud)
说完这一切之后,我有几个问题:
equals()方法,我应该实现一个新hashCode() …对于OCaml来说,我是一个完全新手.我最近才开始使用这种语言(大约2周前),但不幸的是,我的任务是制作语法分析器(解析器+词法分析器,其功能是接受或不接受句子)以获得一种语言使用Menhir.现在,我在互联网上找到了一些有关OCaml和Menhir的资料:
Menhir手册.
在Sourceforge的Toss主页上的简短Menhir教程.
derdon在github上的一个Menhir例子.
关于OCaml的一本书(关于ocamllex + ocamlyacc的一些事情
SooHyoung的随机ocamllex教程哦.
以及Menhir源代码附带的示例.
(我不能放两个以上的超链接,所以我不能直接链接到我在这里提到的一些网站.抱歉!)
所以,正如你所看到的,我一直在拼命寻找越来越多的资料来帮助我制定这个计划.不幸的是,我仍然无法掌握许多概念,因此,我遇到了很多困难.
对于初学者,我不知道如何正确编译我的程序.我一直在使用以下命令:
ocamlbuild -use-menhir -menhir "menhir --external-tokens Tokens" main.native
Run Code Online (Sandbox Code Playgroud)
我的程序分为四个不同的文件:main.ml; lexer.mll; parser.mly; tokens.mly.main.ml是从作为参数给出的文件系统中的文件获取输入的部分.
let filename = Sys.argv.(1)
let () =
let inBuffer = open_in filename in
let lineBuffer = Lexing.from_channel inBuffer in
try
let acceptance = Parser.main Lexer.main lineBuffer in
match acceptance with
| true -> print_string "Accepted!\n"
| false -> print_string "Not accepted!\n"
with
| Lexer.Error msg -> Printf.fprintf stderr "%s%!\n" msg
| Parser.Error -> Printf.fprintf stderr "At …Run Code Online (Sandbox Code Playgroud) 我有一个完全用C语言编写的程序,其中包含多个目标(.o)文件.这些文件都打包在一个存档文件(.a)中,而该存档文件又在程序主(.c)文件的编译时使用.
我想在Go中为这个项目写一个新文件.我的想法是编写这个.go文件,然后(.o)从中创建一个目标文件.之后,我想将此对象文件放在已提到的存档(.a)文件中.
这基本上意味着我想从C程序调用Go函数.我已经读过这个问题了,虽然它向我展示了我想要的东西是通过GCCGO实现的,但它并不是100%清楚如何去做.
即使使用最基本的测试,我也会在链接阶段遇到错误.更具体地说,这是一个基本的例子:
printString.go
package main
import
(
"fmt"
)
func PrintString(buff string) int {
fmt.Printf(buff)
return 1
}
Run Code Online (Sandbox Code Playgroud)
c_caller.c
#define _GNU_SOURCE
#include <stdio.h>
extern int PrintString(char*) __asm__ ("print.main.PrintString");
int main() {
char *string_to_pass= NULL;
asprintf(&string_to_pass, "This is a test.");
int result= PrintString(string_to_pass);
if(result) {printf("Everything went as expected!\n");}
else {printf("Uh oh, something went wrong!\n");}
return result;
}
Run Code Online (Sandbox Code Playgroud)
编译
为了编译Go文件,我使用了以下命令:
gccgo -c printString.go -o …Run Code Online (Sandbox Code Playgroud) failed-installation google-cloud-platform gcloud google-cloud-sdk
我有一个用 pthread 实现的多线程 C 程序,它使用读写锁来保护特定的数据结构。pthread_rwlock_rdlock应该是阻塞调用,但在调用时可能会失败并返回值 EAGAIN。文档说:
在以下情况下,pthread_rwlock_rdlock() 和 pthread_rwlock_tryrdlock() 函数可能会失败:
[再次]
无法获取读锁,因为已超出 rwlock 的读锁最大数量。
这意味着在任何给定时间点都有最大数量的线程可以获得读锁。考虑到这一点,我创建了一个函数来检查返回值并无限循环,直到它真正获得读锁。
void
cache_rdlock(void)
{
int result= pthread_rwlock_rdlock(&cache_access);
if(result== EAGAIN)
{
while((result= pthread_rwlock_rdlock(&cache_access))== EAGAIN);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
在程序执行过程中的某个时刻,两个试图获取此读锁的并发线程将永久挂起在此函数中。看到程序在执行过程中正确解锁了这个读写锁,我该如何解决这个问题呢?有没有办法增加并发读锁的最大数量?为了使程序正常工作,我应该对此函数进行哪些更改?