亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用條件窗口而不是基于時間的窗口將整數流分割到緩沖區中?

如何使用條件窗口而不是基于時間的窗口將整數流分割到緩沖區中?

C#
藍山帝景 2023-07-09 17:45:59
我有一個 Observable 返回整數,如下所示:1, 1, 1, 1, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0如何將此 Observable 轉換為返回這些整數的數組,而不是按基于時間的窗口而是按基于值的窗口拆分流?這些整數是 Unity Update 事件中 Touch 的 FingerId。對于這項任務來說并不是那么重要,但為了解釋為什么我需要它,我必須提供這些詳細信息。-1 表示不觸摸。這就是差距。我需要刪除那些 -1 部分,并將流拆分到“無觸摸”時刻之間的 FingerId 緩沖區。我也可以這樣描述:Touch0, Touch0, Touch0, no Touch, no Touch, Touch1存在整數或其他類型并不重要。只需將流拆分為緩沖區,刪除“窗口值”。這是我的代碼,如果有幫助的話:var leftSideTouchStream = Observable.EveryUpdate()            .Scan(-1, (id, _) =>            {                if (id < 0)                {                    var leftSideTouches = Input.touches                        .Where(t =>                            t.phase == TouchPhase.Began                            && t.position.x < Screen.width / 2                        );                    return leftSideTouches.Any() ? leftSideTouches.First().fingerId : -1;                }                else                {                    var touchEnded = Input.touches                        .Any(t =>                            t.fingerId == id &&                            (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled)                        );                    return touchEnded ? -1 : id;                }            })            .Select(id =>            {                return Input.touches                    .Where(t => t.fingerId == id)                    .Select(t => new Nullable<Touch>(t))                    .FirstOrDefault();            });我需要與 Buffer 函數給出的行為完全相同的行為,但正如我所說,根據值而不是時間。如果我有這個流:1, 1, 1, 1, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0并且“窗口值”為-1,則預期結果為:[1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0]
查看完整描述

2 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

可能存在一些更好的魔法解決方案,但我想最直接的解決方案是這樣的


private static int[][] GetArrays(int[] intputArray)

{

    // use lists for dynamically adding elements

    var outputLists = new List<List<int>>();


    // initialize with the ignored value

    var lastValue = -1;


    // iterate over the inputArray

    foreach (var value in intputArray)

    {

        // skip -1 values

        if (value < 0)

        {

            lastValue = -1;

            continue;

        }


        // if a new value begin a new list

        if (lastValue != value)

        {

            outputLists.Add(new List<int>());

        }


        // add the value to the current (= last) list

        outputLists[outputLists.Count - 1].Add(value);


        // update the lastValue

        lastValue = value;

    }


    // convert to arrays

    // you could as well directly return the List<List<int>> instead

    // and access the values exactly the same way

    // but since you speak of buffers I guess you wanted arrays explicitely

    var outputArrays = new int[outputLists.Count][];

    for (var i = 0; i < outputLists.Count; i++)

    {

        outputArrays[i] = outputLists[i].ToArray();

    }


    return outputArrays;

}

如此呼喚


var arrays = GetArrays(new int[]{1, 1, 1, 1, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0});

應該導致


arrays[0] => [1, 1, 1, 1]

arrays[1] => [0, 0, 0, 0]

arrays[2] => [0, 0, 0]    

因為您似乎更想動態地一一添加值,所以我根本不會使用數組,而是使用類似的東西


private List<List<int>> arrays = new List<List<int>>();


private int lastValue;


private void AddValue(int value)

{

    // skip -1 values

    if (value < 0)

    {

        lastValue = -1;

        return;

    }


    // if a new value begin a new list

    if (lastValue != value)

    {

        arrays.Add(new List<int>());

    }


    // add the value to the current (= last) list

    arrays[outputLists.Count - 1].Add(value);


    // update the lastValue

    lastValue = value;

}

我的意思是你必須存儲在某個地方的東西


查看完整回答
反對 回復 2023-07-09
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

不幸的是我沒有找到任何開箱即用的解決方案,所以我想出了我自己的 Observable 實現:


using System;

using System.Collections.Generic;

using UniRx;


public static class ObservableFunctions

{

    public static IObservable<T[]> BufferWhen<T>(this IObservable<T> source, Predicate<T> predicate)

    {

        return Observable.Create<T[]>(observer =>

        {

            List<T> buffer = new List<T>();


            source.Subscribe(

                t =>

                {

                    if (predicate(t))

                    {

                        buffer.Add(t);

                    }

                    else

                    {

                        if (buffer.Count > 0)

                        {

                            observer.OnNext(buffer.ToArray());

                            buffer = new List<T>();

                        }

                    }

                },

                e =>

                {

                    observer.OnError(e);

                },

                () =>

                {

                    observer.OnCompleted();

                }

            );


            return Disposable.Empty;

        });

    }

}

幸運的是,事情非常簡單。比在 Google 中搜索適當的功能更簡單...


查看完整回答
反對 回復 2023-07-09
  • 2 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號