首页 \ 问答 \ 通过非案例类的字段排序PriorityQueue(Ordering a PriorityQueue by a field of a non-case class)

通过非案例类的字段排序PriorityQueue(Ordering a PriorityQueue by a field of a non-case class)

我目前正在尝试使用scala实现霍夫曼算法。 要做到这一点,我想我会使用PriorityQueue来根据它们的权重对树中不同节点的排序。 因此,我必须创建一个BinarySearchTree节点的PriorityQueue。 但是,Scala只允许我通过案例类的字段进行订购。

这是我想要的:

  class BinarySearchTree(weight: Int)
  case class ForkNode(left: BinarySearchTree, right: BinarySearchTree, chars: List[Char], weight: Int) extends BinarySearchTree(weight)
  case class LeafNode(char: Char, weight: Int) extends BinarySearchTree(weight)

  def createBST(inputFile: ListMap[Char,Int]): BinarySearchTree = {
    def weightOrder(t2: BinarySearchTree) = t2.weight
    val nodeMap:PriorityQueue[BinarySearchTree] = PriorityQueue(Ordering.by(weightOrder))
    null
  }

但它不编译。 但是, def weightOrder(t2: ForkNode) = t2.weight不会编译,但这不是我想要的。

我如何根据非案例类中的字段来排序我的优先级队列?


I'm currently trying to implement the huffman algorithm using scala. To do this, I'd figured I'd use a PriorityQueue for the ordering of the different Nodes in the tree based on their weight. Thus, I have to create a PriorityQueue of BinarySearchTree Nodes. However, Scala only lets me order by a field of a case class.

This is want I want:

  class BinarySearchTree(weight: Int)
  case class ForkNode(left: BinarySearchTree, right: BinarySearchTree, chars: List[Char], weight: Int) extends BinarySearchTree(weight)
  case class LeafNode(char: Char, weight: Int) extends BinarySearchTree(weight)

  def createBST(inputFile: ListMap[Char,Int]): BinarySearchTree = {
    def weightOrder(t2: BinarySearchTree) = t2.weight
    val nodeMap:PriorityQueue[BinarySearchTree] = PriorityQueue(Ordering.by(weightOrder))
    null
  }

But it doesn't compile. However, def weightOrder(t2: ForkNode) = t2.weight does compile, but that is not what I want.

How can I order my priority queue based on a field in a non-case class?


原文:https://stackoverflow.com/questions/47043379
更新时间:2023-08-14 18:08

最满意答案

您可以使用闪现的会话 。 尝试这个:

public function handleProviderCallback()
{
    try 
    {
        $user = Socialite::driver('google')->user();
    } 
    catch (Exception $e) 
    {
        return redirect('auth/google');
    }

    $authUser = User::where('google_id', $user->id)->first();

    if($authUser) //if user is found
    {
        Auth::login($authUser, true);
        return redirect('/home');
    }
    else //if user is not found, redirect to register page
    {
        return redirect('/register')->with('user', $user);
    }
}

并在你的注册视图

<input type='text' name='email' value='{{session("user")->email}}'>

You can use flashed session. Try this:

public function handleProviderCallback()
{
    try 
    {
        $user = Socialite::driver('google')->user();
    } 
    catch (Exception $e) 
    {
        return redirect('auth/google');
    }

    $authUser = User::where('google_id', $user->id)->first();

    if($authUser) //if user is found
    {
        Auth::login($authUser, true);
        return redirect('/home');
    }
    else //if user is not found, redirect to register page
    {
        return redirect('/register')->with('user', $user);
    }
}

and in your register view

<input type='text' name='email' value='{{session("user")->email}}'>

相关问答

更多
  • 我认为这将在https://packagist.org/packages/socialiteproviders/manager之后起作用 $clientId = "secret"; $clientSecret = "secret"; $redirectUrl = "yourdomain.com/api/redirect"; $additionalProviderConfig = ['site' => 'meta.stackoverflow.com']; $config = new \SocialitePro ...
  • 由于您的RegisterController使用了RegistersUsers特性,所有特征的方法都可用于RegisterController 。 您需要重写的方法,以防止用户在成功注册后登录的是register() 。 这是该方法的最初主体: public function register(Request $request) { $this->validator($request->all())->validate(); event(new Registered($user = $th ...
  • 我已经解决了这个问题在控制器laravel 5.4中使用parsedown,我们只需要初始化parsedown类而不使用像这样的命名空间: $parsedown = new \Parsedown(); $md = $parsedown->text('# this H1 tag'); I have solved this Question to use parsedown in controller laravel 5.4, we only initial parsedown class without ...
  • 您可以使用闪现的会话 。 尝试这个: public function handleProviderCallback() { try { $user = Socialite::driver('google')->user(); } catch (Exception $e) { return redirect('auth/google'); } $authUser = User::where('google_id', ...
  • Glyphicons包含在通过npm安装的bootstrap-sass中。 因此,如果要删除glyphicons,则必须在bootstrap.js文件中删除require('boostrap-sass')。 或者如果你真的只想删除字体,那么你删除 node_modules /自举萨斯/资产/字体/引导 目录,但这可能会显示一个错误,即没有找到glyphicons。 要解决上面的错误,你应该去 node_modules /自举萨斯/资产/样式表/ _bootstrap.scss 文件并删除此代码 @impor ...
  • 您需要安装Laravel FormCollective 。 从终端运行以下命令: composer require "laravelcollective/html":"^5.2.0" 接下来,将您的新提供程序添加到config/app.php的providers数组config/app.php : 'providers' => [ // ... Collective\Html\HtmlServiceProvider::class, // ... ], 最后,将两个类别名添加 ...
  • 我认为问题是你正在尝试为Laravel 5.3安装最新的社交网络套件,至少需要Laravel 5.4。 Problem 1 - laravel/socialite v3.0.0 requires illuminate/http ~5.4 -> satisfiable by 尝试在Laravel 5.4上安装。 检查一下: https://github.com/laravel/socialite/blob/3.0/composer.json "require": { "php": ">=5 ...
  • 您User_entity必须扩展Illuminate\Foundation\Auth\User才能使登录工作。 在默认的User模型中,这是别名为Authenticatable 。 目前您正在扩展Eloquent ,但Authenticatable扩展了Illuminate\Database\Eloquent\Model ,它将为您解决这个问题。 在User_entity模型中尝试此User_entity : use Illuminate\Foundation\Auth\User as Authentica ...
  • 从您的(dev-?)依赖项中删除mpociot/laravel-test-factory-helper或将其更新为支持Laravel 5.4的版本 Remove mpociot/laravel-test-factory-helper from your (dev-?) dependencies or update it to a version that supports Laravel 5.4
  • 您将无法使用查询,但如果要限制通过预先加载返回的字段,则可以使用select 。 您只需要确保包含id以便Eloquent可以正确匹配关系,例如: $something = Something::with(array('something_else' => function($query){ $query->select('id', 'field'); }))->first(); 希望这可以帮助! You wouldn't be able to use pluck on the query but ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。