我已经制作了一个自定义的"Coordinate"数据结构,它根据某个系统定义了一个对象的位置.
坐标定义如下:
public class Coordinate
{
public int X;
public int Y;
private int face;
public int Face
{
get { return face; }
set
{
if (value >= 6 | value < 0)
throw new Exception("Invalid face number");
else
face = value;
}
}
private int shell;
public int Shell
{
get { return shell; }
set
{
if (value < 0)
throw new Exception("No negative shell value allowed");
else
shell = value;
}
}
public Coordinate(int face, int …
Run Code Online (Sandbox Code Playgroud) 我正在为一个类做一个项目,该类专注于在内存中存储一个大部分为0值的巨大矩阵,并在其上执行一些矩阵数学运算.我的第一个想法是使用a HashMap
来存储矩阵元素,并且只存储非零的元素,以避免使用大量的内存.
我想创建一个关键字,HashMap
它代表元素的行号和列号,当我在地图中访问该条目时,我可以重新提取这两个值.我不知道Java和C# - 在C#中我会创建一个struct
with Row
和Column
成员,但在Java中我很快意识到没有用户值类型.在最后期限迫在眉睫的情况下,我选择了一个安全的赌注并且做了Key
很长时间.我使用一些非常简单的位移存储了前32位中的行数据(32位int)和最后32位中的列数据.[编辑:我还要注意我的HashMap初始化时使用了一个特定的初始大小,它准确地表示了我存储在其中的值的数量,这是永远不会超过的.]
[旁注:我希望能够再次提取行/列数据的原因是为了大大提高矩阵乘法的效率,O(n^2)
从而开始O(n)
,以及更小n
的引导]
实现这个结构后我注意到,从一个只给出非零元素的文本文件中读取一个23426 x 23426矩阵需要花费7秒钟,但只需要2秒钟来计算我们需要的特征值给!在选择性地评论出方法之后,我得出结论,这7秒的大部分时间用于存储我的值HashMap
.
public void Set(double value, int row, int column) {
//assemble the long key, placing row and column in adjacent sets of bits
long key = (long)row << SIZE_BIT_MAX; //(SIZE_BIT_MAX is 32)
key += column;
elements.put(key, value);
}
Run Code Online (Sandbox Code Playgroud)
这是设置值的代码.如果我改用这种方法:
public void Set(double value, int row, int column) {
//create a distinct but smaller …
Run Code Online (Sandbox Code Playgroud) 我是WPF用于触摸屏的新手,我在解释操作事件时遇到了麻烦.我想要做的是相当简单我相信:当用户捏住UserControl上的任何地方时,它将执行一个动作.
所以,在我的控件中(这是Surface 2.0/Windows Touch):
XAML
<Grid Background="White" IsManipulationEnabled="True"
ManipulationStarting="Grid_ManipulationStarting"
ManipulationDelta="Grid_ManipulationDelta">
<!--Some content controls-->
</Grid>
Run Code Online (Sandbox Code Playgroud)
C#
private void Grid_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = this;
}
private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//do the thing... you know, that thing you do
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在屏幕上搓手时,这些事件都不会触发.我想在这种情况下我不能理解事件的路由.我的显示器(3M MicroTouch PX)在ScatterViewItems中理解触摸事件或内置操作时没有遇到任何问题.
编辑:我从网格内部删除了控件,他们现在开火,所以我猜测内容正在拦截操纵.对不起,应该更清楚控件的内容,因为它们似乎是问题所在.
具体来说,我认为这与我在里面有一个SurfaceListBox的事实有关.我想象SurfaceListBox是拦截操作.有没有办法告诉它要走开?我仍然试图围绕WPF做事件的方式.
Edit2:要粘贴一些更完整的代码.
SEMANTICPANEL.XAML FULL
<UserControl x:Class="SemanticZoom.SemanticPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SemanticZoom"
xmlns:views="clr-namespace:SemanticZoom.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
</UserControl.Resources>
<Grid Background="White" IsManipulationEnabled="True" ManipulationStarting="Grid_ManipulationStarting" ManipulationDelta="Grid_ManipulationDelta">
<views:CategoryView x:Name="CategoryView"/>
<views:ShelfView x:Name="ShelfView" Visibility="Hidden" />
<views:BookView x:Name="BookView" Visibility="Hidden" />
</Grid> …
Run Code Online (Sandbox Code Playgroud) 这是一个用于将数据保存到一个困扰我的文件的方法:
public void SaveData(string filename, Action<StreamWriter> saveAction)
Run Code Online (Sandbox Code Playgroud)
该库的文档将第二个参数描述为"处理实际写入数据的委托操作".但是,我对如何为StreamWriter实际创建一个动作感到迷茫.我做了一些研究,发现很多这样的东西用作例子Action<T>
:
Action<string> s = Console.WriteLine;
Run Code Online (Sandbox Code Playgroud)
从那里我推断这<T>
是传递的参数类型,并且Action
是采用此参数的方法.我知道这可能是非常错误的.
基本上我想做的就是使用StreamWriter.WriteLine(string line)
.但是,我不能用
Action<StreamWriter> s = StreamWriter.WriteLine;
Run Code Online (Sandbox Code Playgroud)
因为这没有任何意义,而且StreamWriter.WriteLine
无论如何我都无法参考.现在我认为必须有一个方法,StreamWriter
从一个我不知道的类.如果有人愿意指出我正确的方向,我将非常感激.
你可能会问为什么我不能创造自己的StreamWriter
,并按照我一直这样做的方式去做.好吧,我正在ZNA上使用XNA,因此写入文本文件实际上很难获得所有级别的保护(所有应用程序都是沙箱),而我现在使用的库现在承诺更多如果我能弄清楚如何Action<T>
有效地使用,那就简化了.此外,我希望学习并更好地配备将来使用代表.
c# ×2
bit ×1
delegates ×1
hash ×1
hashmap ×1
java ×1
performance ×1
pixelsense ×1
wpf ×1
xna ×1