示例#1
0
    public static DsDevice[] Sort(DsDevice[] devices, params object[] arg)
    {
      try
      {
        if (devices == null)
          return devices;
        if (devices.Length <= 1)
          return devices;
        List<string> compareNames = new List<string>();
        foreach (object obj in arg)
        {
          String name = obj as String;
          if (!string.IsNullOrEmpty(name))
          {
            compareNames.Add(name);
            continue;
          }
          DsDevice dev = obj as DsDevice;
          if (dev == null)
            continue;
          if (dev.Name == null)
            continue;
          if (dev.Name.Length == 0)
            continue;
          compareNames.Add(dev.Name);
        }
        if (compareNames.Count == 0)
          return devices;

        List<string> names = new List<string>();
        for (int i = 0; i < devices.Length; ++i)
        {
          if (devices[i] == null)
            continue;
          if (devices[i].Name == null)
            continue;
          if (devices[i].Name.Length == 0)
            continue;
          names.Add(devices[i].Name);
        }

        //sort the devices based 
        float[] results = new float[devices.Length + 20];
        for (int x = 0; x < results.Length; ++x)
        {
          results[x] = 0.0f;
        }

        for (int i = 0; i < compareNames.Count; ++i)
        {
          Levenstein comparer = new Levenstein();
          float[] tmpResults = comparer.batchCompareSet(names.ToArray(), compareNames[i]);
          for (int x = 0; x < tmpResults.Length; ++x)
          {
            results[x] += tmpResults[x];
          }
        }
        List<SortItem> items = new List<SortItem>();
        for (int i = 0; i < devices.Length; ++i)
        {
          SortItem item = new SortItem();
          item.rate = results[i];
          item.device = devices[i];
          items.Add(item);
        }
        items.Sort();
        DsDevice[] newDevices = new DsDevice[items.Count];

        int index = 0;
        foreach (SortItem item in items)
        {
          newDevices[index] = item.device;
          index++;
        }
        return newDevices;
      }
      catch (Exception ex)
      {
        Log.Log.Write(ex);
        return devices;
      }
    }
    private void mpListViewChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
      mpListViewMapped.BeginUpdate();
      try
      {
        mpListViewMapped.Items.Clear();
        if (mpListViewChannels.SelectedIndices == null)
          return;
        if (mpListViewChannels.SelectedIndices.Count != 1)
          return;
        Card card = ((CardInfo)mpComboBoxCard.SelectedItem).Card;
        ListViewItem selectedItem = mpListViewChannels.Items[mpListViewChannels.SelectedIndices[0]];
        Channel selectedChannel = (Channel)selectedItem.Tag;
        IList<Channel> allChannels = Channel.ListAll();
        List<ListViewItem> items = new List<ListViewItem>();
        NotifyForm dlg = new NotifyForm("Searching for Similar Channels...",
                                        "This can take some time\n\nPlease be patient...");
        dlg.Show(this);
        dlg.WaitForDisplay();
        foreach (Channel channel in allChannels)
        {
          if (channel.IsRadio == false)
            continue;

          bool isMapped = false;
          IList<ChannelMap> list = channel.ReferringChannelMap();
          foreach (ChannelMap map in list)
          {
            if (map.IdCard == card.IdCard)
            {
              isMapped = true;
              break;
            }
          }
          if (isMapped)
            continue;

          Levenstein comparer = new Levenstein();
          float result = comparer.getSimilarity(selectedChannel.DisplayName, channel.DisplayName);

          IList<TuningDetail> details = channel.ReferringTuningDetail();
          int imageIndex = GetImageIndex(details);
          ListViewItem item = new ListViewItem((result * 100f).ToString("f2") + "%", imageIndex);
          item.Tag = channel;
          item.SubItems.Add(channel.DisplayName);
          items.Add(item);
        }
        mpListViewMapped.Items.AddRange(items.ToArray());
        mpListViewMapped.Sort();
        dlg.Close();
      }
      finally
      {
        mpListViewMapped.EndUpdate();
      }
    }