给定两个整数X和Y,我想覆盖位置P到P + N的位.
例:
int x = 0xAAAA; // 0b1010101010101010
int y = 0x0C30; // 0b0000110000110000
int result = 0xAC3A; // 0b1010110000111010
Run Code Online (Sandbox Code Playgroud)
这个程序有名字吗?
如果我有面具,操作很简单:
int mask_x = 0xF00F; // 0b1111000000001111
int mask_y = 0x0FF0; // 0b0000111111110000
int result = (x & mask_x) | (y & mask_y);
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何以通用的方式编写它,例如在下面的通用C++函数中:
template<typename IntType>
IntType OverwriteBits(IntType dst, IntType src, int pos, int len) {
// If:
// dst = 0xAAAA; // 0b1010101010101010
// src = 0x0C30; // 0b0000110000110000
// pos = 4 ^
// len …Run Code Online (Sandbox Code Playgroud) 这是一个令人尴尬的问题,但即使是boost.interprocess提供的精心编写的文档也不足以让我弄清楚如何做到这一点.
我有一个cached_adaptive_pool分配器实例,我想用它来构造一个对象,传递构造函数参数:
struct Test {
Test(float argument, bool flag);
Test();
};
// Normal construction
Test obj(10, true);
// Normal dynamic allocation
Test* obj2 = new Test(20, false);
typedef managed_unique_ptr<
Test, boost::interprocess::managed_shared_memory>::type unique_ptr;
// Dynamic allocation where allocator_instance == cached_adaptive_pool,
// using the default constructor
unique_ptr obj3 = allocator_instance.allocate_one()
// As above, but with the non-default constructor
unique_ptr obj4 = allocator_instance ... ???
Run Code Online (Sandbox Code Playgroud)
这可能是我在如何使用分配器对象方面的失败.但在任何情况下,我都看不到如何使用这个特定的分配器,使用cached_adaptive_pool中指定的接口将构造函数参数传递给我的对象.
cached_adaptive_pool有方法:void construct(const pointer & ptr, const_reference v)但我不明白这意味着什么,我找不到使用它的例子.
我的头一整天都在模板中游泳,所以即使答案很明显,也会非常感激.
当我使用标准的python日志记录模块有很多不同的模块时,下面的堆栈跟踪几乎没有帮助我找出确切地说,我有一个格式错误的日志语句:
Traceback (most recent call last):
File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit
msg = self.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 648, in format
return fmt.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 436, in format
record.message = record.getMessage()
File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
我只是开始使用python的日志模块,所以也许我忽略了一些明显的东西.我不确定堆栈跟踪是否无用,因为我正在使用greenlets,或者如果这对于日志记录模块来说是正常的,但任何帮助都将不胜感激.我愿意修改源代码,任何使日志库真正给出问题所在的线索的东西.
typedef boost::interprocess::managed_shared_memory::segment_manager
segment_manager_t; // Works fine, segment_manager is a class
typedef boost::interprocess::adaptive_pool
allocator_t; // Can't do this, adaptive_pool is a template
Run Code Online (Sandbox Code Playgroud)
我的想法是,如果我想在boost进程之间切换共享内存和分配器的几个不同选项,我只需要修改typedef.不幸的是,分配器是模板,所以我不能键入我想要使用的分配器.
有没有办法在C++中实现模板的别名?(除了显而易见的#define ALLOCATOR_T boost::interprocess::adaptive_pool)
我正在使用py.test来测试我的一些模块,其中包含相当多的stdlib日志记录.我当然喜欢将日志记录到stdout,这是由py.test捕获的,因此如果测试失败,我将获得所有相关的日志消息.
这样做的问题是,在py.test 丢弃此对象之后,日志记录模块最终尝试将消息记录到py.test提供的'stdout'对象.也就是说,我得到:
Traceback (most recent call last):
File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
h.flush()
File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
self.stream.flush()
ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)
如果我关闭捕获-s,我没有任何问题,但当然这使得测试输出与不相关的日志记录无法读取.
谁能告诉我将stdlib日志记录与py.test集成的正确方法?
(我试着看看这个,它看起来应该只是没有问题,所以它对我帮助不大)
pub struct WidgetWrap {
// ...
widget: RefCell<Box<Any>>,
}
Run Code Online (Sandbox Code Playgroud)
在某些时候,我想投Box<Any>给Box<WidgetTrait>
let mut cell = widget.borrow_mut();
let w = cell.downcast_mut::<Box<WidgetTrait>>();
Run Code Online (Sandbox Code Playgroud)
这给了我这样的错误:
error: instantiating a type parameter with an incompatible type
`Box<WidgetTrait>`, which does not fulfill `'static` [E0144]
Run Code Online (Sandbox Code Playgroud)
这究竟意味着什么?
我看过如何解决:值可能包含引用; 添加`'static`绑定到`T`并尝试添加+ 'static到处.
pub struct WidgetWrap {
// ...
widget: RefCell<Box<Any + 'static>>,
}
let mut cell = widget.borrow_mut();
let w = cell.downcast_mut::<Box<WidgetTrait + 'static>>();
Run Code Online (Sandbox Code Playgroud)
它修复了编译错误,但是当我尝试打开如下所示的downcasrap框时失败.是的,盒子的内容是一个实现的对象WidgetTrait.
显然,我在Rust中的编码水平我不太了解,但也许有人可以帮助我更好地掌握上述任务中涉及的概念.
对不起,我通常很难阅读当前的ctypes文档...
如果我有一个带有const char *指针的C函数,并且我知道它既不会修改传入的字符串,也不会在函数调用之外保留对它的引用,那么将指针直接传递给python的字节真的很有意义串.
ctypes可以做到这一点,还是只是简单的不支持?我真的必须create_string_buffer将我的字符串复制到其中吗?
基本上,当使用减法溢出整数时,您获得的行为,但对于给定的位数.显而易见的方法,假设有符号整数:
template <int BITS>
int sub_wrap(int v, int s) {
int max = (1<<(BITS));
v -= s;
if (v < -max) v += max*2;
// or if branching is bad, something like:
// v += (max*2) * (v < -max)
return v;
}
// For example subtracting 24 from -16 with 5 bit wrap,
// with a range of -32, 31
sub_wrap<5>(-16, 28); -> 20
Run Code Online (Sandbox Code Playgroud)
有没有一种巧妙的做法,不那么丑陋,最好比上面的更快?
更新:对于混乱感到抱歉.我不假思索地包括使用除了叹息位之外的位数的令人困惑的表示法.所以在上面,用6位替换5位以获得更多的理智.
以下python代码根据一些变量计算执行内容的迭代次数.
# a - b - c is always a multiple of d.
i = (a - b - c) / d
while i:
# do stuff
i -= 1
Run Code Online (Sandbox Code Playgroud)
这些变量都将是同一种类型的,即只有ints或floats或什么的.我担心的是,如果值是否可以正常工作floats.我知道足以始终考虑依赖于精确浮点值的陷阱.但我不知道上述情况是否危险.我可以使用i = int(round((a - b - c) / d)),但我很好奇了解更好的花车.
这一切都归结为以下几点:a - b - c是一个完全相同的d.因此,我依赖于(a-b-c)/d成为一个i可以减去的值1并在while循环中获得预期的迭代次数,隐含的假设i == 0变为真.也就是说,像这样的计算倍数可以递减1来恰好达到0吗?
我不仅要知道它是否不安全,更重要的是,我需要了解浮点数来解决这样的问题?如果有人果断地知道这是否安全,是否有可能解释如何?
考虑以下简单结构:
struct Monster {
// ...
}
struct Player {
// ...
}
struct Missile {
// ...
//target: ???,
}
Run Code Online (Sandbox Code Playgroud)
在编写游戏逻辑时,让对象相互引用是很常见的.在上面的例子中,我们有结构Monster,Player并Missile说明所需的各种交互.
想象一下,我们有以下特点:Position和Think.以上所有结构都实现了Position,除了Missile实现之外的所有结构Think.
首先要注意的是它Missile是一个归航导弹:它存储一个目标,每当游戏更新Missile物体时,它就会将它移向它的目标.
据我所知,不可能Missile在Rust中合理地存储这个目标.
显然,导弹没有自己的目标.它只是想要访问它的Position特性.游戏对象必须通过Rc或共享Gc.但它并不像Missile只能存储Weak<???>具有Position特征的东西的引用.Box<Position>消耗具有该特征的任何对象的手段.A Box<Any>不允许向下转向特征.
制作Missile<T>和存储目标Weak<T>无济于事.怎么Missile<T>存储?在Collection<T>导弹所针对的每种物体中都有一种?游戏对象需要更通用,而可怕的游戏Box<Any>似乎是不可避免的.
我对Rust很新.令我感到困惑的是,这是不可能的.当然,我一定错过了什么?