我需要从一个以空格分隔的人类可读文件中读取一系列数字并进行一些数学运算,但是我在读取文件时遇到了一些真正奇怪的内存行为。
如果我阅读数字并立即丢弃它们......
#include <fstream>
int main(int, char**) {
std::ifstream ww15mgh("ww15mgh.grd");
double value;
while (ww15mgh >> value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的程序根据 valgrind 分配了 59MB 的内存,相对于文件的大小线性缩放:
$ g++ stackoverflow.cpp
$ valgrind --tool=memcheck --leak-check=yes ./a.out 2>&1 | grep total
==523661== total heap usage: 1,038,970 allocs, 1,038,970 frees, 59,302,487
Run Code Online (Sandbox Code Playgroud)
但是,如果我ifstream >> string改为使用然后使用sscanf来解析字符串,我的内存使用情况看起来更加正常:
#include <fstream>
#include <string>
#include <cstdio>
int main(int, char**) {
std::ifstream ww15mgh("ww15mgh.grd");
double value;
std::string text;
while (ww15mgh >> text)
std::sscanf(text.c_str(), "%lf", &value);
return …Run Code Online (Sandbox Code Playgroud) 前一个问题显示了如何使用静态初始值设定项在类中companion object.我正在尝试找到一种在包级别添加静态初始化程序的方法,但似乎包没有伴随对象.
// compiler error: Modifier 'companion' is not applicable inside 'file'
companion object { init { println("Loaded!") } }
fun main(args: Array<String>) { println("run!") }
Run Code Online (Sandbox Code Playgroud)
我试过,可能已经是有道理的(其他的变化init就其本身而言static),我知道作为一个解决办法,我可以用暴殄天物val作为
val static_init = {
println("ugly workaround")
}()
Run Code Online (Sandbox Code Playgroud)
但有没有一种干净,官方的方式来达到同样的效果?
编辑:正如@ mfulton26的回答所提到的那样,JVM中没有真正的包级函数.在幕后,kotlin编译器包含任何自由函数,包括main在类中.我想静态初始化程序添加到该班-通过科特林在文件中声明的自由函数生成的类.
我想在a上设置背景和圆角边框<tbody/>,例如
tbody { border-radius: 15px; border: 1px solid black; background: #ccf; }
Run Code Online (Sandbox Code Playgroud)
但是,当我在Codepen中尝试这个时,边框和背景颜色显示,但<tbody/>仍然有方角.
我能够使用一系列:last-child和:first-child选择器来解决这个问题,将半径应用于td角落上的各个s,例如
tbody tr:first-child td:first-child { border-top-left-radius: 15px; }
Run Code Online (Sandbox Code Playgroud)
这个版本做什么,我希望(至少,火狐下),但也觉得非常详细和哈克,一个问题,当我添加前缀版本兼容性(那会变得更糟-moz-,-webkit-等等),并支持<th/>除了元素<td/>.是否有一种简洁,纯粹的css方式来获得这种行为?
IPython源代码包括一个getattr检查函数'_ipython_canary_method_should_not_exist_'开头是否存在的检查get_real_method:
def get_real_method(obj, name):
"""Like getattr, but with a few extra sanity checks:
- If obj is a class, ignore everything except class methods
- Check if obj is a proxy that claims to have all attributes
- Catch attribute access failing with any exception
- Check that the attribute is a callable object
Returns the method or None.
"""
try:
canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
except Exception:
return None
if …Run Code Online (Sandbox Code Playgroud) 这是python中的合并排序逻辑:(这是第一部分,忽略函数merge())问题点是将递归逻辑转换为while循环.代码礼貌:Rosettacode Merge Sort
def merge_sort(m):
if len(m) <= 1:
return m
middle = len(m) / 2
left = m[:middle]
right = m[middle:]
left = merge_sort(left)
right = merge_sort(right)
return list(merge(left, right))
Run Code Online (Sandbox Code Playgroud)
是否有可能在while循环中使它成为一种动态,而每个左右数组都分成两个,一种指针根据左右数组的数量不断增加并打破它们直到只剩下单个长度的列表?因为每当下一次分割进入左右两侧时,阵列就会不断分解,直到只剩下单个长度列表,因此左侧(左 - 左,右 - 右)和右侧(右 - 左,右 - 右)中断将增加,直到达到所有大小为1的列表.
Kotlin 和 Groovy 都提供了一种编写高阶函数的方法,其中函数参数具有隐式接收器。
科特林版本
class KotlinReceiver {
fun hello() {
println("Hello from Kotlin")
}
}
class KotlinVersion {
fun withReceiver(fn: KotlinReceiver.() -> Unit) {
KotlinReceiver().fn()
}
}
// And then I can call...
val foo = KotlinVersion()
foo.withReceiver { hello() }
Run Code Online (Sandbox Code Playgroud)
Groovy 版本
class GroovyReceiver {
void hello() {
println("Hello from Groovy")
}
}
class GroovyVersion {
void withReceiver(Closure fn) {
fn.resolveStrategy = Closure.DELEGATE_FIRST
fn.delegate = new GroovyReceiver()
fn.run()
}
}
// And then I can call...
def foo …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个用C++编写的应用程序,它有一个简洁的代码库,可以学习,甚至可以为它做出贡献.很多用C++编写的应用程序要么非常大,要么做一些非常高级的东西,这只是C++的领域.
大小和形状的优秀候选者将类似于来自Archlinux的pacman和libalpm,但它是用简单的C编写的,而不是C++.WebKit远远不够大,Protobuf看起来很尴尬.Gnote看起来是一个很好的起点,我也使用GNOME,但我不确定它,因为它提供了一个GUI.这让我回到了pacman ; 我自己已经在使用它,它不会分散像GUI这样的东西.
所以我决定寻找一些小而且理智的东西,它提供了一个CLI,我可以自己使用它.我使用术语note + c ++ + stars打开了github的搜索,结果是taskwarrior.简要介绍任务来源(战士)看起来很有希望.
你知道一个用C++编写的整洁项目吗?
谢谢
精简版
在 SQLAlchemy 的 ORM 列声明中,我如何server_default=sa.FetchedValue()在一种方言和default=somePythonFunction另一种方言上使用,以便我真正的 DBMS 可以使用触发器填充事物,并且我的测试代码可以针对 sqlite 编写?
背景
我正在使用 SQLAlchemy 的声明性 ORM 来处理 Postgres 数据库,但尝试针对 编写单元测试sqlite:///:memory:,并且遇到了在其主键上计算默认值的列的问题。对于一个最小的例子:
CREATE TABLE test_table(
id VARCHAR PRIMARY KEY NOT NULL
DEFAULT (lower(hex(randomblob(16))))
)
Run Code Online (Sandbox Code Playgroud)
SQLite 本身对这个表定义 ( sqlfiddle )非常满意,但 SQLAlchemy 似乎无法计算出新创建的行的 ID。
class TestTable(Base):
__tablename__ = 'test_table'
id = sa.Column(
sa.VARCHAR,
primary_key=True,
server_default=sa.FetchedValue())
Run Code Online (Sandbox Code Playgroud)
像这样的定义在 postgres 中工作得很好,但在 sqlite 中死了(正如你在 Ideone 上看到的),FlushError当我打电话时Session.commit:
sqlalchemy.orm.exc.FlushError:实例<TestTable at 0x7fc0e0254a10>具有NULL身份密钥。如果这是自动生成的值,请检查数据库表是否允许生成新的主键值,以及映射的 Column 对象是否配置为期望这些生成的值。还要确保这flush() …
在上一个问题中,发现使用最新版本的 GNU libstdc++ 从以空格分隔的人类可读文件( mirror )读取一系列数字会导致大量分配,并随文件大小线性缩放。
鉴于上面链接的文件和这个测试程序:
#include <fstream>
int main(int, char**) {
std::ifstream ww15mgh("ww15mgh.grd");
double value;
while (ww15mgh >> value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind--tool=memcheck报告:
==523661== total heap usage: 1,038,970 allocs, 1,038,970 frees, 59,302,487
Run Code Online (Sandbox Code Playgroud)
因为这百万分配中的每一个都在operator>>返回之前立即释放,所以没有泄漏并且程序在发布版本中的实际内存占用很小(81KB)。但是,编译-fsanitize=address将大量分配变成了一个真正的问题。
以下是上述程序的总内存占用,运行和不运行 ASAN:
$ g++ stackoverflow.cpp -o _build/stackoverflow
$ /usr/bin/time -v _build/stackoverflow |& grep 'm r'
Maximum resident set size (kbytes): 3512
Run Code Online (Sandbox Code Playgroud)
$ g++ stackoverflow.cpp -o _build/stackoverflow_asan -fsanitize=address
$ /usr/bin/time -v _build/stackoverflow_asan |& grep 'm r' …Run Code Online (Sandbox Code Playgroud)