text:
package websocket // import "nhooyr.io/websocket"

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"io"
	"net/http"
	"reflect"
	"runtime"
	"strings"
	"sync"
	"syscall/js"

	"nhooyr.io/websocket/internal/bpool"
	"nhooyr.io/websocket/internal/wsjs"
	"nhooyr.io/websocket/internal/xsync"
)

// Conn provides a wrapper around the browser WebSocket API.
type Conn struct {
	ws wsjs.WebSocket

	// read limit for a message in bytes.
	msgReadLimit xsync.Int64

	closingMu     sync.Mutex
	isReadClosed  xsync.Int64
	closeOnce     sync.Once
	closed        chan struct{}
	closeErrOnce  sync.Once
	closeErr      error
	closeWasClean bool

	releaseOnClose   func()
	releaseOnMessage func()

	readSignal chan struct{}
	readBufMu  sync.Mutex
	readBuf    []wsjs.MessageEvent
}

func (c *Conn) close(err error, wasClean bool) {
	c.closeOnce.Do(func() {
		runtime.SetFinalizer(c, nil)

		if !wasClean {
			err = fmt.Errorf("unclean connection close: %w", err)
		}
		c.setCloseErr(err)
		c.closeWasClean = wasClean
		close(c.closed)
	})
}

func (c *Conn) init() {
	c.closed = make(chan struct{})
	c.readSignal = make(chan struct{}, 1)

	c.msgReadLimit.Store(32768)

	c.releaseOnClose = c.ws.OnClose(func(e wsjs.CloseEvent) {
		err := CloseError{
			Code:   StatusCode(e.Code),
			Reason: e.Reason,
		}
		// We do not know if we sent or received this close as
		// its possible the browser triggered it without us
		// explicitly sending it.
		c.close(err, e.WasClean)

		c.releaseOnClose()
		c.releaseOnMessage()
	})

	c.releaseOnMessage = c.ws.OnMessage(func(e wsjs.MessageEvent) {
		c.readBufMu.Lock()
		defer c.readBufMu.Unlock()

		c.readBuf = append(c.readBuf, e)

		// Lets the read goroutine know there is definitely something in readBuf.
		select {
		case c.readSignal <- struct{}{}:
		default:
		}
	})

	runtime.SetFinalizer(c, func(c *Conn) {
		c.setCloseErr(errors.New("connection garbage collected"))
		c.closeWithInternal()
	})
}

func (c *Conn) closeWithInternal() {
	c.Close(StatusInternalError, "something went wrong")
}

// Read attempts to read a message from the connection.
// The maximum time spent waiting is bounded by the context.
func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
	if c.isReadClosed.Load() == 1 {
		return 0, nil, errors.New("WebSocket connection read closed")
	}

	typ, p, err := c.read(ctx)
	if err != nil {
		return 0, nil, fmt.Errorf("failed to read: %w", err)
	}
	if int64(len(p)) > c.msgReadLimit.Load() {
		err := fmt.Errorf("read limited at %v bytes", c.msgReadLimit.Load())
		c.Close(StatusMessageTooBig, err.Error())
		return 0, nil, err
	}
	return typ, p, nil
}

func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) {
	select {
	case <-ctx.Done():
		c.Close(StatusPolicyViolation, "read timed out")
		return 0, nil, ctx.Err()
	case <-c.readSignal:
	case <-c.closed:
		return 0, nil, c.closeErr
	}

	c.readBufMu.Lock()
	defer c.readBufMu.Unlock()

	me := c.readBuf[0]
	// We copy the messages forward and decrease the size
	// of the slice to avoid reallocating.
	copy(c.readBuf, c.readBuf[1:])
	c.readBuf = c.readBuf[:len(c.readBuf)-1]

	if len(c.readBuf) > 0 {
		// Next time we read, we'll grab the message.
		select {
		case c.readSignal <- struct{}{}:
		default:
		}
	}

	switch p := me.Data.(type) {
	case string:
		return MessageText, []byte(p), nil
	case []byte:
		return MessageBinary, p, nil
	default:
		panic("websocket: unexpected data type from wsjs OnMessage: " + reflect.TypeOf(me.Data).String())
	}
}

// Ping is mocked out for Wasm.
func (c *Conn) Ping(ctx context.Context) error {
	return nil
}

// Write writes a message of the given type to the connection.
// Always non blocking.
func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
	err := c.write(ctx, typ, p)
	if err != nil {
		// Have to ensure the WebSocket is closed after a write error
		// to match the Go API. It can only error if the message type
		// is unexpected or the passed bytes contain invalid UTF-8 for
		// MessageText.
		err := fmt.Errorf("failed to write: %w", err)
		c.setCloseErr(err)
		c.closeWithInternal()
		return err
	}
	return nil
}

func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error {
	if c.isClosed() {
		return c.closeErr
	}
	switch typ {
	case MessageBinary:
		return c.ws.SendBytes(p)
	case MessageText:
		return c.ws.SendText(string(p))
	default:
		return fmt.Errorf("unexpected message type: %v", typ)
	}
}

// Close closes the WebSocket with the given code and reason.
// It will wait until the peer responds with a close frame
// or the connection is closed.
// It thus performs the full WebSocket close handshake.
func (c *Conn) Close(code StatusCode, reason string) error {
	err := c.exportedClose(code, reason)
	if err != nil {
		return fmt.Errorf("failed to close WebSocket: %w", err)
	}
	return nil
}

func (c *Conn) exportedClose(code StatusCode, reason string) error {
	c.closingMu.Lock()
	defer c.closingMu.Unlock()

	ce := fmt.Errorf("sent close: %w", CloseError{
		Code:   code,
		Reason: reason,
	})

	if c.isClosed() {
		return fmt.Errorf("tried to close with %q but connection already closed: %w", ce, c.closeErr)
	}

	c.setCloseErr(ce)
	err := c.ws.Close(int(code), reason)
	if err != nil {
		return err
	}

	<-c.closed
	if !c.closeWasClean {
		return c.closeErr
	}
	return nil
}

// Subprotocol returns the negotiated subprotocol.
// An empty string means the default protocol.
func (c *Conn) Subprotocol() string {
	return c.ws.Subprotocol()
}

// DialOptions represents the options available to pass to Dial.
type DialOptions struct {
	// Subprotocols lists the subprotocols to negotiate with the server.
	Subprotocols []string
}

// Dial creates a new WebSocket connection to the given url with the given options.
// The passed context bounds the maximum time spent waiting for the connection to open.
// The returned *http.Response is always nil or a mock. It's only in the signature
// to match the core API.
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
	c, resp, err := dial(ctx, url, opts)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to WebSocket dial %q: %w", url, err)
	}
	return c, resp, nil
}

func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
	if opts == nil {
		opts = &DialOptions{}
	}

	url = strings.Replace(url, "http://", "ws://", 1)
	url = strings.Replace(url, "https://", "wss://", 1)

	ws, err := wsjs.New(url, opts.Subprotocols)
	if err != nil {
		return nil, nil, err
	}

	c := &Conn{
		ws: ws,
	}
	c.init()

	opench := make(chan struct{})
	releaseOpen := ws.OnOpen(func(e js.Value) {
		close(opench)
	})
	defer releaseOpen()

	select {
	case <-ctx.Done():
		c.Close(StatusPolicyViolation, "dial timed out")
		return nil, nil, ctx.Err()
	case <-opench:
		return c, &http.Response{
			StatusCode: http.StatusSwitchingProtocols,
		}, nil
	case <-c.closed:
		return nil, nil, c.closeErr
	}
}

// Reader attempts to read a message from the connection.
// The maximum time spent waiting is bounded by the context.
func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) {
	typ, p, err := c.Read(ctx)
	if err != nil {
		return 0, nil, err
	}
	return typ, bytes.NewReader(p), nil
}

// Writer returns a writer to write a WebSocket data message to the connection.
// It buffers the entire message in memory and then sends it when the writer
// is closed.
func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error) {
	return writer{
		c:   c,
		ctx: ctx,
		typ: typ,
		b:   bpool.Get(),
	}, nil
}

type writer struct {
	closed bool

	c   *Conn
	ctx context.Context
	typ MessageType

	b *bytes.Buffer
}

func (w writer) Write(p []byte) (int, error) {
	if w.closed {
		return 0, errors.New("cannot write to closed writer")
	}
	n, err := w.b.Write(p)
	if err != nil {
		return n, fmt.Errorf("failed to write message: %w", err)
	}
	return n, nil
}

func (w writer) Close() error {
	if w.closed {
		return errors.New("cannot close closed writer")
	}
	w.closed = true
	defer bpool.Put(w.b)

	err := w.c.Write(w.ctx, w.typ, w.b.Bytes())
	if err != nil {
		return fmt.Errorf("failed to close writer: %w", err)
	}
	return nil
}

// CloseRead implements *Conn.CloseRead for wasm.
func (c *Conn) CloseRead(ctx context.Context) context.Context {
	c.isReadClosed.Store(1)

	ctx, cancel := context.WithCancel(ctx)
	go func() {
		defer cancel()
		c.read(ctx)
		c.Close(StatusPolicyViolation, "unexpected data message")
	}()
	return ctx
}

// SetReadLimit implements *Conn.SetReadLimit for wasm.
func (c *Conn) SetReadLimit(n int64) {
	c.msgReadLimit.Store(n)
}

func (c *Conn) setCloseErr(err error) {
	c.closeErrOnce.Do(func() {
		c.closeErr = fmt.Errorf("WebSocket closed: %w", err)
	})
}

func (c *Conn) isClosed() bool {
	select {
	case <-c.closed:
		return true
	default:
		return false
	}
}


text:
function Disable-UAC {
<#
.SYNOPSIS
Turns off Windows User Access Control

.LINK
http://boxstarter.codeplex.com
Enable-UAC

#>
    Write-BoxstarterMessage "Disabling UAC"
    Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name EnableLUA  -Value 0
}

redpajama_set_name: RedPajamaGithub


text:
use std::sync::Arc;
use std::thread;
use std::time::Duration;

struct JobStatus {
    jobs_completed: u32,
}

fn main() {
    let status = Arc::new(JobStatus { jobs_completed: 0 });
    let status_shared = status.clone();
    thread::spawn(move || {
        for _ in 0..10 {
            thread::sleep(Duration::from_millis(250));
            status_shared.jobs_completed += 1;
        }
    });
    while status.jobs_completed < 10 {
        println!("waiting... ");
        thread::sleep(Duration::from_millis(500));
    }
}

// `Arc` is an Atomic Reference Counted pointer that allows safe, shared access
// to **immutable** data. But we want to *change* the number of `jobs_completed`
// so we'll need to also use another type that will only allow one thread to
// mutate the data at a time. Take a look at this section of the book:
// https://doc.rust-lang.org/stable/book/concurrency.html#safe-shared-mutable-state
// and keep scrolling if you'd like more hints :)

// Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like:
// `let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));`
// Similar to the code in the example in the book that happens after the text
// that says "We can use Arc<T> to fix this.". If not, give that a try! If you
// do and would like more hints, keep scrolling!!

// Make sure neither of your threads are holding onto the lock of the mutex
// while they are sleeping, since this will prevent the other thread from
// being allowed to get the lock. Locks are automatically released when
// they go out of scope.

// Ok, so, real talk, this was actually tricky for *me* to do too. And
// I could see a lot of different problems you might run into, so at this
// point I'm not sure which one you've hit :) Please see a few possible
// answers on https://github.com/carols10cents/rustlings/issues/3 --
// mine is a little more complicated because I decided I wanted to see
// the number of jobs currently done when I was checking the status.

// Please open an issue if you're still running into a problem that
// these hints are not helping you with, or if you've looked at the sample
// answers and don't understand why they work and yours doesn't.

// If you've learned from the sample solutions, I encourage you to come
// back to this exercise and try it again in a few days to reinforce
// what you've learned :)


text:
package uk.gov.hmrc.helptosavefrontend.config

import play.api.Configuration
import play.api.http.DefaultHttpFilters
import uk.gov.hmrc.helptosavefrontend.controllers.ControllerSpecWithGuiceAppPerTest

class FiltersSpec extends ControllerSpecWithGuiceAppPerTest {
  val frontendFilters = new DefaultHttpFilters()
  val allowListFilter: AllowListFilter = mock[AllowListFilter]

  "Filters" must {

    "include the allowList filter if the allowList from config is non empty" in {
      val config = Configuration("http-header-ip-allowlist" → List("1.2.3"))

      val filters = new Filters(config, allowListFilter, frontendFilters)
      filters.filters shouldBe Seq(allowListFilter)
    }

    "not include the allowList filter if the allowList from config is empty" in {
      val config = Configuration("http-header-ip-allowlist" → List())

      val filters = new Filters(config, allowListFilter, frontendFilters)
      filters.filters shouldBe Seq()
    }
  }

}


text:
/**
@fileOverview

@toc
1. formProps
*/

'use strict';

var self;
// var yoThis =false;		//will be used for calling yeoman commands like 'this.spawnCommand'

/**
@param {Object} opts
*/
function Prompts(opts) {
	self =this;
}

/**
Copy over and extend defaults for prompts
@toc 1.
@method formProps
@param {Array} prompts
@param {Object} props
@param {Array} skipKeys Keys to skip (NOT copy over)
	@example ['optAppKeywords']
@param {Array} toInt Keys to turn into integers (instead of strings)
	@example ['optNpmInstall', 'optBowerInstall', 'optSeleniumInstall', 'optGruntQ']
@param {Object} params
@return {Object} The new/final props - WITHOUT skip keys and in proper format, with defaults extended if applicable, etc.
*/
Prompts.prototype.formProps =function(prompts, props, skipKeys, toInt, params) {
	var newProps ={};

	var ii, jj, kk, skip, curName;
	for(ii =0; ii<prompts.length; ii++) {
		curName =prompts[ii].name;
		skip =false;
		for(jj =0; jj<skipKeys.length; jj++) {
			if(curName ==skipKeys[jj]) {
				skip =true;
				break;
			}
		}
		if(!skip) {		//copy over
			//extend default if necessary
			if(props[curName] ===undefined && prompts[ii].default !==undefined) {
				console.log('setting default for property: '+curName+': '+prompts[ii].default);
				props[curName] =prompts[ii].default;
			}

			//convert to integer (from string) if necessary
			for(kk =0; kk<toInt.length; kk++) {
				if(curName ==toInt[kk]) {
					props[curName] =parseInt(props[curName], 10);
				}
			}

			newProps[curName] =props[curName];
		}
	}

	return newProps;
};

module.exports = new Prompts({});


text:
<ion-view view-title="Register" class="register-form" hide-back-button="true">
<ion-content class="center" scroll="false" locking="true">
  <form ng-submit="register(user)">

        Display Name
        <input type="text" placeholder="Display Name" ng-model="user.username" required>

        Password
        <input type="password" placeholder="Password" ng-model="user.password" required>

          Artist/Venue

        <select ng-model="user.userType">
          <option>Artist</option>
          <option selected>Venue</option>
        </select>

        Genre(s)
        <input type="text" placeholder="Genre(s)" ng-model="user.genre" required>

        Email
        <input type="email" placeholder="Email" ng-model="user.email" required>

        Location
        <input type="text" placeholder="Location" ng-model="user.location" required>

        Profile Image Url
        <input type="text" placeholder="Profile Image Url ex. http://placehold.it/150x150" ng-model="user.imgUrl">

        <button type="submit" name="button" class="button button-dark center">Submit</button>

  </form>
</ion-content>


text:
github-crash-course
===================

Git Crash Course, for others to be forked and tried

Crash Course
------------

First Step In the Crash course: Fork It!
Once done that using your github login, you may go ahead with cloning it.
You should find the clone URL in your forked repository.

Choose among HTTPS / SSH / SVN as transport protocol ("Clone URL")

Clone It!
---------

    aniruddha@al1:/media/aniruddha/repositories/git$ git clone https://github.com/<yourusername>/github-crash-course.git
    Cloning into 'github-crash-course'...
    remote: Counting objects: 4, done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 4 (delta 0), reused 4 (delta 0)
    Unpacking objects: 100% (4/4), done.
    Checking connectivity... done.
    aniruddha@al1:/media/aniruddha/repositories/git$ ls
    github-crash-course


text:
package com.bjzy.qctt.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

public class MySlideView extends ViewGroup{

	private View view;

	/**
	 * mScrollX 允许的最小值
	 */
	private int Min_X = -1000;

	/**
	 * 允许的最大值
	 */
	private int MAX_x = 0;

	private int MID = -300;

	private boolean isMenuShow = false;

	public MySlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//ll_select_bg = (LinearLayout) findViewById(R.id.ll_selectcar_bg);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		view = getChildAt(0);
		view.measure(widthMeasureSpec, heightMeasureSpec);
	}
	int starX;
	int starY;
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			starX = (int) ev.getX();
			starY = (int) ev.getY();
			break;
		case MotionEvent.ACTION_UP:

			break;
		case MotionEvent.ACTION_MOVE:
			int endX = (int) ev.getX();
			int endY = (int) ev.getY();
			int dX = endX-starX;
			int dY = endY-starY;
			if (dY>dX&&dY>com.riven.utils.volley.ScreenUtils.getWindowswidth()/4) {
			return false;
			}else if (dX>dY&&dX>com.riven.utils.volley.ScreenUtils.getWindowswidth()/6) {
				return true;
			}
			break;

		default:
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		view.layout(l, t, r, b);
	}

	private int lastX;
	private int firstX;

	private VelocityTracker mVelocityTracker;

	private boolean isFling;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);

		if(event.getX()<-getScrollX()){
			flushState();
			return false;
		}

		if(mVelocityTracker == null){
			mVelocityTracker = VelocityTracker.obtain();
		}
		mVelocityTracker.addMovement(event);

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			firstX = lastX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:

			int disX = (int) (lastX - event.getX());
			lastX = (int) event.getX();

			int nextScrollX = getScrollX()+disX;
			if(nextScrollX>= Min_X && nextScrollX <= MAX_x){

			}

			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:

			//先计算速度

			mVelocityTracker.computeCurrentVelocity(5500);
			float velocityX = mVelocityTracker.getXVelocity();
			if(Math.abs(velocityX) > ViewConfiguration.get(this.getContext()).getScaledMinimumFlingVelocity()){
				isFling = true;
				//发生了fling 事件
				if(velocityX>0){ // 向右飞
					isMenuShow = true;

				}else{ 	//向左飞
					isMenuShow = false;
				}
				flushState();
			}
			mVelocityTracker.recycle();
			mVelocityTracker = null;

			//再判断位置
			if(!isFling){
				int scrollX = getScrollX();

				if(scrollX>MID){ // 关闭菜单
					isMenuShow = false;
				}else{ //打开菜单
					isMenuShow = true;
				}
				flushState();
			}

			isFling = false;
			break;

		default:
			break;
		}

		return true;
	}

	public void flushState() {
//		if(isMenuShow){
//			scrollTo(Min_X, 0);
//			this.setVisibility(View.GONE);
//		}else{
//			scrollTo(0, 0);
//			this.setVisibility(View.VISIBLE);
//		}

		flushState(isMenuShow);
	}
	public void flushState(boolean isShow) {
		if(isShow){
			scrollTo(Min_X, 0);
			this.setVisibility(View.GONE);
		}else{
			this.setVisibility(View.VISIBLE);
			scrollTo(0, 0);
		}
	}

}


text:
This is a banner with similar style to the App Store. It has auto-slide to left every 10 seconds and manual-slide to both sides.
It can be used quickly only by passing an array of images.

Lenguage: Swift 3

Author: Alan Roldan

## Example:

## How to use?

Create a variable of type BannerView:
``` swift
@IBOutlet weak var baner: BannerView!
```
Import images and create a array of images:
```swift
//at this step, you download images from a URL or local directory
let imageName = ["img1","img2","img3","img4"]

//you create array of type UIImage
var arrayImages = [UIImage]()
for i in 0 ... imageName.count-1
{
    arrayImages.append(UIImage(named: imageName[i])!)
}
```
Create an array with links:
```swift
//Use nul for disable the link or use the string "http://url..." for enable the links
let urlList = ["http://url1..",nil,nil,"http://url4.."]
```
Instantiate class BannerView:
```swift
baner.createBanner(imagesArray: arrayImages) //for only images
//or this:
baner.createBanner(imagesArray: arrayImages, urlArray: urlList) //for images + url
```
You can also recharge the view items:
```swift
baner.refreshBanner(imagesArray: arrayImages) //for only images
//or this:
baner.refreshBanner(imagesArray: arrayImages, urlArray: urlList) //for images + url
```

 Images should be for proper operation of 720x300


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.Utilities
{
    /// <summary>
    /// The purpose of this class is to provide a cached reference to the main camera. Calling Camera.main
    /// executes a FindByTag on the scene, which will get worse and worse with more tagged objects.
    /// </summary>
    public static class CameraCache
    {
        private static Camera cachedCamera;

        /// <summary>
        /// Returns a cached reference to the main camera and uses Camera.main if it hasn't been cached yet.
        /// </summary>
        public static Camera Main
        {
            get
            {
                if (cachedCamera != null)
                {
                    if (cachedCamera.gameObject.activeInHierarchy)
                    {   // If the cached camera is active, return it
                        // Otherwise, our playspace may have been disabled
                        // We'll have to search for the next available
                        return cachedCamera;
                    }
                }

                // If the cached camera is null, search for main
                var mainCamera = Camera.main;

                if (mainCamera == null)
                {
                    Debug.Log("No main camera found. Searching for cameras in the scene.");

                    // If no main camera was found, try to determine one.
                    Camera[] cameras = GameObject.FindObjectsOfType<Camera>();
                    if (cameras.Length == 0)
                    {
                        Debug.LogWarning("No cameras found. Creating a \"MainCamera\".");
                        mainCamera = new GameObject("Main Camera", typeof(Camera), typeof(AudioListener)) { tag = "MainCamera" }.GetComponent<Camera>();
                    }
                    else
                    {
                        Debug.LogWarning("The Mixed Reality Toolkit requires one camera in the scene to be tagged as \"MainCamera\". Please ensure the application's main camera is tagged.");
                    }
                }

                // Cache the main camera
                cachedCamera = mainCamera;

                return cachedCamera;
            }
        }
    }
}


text:
package org.nd4j.linalg.dataset.api.preprocessor.stats;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.nd4j.linalg.api.memory.MemoryWorkspace;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.DataSetUtil;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;

import java.util.Arrays;

/**
 * Statistics about the lower bounds and upper bounds of values in data.
 * Can be constructed incrementally by using the DynamicCustomOpsBuilder,
 * which is useful for obtaining these statistics from an
 * iterator.
 *
 * @author Ede Meijer
 */
@EqualsAndHashCode
@Slf4j
public class MinMaxStats implements NormalizerStats {

    @Getter
    private final INDArray lower;
    @Getter
    private final INDArray upper;
    private INDArray range;

    /**
     * @param lower row vector of lower bounds
     * @param upper row vector of upper bounds
     */
    public MinMaxStats(@NonNull INDArray lower, @NonNull INDArray upper) {
        // Check for 0 differences and round up to epsilon
        INDArray diff = upper.sub(lower);
        INDArray addedPadding = Transforms.max(diff, Nd4j.EPS_THRESHOLD).subi(diff);
        // If any entry in `addedPadding` is not 0, then we had to add something to prevent 0 difference, Add this same
        // value to the upper bounds to actually apply the padding, and log about it
        if (addedPadding.sumNumber().doubleValue() > 0) {
            log.info("API_INFO: max val minus min val found to be zero. Transform will round up to epsilon to avoid nans.");
            upper.addi(addedPadding);
        }

        this.lower = lower;
        this.upper = upper;
    }

    /**
     * Get the feature wise
     * range for the statistics.
     * Note that this is a lazy getter.
     * It is only computed when needed.
     * @return the feature wise range
     * given the min and max
     */
    public INDArray getRange() {
        if (range == null) {
            try (MemoryWorkspace ws = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) {
                range = upper.sub(lower);
            }
        }
        return range;
    }

    /**
     * DynamicCustomOpsBuilder class that can incrementally update a running lower and upper bound in order to create statistics for a
     * large set of data
     */
    public static class Builder implements NormalizerStats.Builder<MinMaxStats> {
        private INDArray runningLower;
        private INDArray runningUpper;

        /**
         * Add the features of a DataSet to the statistics
         */
        public MinMaxStats.Builder addFeatures(@NonNull org.nd4j.linalg.dataset.api.DataSet dataSet) {
            return add(dataSet.getFeatures(), dataSet.getFeaturesMaskArray());
        }

        /**
         * Add the labels of a DataSet to the statistics
         */
        public MinMaxStats.Builder addLabels(@NonNull org.nd4j.linalg.dataset.api.DataSet dataSet) {
            return add(dataSet.getLabels(), dataSet.getLabelsMaskArray());
        }

        /**
         * Add rows of data to the statistics
         *
         * @param data the matrix containing multiple rows of data to include
         * @param mask (optionally) the mask of the data, useful for e.g. time series
         */
        public MinMaxStats.Builder add(@NonNull INDArray data, INDArray mask) {
            data = DataSetUtil.tailor2d(data, mask);
            if (data == null) {
                // Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
                // null pointer exceptions.
                return this;
            }

            INDArray tad = data.javaTensorAlongDimension(0, 0);
            INDArray batchMin = data.min(0);
            INDArray batchMax = data.max(0);
            if (!Arrays.equals(batchMin.shape(), batchMax.shape()))
                throw new IllegalStateException(
                                "Data min and max must be same shape. Likely a bug in the operation changing the input?");
            if (runningLower == null) {
                // First batch
                // Create copies because min and max are views to the same data set, which will cause problems with the
                // side effects of Transforms.min and Transforms.max
                runningLower = batchMin.dup();
                runningUpper = batchMax.dup();
            } else {
                // Update running bounds
                Transforms.min(runningLower, batchMin, false);
                Transforms.max(runningUpper, batchMax, false);
            }

            return this;
        }

        /**
         * Create a DistributionStats object from the data ingested so far. Can be used multiple times when updating
         * online.
         */
        public MinMaxStats build() {
            if (runningLower == null) {
                throw new RuntimeException("No data was added, statistics cannot be determined");
            }
            try (MemoryWorkspace workspace = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) {
                return new MinMaxStats(runningLower.dup(), runningUpper.dup());
            }
        }
    }
}


text:
package com.bukhmastov.cdoitmo.object.schedule.impl;

import com.bukhmastov.cdoitmo.event.bus.annotation.Event;
import com.bukhmastov.cdoitmo.event.events.ClearCacheEvent;
import com.bukhmastov.cdoitmo.factory.AppComponentProvider;
import com.bukhmastov.cdoitmo.firebase.FirebasePerformanceProvider;
import com.bukhmastov.cdoitmo.model.parser.ScheduleAttestationsParser;
import com.bukhmastov.cdoitmo.model.schedule.attestations.SAttestations;
import com.bukhmastov.cdoitmo.network.DeIfmoClient;
import com.bukhmastov.cdoitmo.network.handlers.RestResponseHandler;
import com.bukhmastov.cdoitmo.network.handlers.joiner.RestStringResponseHandlerJoiner;
import com.bukhmastov.cdoitmo.network.model.Client;
import com.bukhmastov.cdoitmo.object.schedule.ScheduleAttestations;
import com.bukhmastov.cdoitmo.util.singleton.StringUtils;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

public class ScheduleAttestationsImpl extends ScheduleImpl<SAttestations> implements ScheduleAttestations {

    private static final String TAG = "ScheduleExams";

    @Inject
    DeIfmoClient deIfmoClient;

    public ScheduleAttestationsImpl() {
        AppComponentProvider.getComponent().inject(this);
        eventBus.register(this);
    }

    @Event
    public void onClearCacheEvent(ClearCacheEvent event) {
        if (event.isNot(ClearCacheEvent.SCHEDULE_ATTESTATIONS)) {
            return;
        }
        clearLocalCache();
    }

    @Override
    protected void searchPersonal(int refreshRate, boolean forceToCache, boolean withUserChanges) throws Exception {
        log.v(TAG, "searchPersonal | personal schedule is unavailable");
        invokePendingAndClose("personal", withUserChanges, handler -> handler.onFailure(FAILED_INVALID_QUERY));
    }

    @Override
    protected void searchGroup(String group, int refreshRate, boolean forceToCache, boolean withUserChanges) throws Exception {
        List<String> sources = makeSources(SOURCE.DE_IFMO);
        log.v(TAG, "searchGroup | group=", group, " | refreshRate=", refreshRate,
                " | forceToCache=", forceToCache, " | withUserChanges=", withUserChanges, " | sources=", sources);
        searchByQuery(group, sources, refreshRate, withUserChanges, new SearchByQuery<SAttestations>() {
            @Override
            public void onWebRequest(String query, String source, RestResponseHandler<SAttestations> handler) {
                switch (source) {
                    case SOURCE.DE_IFMO: {
                        int term = getTerm();
                        String url = String.format("index.php?node=schedule&index=sched&semiId=%s&group=%s",
                                String.valueOf(term), StringUtils.prettifyGroupNumber(group));
                        deIfmoClient.get(context, url, null, new RestStringResponseHandlerJoiner(handler) {
                            @Override
                            public void onSuccess(int code, Client.Headers headers, String response) throws Exception {
                                SAttestations schedule = new ScheduleAttestationsParser(response, term).parse();
                                if (schedule == null) {
                                    handler.onFailure(code, headers, FAILED_LOAD);
                                    return;
                                }
                                schedule.setQuery(query);
                                schedule.setType("group");
                                schedule.setTitle(StringUtils.prettifyGroupNumber(group));
                                schedule.setTimestamp(time.getTimeInMillis());
                                schedule.setDataSource(source);
                                handler.onSuccess(code, headers, schedule);
                            }
                        });
                        break;
                    }
                }
            }
            @Override
            public void onFound(String query, SAttestations schedule, boolean fromCache) {
                onScheduleFound(query, schedule, forceToCache, fromCache, withUserChanges);
            }
        });
    }

    @Override
    protected void searchRoom(String room, int refreshRate, boolean forceToCache, boolean withUserChanges) throws Exception {
        log.v(TAG, "searchRoom | rooms schedule is unavailable");
        invokePendingAndClose(room, withUserChanges, handler -> handler.onFailure(FAILED_INVALID_QUERY));
    }

    @Override
    protected void searchTeacher(String teacherId, int refreshRate, boolean forceToCache, boolean withUserChanges) throws Exception {
        log.v(TAG, "searchTeacher | teacher schedule is unavailable");
        invokePendingAndClose(teacherId, withUserChanges, handler -> handler.onFailure(FAILED_INVALID_QUERY));
    }

    @Override
    protected void searchTeachers(String lastname, boolean withUserChanges) throws Exception {
        log.v(TAG, "searchTeachers | teachers schedule is unavailable");
        invokePendingAndClose(lastname, withUserChanges, handler -> handler.onFailure(FAILED_INVALID_QUERY));
    }

    @Override
    protected String getType() {
        return TYPE;
    }

    @Override
    protected String getDefaultSource() {
        return SOURCE.DE_IFMO;
    }

    @Override
    protected List<String> getSupportedSources() {
        return Collections.singletonList(SOURCE.DE_IFMO);
    }

    @Override
    protected SAttestations getNewInstance() {
        return new SAttestations();
    }

    @Override
    protected String getTraceName() {
        return FirebasePerformanceProvider.Trace.Schedule.ATTESTATIONS;
    }

    private int getTerm() {
        int term;
        try {
            term = Integer.parseInt(storagePref.get(context, "pref_schedule_attestations_term", "0"));
            if (term == 0) {
                int month = time.getCalendar().get(Calendar.MONTH);
                if (month >= Calendar.SEPTEMBER || month == Calendar.JANUARY) {
                    term = 1;
                } else {
                    term = 2;
                }
            } else if (term < 1) {
                term = 1;
            } else if (term > 2) {
                term = 2;
            }
        } catch (Exception e) {
            term = 1;
        }
        return term;
    }

    private void onScheduleFound(String query, SAttestations schedule, boolean forceToCache, boolean fromCache, boolean withUserChanges) {
        try {
            if (schedule == null) {
                invokePendingAndClose(query, withUserChanges, handler -> handler.onFailure(FAILED_NOT_FOUND));
                return;
            }
            if (!fromCache) {
                putToCache(query, schedule, forceToCache);
            }
            invokePendingAndClose(query, withUserChanges, handler -> handler.onSuccess(schedule, fromCache));
        } catch (Exception e) {
            log.exception(e);
            invokePendingAndClose(query, withUserChanges, handler -> handler.onFailure(FAILED_LOAD));
        }
    }
}


text:
namespace storage {

class QuotaManagerProxy;

struct MockStorageKeyData {
  const char* origin;
  blink::mojom::StorageType type;
  int64_t usage;
};

// Mock QuotaClient implementation for testing.
class MockQuotaClient : public mojom::QuotaClient {
 public:
  MockQuotaClient(scoped_refptr<QuotaManagerProxy> quota_manager_proxy,
                  base::span<const MockStorageKeyData> mock_data,
                  QuotaClientType client_type);

  MockQuotaClient(const MockQuotaClient&) = delete;
  MockQuotaClient& operator=(const MockQuotaClient&) = delete;

  ~MockQuotaClient() override;

  // To add or modify mock data in this client.
  void AddStorageKeyAndNotify(const blink::StorageKey& storage_key,
                              blink::mojom::StorageType type,
                              int64_t size);
  void ModifyStorageKeyAndNotify(const blink::StorageKey& storage_key,
                                 blink::mojom::StorageType type,
                                 int64_t delta);
  void TouchAllStorageKeysAndNotify();

  void AddStorageKeyToErrorSet(const blink::StorageKey& storage_key,
                               blink::mojom::StorageType type);

  base::Time IncrementMockTime();

  // QuotaClient.
  void GetStorageKeyUsage(const blink::StorageKey& storage_key,
                          blink::mojom::StorageType type,
                          GetStorageKeyUsageCallback callback) override;
  void GetStorageKeysForType(blink::mojom::StorageType type,
                             GetStorageKeysForTypeCallback callback) override;
  void GetStorageKeysForHost(blink::mojom::StorageType type,
                             const std::string& host,
                             GetStorageKeysForHostCallback callback) override;
  void DeleteStorageKeyData(const blink::StorageKey& storage_key,
                            blink::mojom::StorageType type,
                            DeleteStorageKeyDataCallback callback) override;
  void PerformStorageCleanup(blink::mojom::StorageType type,
                             PerformStorageCleanupCallback callback) override;

 private:
  void RunGetStorageKeyUsage(const blink::StorageKey& storage_key,
                             blink::mojom::StorageType type,
                             GetStorageKeyUsageCallback callback);
  void RunGetStorageKeysForType(blink::mojom::StorageType type,
                                GetStorageKeysForTypeCallback callback);
  void RunGetStorageKeysForHost(blink::mojom::StorageType type,
                                const std::string& host,
                                GetStorageKeysForTypeCallback callback);
  void RunDeleteStorageKeyData(const blink::StorageKey& storage_key,
                               blink::mojom::StorageType type,
                               DeleteStorageKeyDataCallback callback);

  const scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
  const QuotaClientType client_type_;

  std::map<std::pair<blink::StorageKey, blink::mojom::StorageType>, int64_t>
      storage_key_data_;
  std::set<std::pair<blink::StorageKey, blink::mojom::StorageType>>
      error_storage_keys_;

  int mock_time_counter_ = 0;

  base::WeakPtrFactory<MockQuotaClient> weak_factory_{this};
};

}  // namespace storage

#endif  // STORAGE_BROWSER_TEST_MOCK_QUOTA_CLIENT_H_


text:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Chapter 25.  DbstlDbt</title>
    <link rel="stylesheet" href="apiReference.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB C++ Standard Template Library API Reference" />
    <link rel="up" href="index.html" title="Berkeley DB C++ Standard Template Library API Reference" />
    <link rel="prev" href="stlElementRef_DB_STL_value.html" title="_DB_STL_value" />
    <link rel="next" href="stlDbstlDbtdstr_DbstlDbt.html" title="~DbstlDbt" />

          Chapter 25.
 DbstlDbt

          Prev

           Next

Chapter 25.
 DbstlDbt

You can persist all bytes in a chunk of contiguous memory by constructing an DbstlDbt
 object A(use malloc to allocate the required number of bytes for A.data and copy the bytes to be stored into A.data, set other fields as necessary) and store A into a container, e.g.

db_vector<DbstlDbt>, this stores the bytes rather than the object A into the underlying database. The DbstlDbt
 class can help you avoid memory leaks, so it is strongly recommended that you use DbstlDbt
 rather than Dbt class.

DbstlDbt
 derives from Dbt class, and it does an deep copy on copy construction and assignment --by calling malloc to allocate its own memory and then copying the bytes to it; Conversely the destructor will free the memory on destruction if the data pointer is non-NULL. The destructor assumes the memory is allocated via malloc, hence why you are required to call malloc to allocate memory in order to use DbstlDbt
.

DbstlDbt
 simply inherits all methods from Dbt with no extra new methods except the constructors/destructor and assignment operator, so it is easy to use.

In practice you rarely need to use DbstlDbt
 or Dbt because dbstl enables you to store any complex objects or primitive data. Only when you need to store raw bytes, e.g. a bitmap, do you need to use DbstlDbt
.

Hence, DbstlDbt
 is the right class to use to store any object into Berkeley DB via dbstl without memory leaks.

Don't free the memory referenced by DbstlDbt
 objects, it will be freed when the DbstlDbt
 object is destructed.

Please refer to the two examples using DbstlDbt
 in TestAssoc::test_arbitrary_object_storage and TestAssoc::test_char_star_string_storage member functions, which illustrate how to correctly use DbstlDbt
 in order to store raw bytes.

This class handles the task of allocating and de-allocating memory internally. Although it can be used to store data which cannot be handled by the DbstlElemTraits
 class, in practice, it is usually more convenient to register callbacks in the DbstlElemTraits
 class for the type you are storing/retrieving using dbstl.

 Public Members

          <colgroup>
            <col />
            <col />
          </colgroup>

              Member
              Description

DbstlDbt

Construct an object with an existing chunk of memory of size1 bytes, refered by data1,.

~DbstlDbt

The memory will be free'ed by the destructor.

operator=

The memory will be reallocated if neccessary.

Group

 Dbstl Helper Classes

DbstlDbt

Function Details

DbstlDbt(void *data1,
    u_int32_t size1)

Construct an object with an existing chunk of memory of size1 bytes, refered by data1,.

DbstlDbt()

DbstlDbt(const DbstlDbt &d)

This copy constructor does a deep copy.

Class

  DbstlDbt

          Prev

           Next

_DB_STL_value

            Home

~DbstlDbt