如何在java中有不同数量的元素时交换队列?

时间:2017-06-09 06:23:36

标签: java

我们如何以恒定的时间复杂度交换java中的两个队列? 我想过尝试一下。 是否可以写:

static int fpgaProbe(struct pci_dev *dev, const struct pci_device_id *id)
{
    struct DevInfo_t *devInfo = 0;
int i;
int nvec=0;

LOGINF("Benim ilk logum nvec:%d", nvec);
LOGINF("Probe Fonksiyonundayım\n");
printk(KERN_INFO "Vendor = 0x%x, Device = 0x%x \n", dev->vendor, dev->device);

//Allocate and zero memory for devInfo

devInfo = kzalloc(sizeof(struct DevInfo_t), GFP_KERNEL);
if (!devInfo)
{
    printk(KERN_WARNING "Couldn't allocate memory for device info!\n");
    return -1;
}

//Copy in the pci device info
devInfo->pciDev = dev;

//Save the device info itself into the pci driver
dev_set_drvdata(&dev->dev, (void*) devInfo);

// Try to dynamically allocate a major number for the device -- more difficult but worth it
majorNumber = register_chrdev(0, DEVICE_NAME, &fileOps);
if (majorNumber<0)
{
    printk(KERN_ALERT "failed to register a major number\n");
    return majorNumber;
}
printk(KERN_INFO "registered correctly with major number %d\n", majorNumber);

// Register the device class
fpgacharClass = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(fpgacharClass))
{                // Check for error and clean up if there is
    unregister_chrdev(majorNumber, DEVICE_NAME);
    printk(KERN_ALERT "Failed to register device class\n");
    return -1;          // Correct way to return an error on a pointer
}
printk(KERN_INFO "device class registered correctly\n");

// Register the device driver
fpgacharDevice = device_create(fpgacharClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME);
if (IS_ERR(fpgacharDevice))
{               // Clean up if there is an error
    class_destroy(fpgacharClass);           // Repeated code but the alternative is goto statements
    unregister_chrdev(majorNumber, DEVICE_NAME);
    printk(KERN_ALERT "Failed to create the device\n");
    return -1;
}
printk(KERN_INFO "device class created correctly\n"); // Made it! device was initialized

//Initialize other fields
devInfo->userPID = -1;
devInfo->buffer = kmalloc (BUFFER_SIZE * sizeof(char), GFP_KERNEL);

//Enable the PCI
if (pci_enable_device(dev))
{
    printk(KERN_WARNING "pci_enable_device() failed!\n");
    return -1;
}

//Enable PCI resources
if (pci_request_regions(dev, DRIVER_NAME))
{
    printk(KERN_WARNING "pci_request_regions() failed!\n");
    return -1;
}
pci_set_master(dev);

//Memory map the BAR regions into virtual memory space
//mapBars(devInfo);

//TODO: proper error catching and memory releasing
for(i=0;i<NUM_BARS;i++)
{
    devInfo->resource[i].type=-1;
    devInfo->resource[i].RegsBase=NULL;
    devInfo->resource[i].RegsLength=-1;
}
for(i=0;i<NUM_BARS;i++)
{
    if((pci_resource_flags(dev,i)&IORESOURCE_TYPE_BITS)==IORESOURCE_MEM)
    {
        printk(KERN_INFO "memmap1");
        devInfo->resource[i].type=IORESOURCE_MEM;
        devInfo->resource[i].flag=pci_resource_flags(dev,i);
        devInfo->resource[i].RegsLength=(u32)pci_resource_len(dev,i);
        devInfo->resource[i].RegsBase=ioremap_nocache(pci_resource_start(dev,i),pci_resource_len(dev,i));
        devInfo->resource[i].PhysBase.QuadPart=(u64)pci_resource_start(dev,i);

        if(devInfo->resource[i].RegsBase==NULL)
        {
            printk(KERN_ERR "pci_ioremap_bar da hata olustu!\n");
            return -ENOMEM;
        }
        printk(KERN_INFO "%d. bar icin type:%d",i,devInfo->resource[i].type);
        printk(KERN_INFO "%d. bar icin register length:%d",i,devInfo->resource[i].RegsLength);
        printk(KERN_INFO "%d. bar icin RegsBase:%p",i,devInfo->resource[i].RegsBase);


    }
    else if((pci_resource_flags(dev,i)&IORESOURCE_TYPE_BITS)==IORESOURCE_IO)
    {
        printk(KERN_INFO "iomap1\n");
        //no iomap
    }
}}

这里q1,q2和q3是基于链表的队列。

1 个答案:

答案 0 :(得分:0)

无法在恒定时间内交换两个队列。如果队列的大小为n,那么将一个队列的元素交换到另一个队列需要花费O(n)的时间。但是你可以改变两个队列的前后,这将花费O(1)即恒定时间。