小编Lia*_*amK的帖子

将SortedList转换为IOrderedEnumerable

我目前无法找到从SortedList获取IOrderedEnumerable的方法.

我有一个复杂的类型,我们现在只称它为'A',它可以被分解为一个类型'A'的枚举.我目前正在递归分解函数中创建一个SortedList,其中键a int与片段被分解的顺序相关:

private static SortedList<int, A> RecursivelyBuildMySortedList(A myValue)
{
    if (myValue == StopCondition())
    {
        return new SortedList<int, A> { { 1, myValue } };
    }

    var left = RecursivelyBuildMySortedList(myValue.Left);
    var right = RecursivelyBuildMySortedList(myValue.Right).Select(entry => new KeyValuePair<int, A>(entry.Key + left.Count, entry.Value)).ToList();
    right.ForEach(pair => left.Add(pair.Key, pair.Value));
    return left;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不想将SortedList公开给消费者,因为与分解顺序相关的键的值对消费者没有什么意义(特别是作为一个int).消费者唯一需要关心的是这些碎片的最终排序是什么,以便每件都能以正确的顺序进行处理.我更愿意向消费者公开IOrderedEnumerable.我认为这将是一个相当简单的任务,因为SortedList在许多方面与OrderedEnumerable非常相似,但我还没有找到一个好的转换:

public static IOrderedEnumerable<A> Decompose(A myValue)
{
    SortedList<int, A> mySortedList = RecursivelyBuildMySortedList(myValue);

    // Can't find a way to preserve the order of the objects during 'OrderBy'
    // mySortedList.Select(keyValuePair => …
Run Code Online (Sandbox Code Playgroud)

c# linq

6
推荐指数
1
解决办法
1121
查看次数

Pthread_mutex_lock返回访问错误

我正在编写一个读取和处理大量数据的程序.为了加快这个过程,我使用C++的Pthreads库实现了多线程读取/处理.然而,当我在我的互斥锁上调用pthread_mutex_lock(&lock1)时,我的程序错误导致我在lock1的地址上出现"访问冲突读取位置"错误.我需要在类的方法上运行这些线程,这需要在pthread库的限制范围内进行一些操作,并且它在某种操作中使我犯了错误.下面是我的代码的缩写版本,对不起tl;博士.在不遗漏任何对多线程至关重要的部分的情况下尽可能缩短它:

#include "pthread.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>


/*Ideally I need to create one instance of this object that has the run_stuff method 
being executed by two different threads.  I need both threads to have access to the 
same private variables, and have ensured that those variables won't be accessed at the 
same time. That is the end goal of my multithreading*/

class test_obj {
public:
    test_obj();
    void run_stuff(int); //the method I want to run on two …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex pthreads access-violation

3
推荐指数
1
解决办法
1805
查看次数

C++:当并非所有派生类都是预先知道的时候使用继承?

可能重复:
有没有办法从保存其类名的字符串中实例化对象?

我正在在具有基类一些C++代码中的问题InputFile,和多个派生类的:TxtInputFile,ASCInputFile,等等,其中每一个派生类是一个特定的输入类型.

我希望能够做的是从命令行中取一个变量,然后生成正确的派生类对象来处理指定的文件类型(例如,用户TXT在命令行中指示,因此,我生成一个TXTInputFile对象并在InputFile标签下返回以供在程序的其余部分使用).

我可以使用一串IF/'ELSE`语句来做这个,将用户输入与一堆预定的文件代码进行比较,但是我希望将来能够添加对新文件类型的支持而无需编辑字符串if语句和添加新文件代码等

有没有办法在运行时访问所有派生类的一些编译器生成的表到基类?

或者可能是某种基于传递参数等于动态绑定的多态构造函数?

(例如InputFile(string)...... TXTInputFile(string temp = "TXT"),ASCInputFile(string temp = ASC")......我意识到这是参数默认值的格式,只是试图用我的思路来表明我的目标.)

提前致谢.

c++ polymorphism inheritance class derived

1
推荐指数
1
解决办法
154
查看次数

C#:为什么以下比较表明0!= 0

我最近在一些比较代码中偶然发现了有趣的错误,其中两个对象的属性都等于0.0m.当该属性转换为int并进行比较时,比较永远不会相等.复制如下:

采用抽象A和两个实现B和C:

public abstract class A
{
    public decimal MyProp { get; set; }
}

public class B : A
{
}

public class C : A
{
}
Run Code Online (Sandbox Code Playgroud)

抽象定义了几个公共属性,主要是但不完全是十进制.所有公共属性都是原始属性.具体的子类型表示从两个不同的数据源获得的这种抽象.当且仅当它们的所有公共属性相等时,A类型的两个对象才被认为是相等的.需要注意的是:使用默认的舍入行为(MidpointRounding.ToEven),所有十进制属性都应在比较前转换为int.这导致了以下比较代码:

private static bool Compare(A a1, A a2)
{
    var propertiesList = typeof(A).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
    foreach (var propertyInfo in propertiesList)
    {
        var value1 = propertyInfo.GetValue(a1);
        var value2 = propertyInfo.GetValue(a2);

        if (propertyInfo.PropertyType == typeof(decimal))
        {
            value1 = Convert.ToInt32(value1);
            value2 = Convert.ToInt32(value2);
        }

        // debugger confirms that value1 is 0 and …
Run Code Online (Sandbox Code Playgroud)

c# reflection autoboxing equality

1
推荐指数
1
解决办法
195
查看次数